<?php
namespace App\Security\Core;
use App\Services\Admin\EasyAdminService;
use App\Entity\Core\Mathproblem;
use App\Entity\Core\Quiz\Quiz;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use LogicException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class ProblemVoter extends Voter
{
const PERMISSION = 'problemEntityPermission';
const INDEX_ACTION = 'problemIndexAction';
const NEW_ACTION = 'problemNewAction';
const EDIT_ACTION = 'problemEditAction';
const DELETE_ACTION = 'problemDeleteAction';
private Security $security;
private EntityManagerInterface $em;
private EasyAdminService $easyAdminService;
private RequestStack $requestStack;
public function __construct(Security $security, EntityManagerInterface $em, EasyAdminService $easyAdminService, RequestStack $requestStack)
{
$this->security = $security;
$this->em = $em;
$this->easyAdminService = $easyAdminService;
$this->requestStack = $requestStack;
}
protected function supports(string $attribute, $subject): bool
{
// For index and new, $subject will always be null. For permission, it will be null when trying to create a new entity.
if (in_array($attribute, [self::INDEX_ACTION, self::NEW_ACTION, self::PERMISSION])) {
return true;
}
if (in_array($attribute, [self::EDIT_ACTION, self::DELETE_ACTION])) {
return $subject instanceof Mathproblem;
}
return false;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
if ($attribute === self::INDEX_ACTION) {
// Allow everyone to list - the entity permissions will still apply and hide entities you are not allowed
// to access.
return true;
}
if ($attribute === self::NEW_ACTION || $attribute === self::PERMISSION && $subject === null) {
// Anyone with cms access should be able to create new problems
return true;
}
if (!$subject instanceof Mathproblem) {
throw new LogicException("Invalid type for voter and attribute.");
}
return $this->checkEntityPermissions($subject, $attribute);
}
public function checkEntityPermissions(Mathproblem $subject): bool
{
// ROLE_SUPER_ADMIN inherits ROLE_ADMIN, and will also be included here.
if ($this->security->isGranted('ROLE_ADMIN')) {
return true;
}
if ($subject->getId() === null) {
// Creating a new problem via MathproblemFromQuizCrudController
try {
$quiz = $this->easyAdminService->getQuiz($this->requestStack->getCurrentRequest());
return $this->checkPermissionByQuiz($quiz);
} catch (Exception) {
return false;
}
}
return $this->checkPermissionByProblem($subject);
}
private function checkPermissionByProblem(Mathproblem $problem): bool
{
$dql = <<<DQL
SELECT p.id
FROM App\Entity\Core\RelationQuizMathproblem rel
JOIN rel.quiz q
JOIN q.publication pc
JOIN pc.publisher ps
JOIN App\Entity\Core\PublisherPermission p WITH p.publisher = ps
WHERE p.user = ?1
AND rel.mathproblem = ?2
DQL;
return $this->runQuery($dql, $problem);
}
private function checkPermissionByQuiz(Quiz $quiz): bool
{
$dql = <<<DQL
SELECT p.id
FROM App\Entity\Core\Quiz\Quiz q
JOIN q.publication pc
JOIN pc.publisher ps
JOIN App\Entity\Core\PublisherPermission p WITH p.publisher = ps
WHERE p.user = ?1
AND q = ?2
DQL;
return $this->runQuery($dql, $quiz);
}
private function runQuery(string $dql, Mathproblem|Quiz $parameter): bool
{
if ($this->security->isGranted('ROLE_EDITOR'))
$dql .= PHP_EOL . 'AND (p.editorPermission = true OR p.publication = pc)';
else if ($this->security->isGranted('ROLE_AUTHOR'))
$dql .= PHP_EOL . 'AND p.publication = pc';
else
$dql .= PHP_EOL . 'AND 0 = 1';
$query = $this->em->createQuery($dql);
$query->setParameter(1, $this->security->getUser());
$query->setParameter(2, $parameter);
$result = $query->getResult();
return !empty($result);
}
}