src/Security/Core/ProblemVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security\Core;
  3. use App\Services\Admin\EasyAdminService;
  4. use App\Entity\Core\Mathproblem;
  5. use App\Entity\Core\Quiz\Quiz;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Exception;
  8. use LogicException;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use Symfony\Component\Security\Core\Security;
  13. class ProblemVoter extends Voter
  14. {
  15. const PERMISSION = 'problemEntityPermission';
  16. const INDEX_ACTION = 'problemIndexAction';
  17. const NEW_ACTION = 'problemNewAction';
  18. const EDIT_ACTION = 'problemEditAction';
  19. const DELETE_ACTION = 'problemDeleteAction';
  20. private Security $security;
  21. private EntityManagerInterface $em;
  22. private EasyAdminService $easyAdminService;
  23. private RequestStack $requestStack;
  24. public function __construct(Security $security, EntityManagerInterface $em, EasyAdminService $easyAdminService, RequestStack $requestStack)
  25. {
  26. $this->security = $security;
  27. $this->em = $em;
  28. $this->easyAdminService = $easyAdminService;
  29. $this->requestStack = $requestStack;
  30. }
  31. protected function supports(string $attribute, $subject): bool
  32. {
  33. // For index and new, $subject will always be null. For permission, it will be null when trying to create a new entity.
  34. if (in_array($attribute, [self::INDEX_ACTION, self::NEW_ACTION, self::PERMISSION])) {
  35. return true;
  36. }
  37. if (in_array($attribute, [self::EDIT_ACTION, self::DELETE_ACTION])) {
  38. return $subject instanceof Mathproblem;
  39. }
  40. return false;
  41. }
  42. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  43. {
  44. if ($attribute === self::INDEX_ACTION) {
  45. // Allow everyone to list - the entity permissions will still apply and hide entities you are not allowed
  46. // to access.
  47. return true;
  48. }
  49. if ($attribute === self::NEW_ACTION || $attribute === self::PERMISSION && $subject === null) {
  50. // Anyone with cms access should be able to create new problems
  51. return true;
  52. }
  53. if (!$subject instanceof Mathproblem) {
  54. throw new LogicException("Invalid type for voter and attribute.");
  55. }
  56. return $this->checkEntityPermissions($subject, $attribute);
  57. }
  58. public function checkEntityPermissions(Mathproblem $subject): bool
  59. {
  60. // ROLE_SUPER_ADMIN inherits ROLE_ADMIN, and will also be included here.
  61. if ($this->security->isGranted('ROLE_ADMIN')) {
  62. return true;
  63. }
  64. if ($subject->getId() === null) {
  65. // Creating a new problem via MathproblemFromQuizCrudController
  66. try {
  67. $quiz = $this->easyAdminService->getQuiz($this->requestStack->getCurrentRequest());
  68. return $this->checkPermissionByQuiz($quiz);
  69. } catch (Exception) {
  70. return false;
  71. }
  72. }
  73. return $this->checkPermissionByProblem($subject);
  74. }
  75. private function checkPermissionByProblem(Mathproblem $problem): bool
  76. {
  77. $dql = <<<DQL
  78. SELECT p.id
  79. FROM App\Entity\Core\RelationQuizMathproblem rel
  80. JOIN rel.quiz q
  81. JOIN q.publication pc
  82. JOIN pc.publisher ps
  83. JOIN App\Entity\Core\PublisherPermission p WITH p.publisher = ps
  84. WHERE p.user = ?1
  85. AND rel.mathproblem = ?2
  86. DQL;
  87. return $this->runQuery($dql, $problem);
  88. }
  89. private function checkPermissionByQuiz(Quiz $quiz): bool
  90. {
  91. $dql = <<<DQL
  92. SELECT p.id
  93. FROM App\Entity\Core\Quiz\Quiz q
  94. JOIN q.publication pc
  95. JOIN pc.publisher ps
  96. JOIN App\Entity\Core\PublisherPermission p WITH p.publisher = ps
  97. WHERE p.user = ?1
  98. AND q = ?2
  99. DQL;
  100. return $this->runQuery($dql, $quiz);
  101. }
  102. private function runQuery(string $dql, Mathproblem|Quiz $parameter): bool
  103. {
  104. if ($this->security->isGranted('ROLE_EDITOR'))
  105. $dql .= PHP_EOL . 'AND (p.editorPermission = true OR p.publication = pc)';
  106. else if ($this->security->isGranted('ROLE_AUTHOR'))
  107. $dql .= PHP_EOL . 'AND p.publication = pc';
  108. else
  109. $dql .= PHP_EOL . 'AND 0 = 1';
  110. $query = $this->em->createQuery($dql);
  111. $query->setParameter(1, $this->security->getUser());
  112. $query->setParameter(2, $parameter);
  113. $result = $query->getResult();
  114. return !empty($result);
  115. }
  116. }