src/Security/Core/QuizVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Core;
  3. use App\Services\Admin\EasyAdminService;
  4. use App\Entity\Core\Quiz\Quiz;
  5. use Exception;
  6. use LogicException;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. class QuizVoter extends Voter
  12. {
  13. const PERMISSION = 'quizEntityPermission';
  14. const INDEX_ACTION = 'quizIndexAction';
  15. const NEW_ACTION = 'quizNewAction';
  16. const EDIT_ACTION = 'quizEditAction';
  17. const DELETE_ACTION = 'quizDeleteAction';
  18. private Security $security;
  19. private EasyAdminService $easyAdminService;
  20. private RequestStack $requestStack;
  21. public function __construct(Security $security, EasyAdminService $easyAdminService, RequestStack $requestStack)
  22. {
  23. $this->security = $security;
  24. $this->easyAdminService = $easyAdminService;
  25. $this->requestStack = $requestStack;
  26. }
  27. protected function supports(string $attribute, $subject): bool
  28. {
  29. // For index and new, $subject will always be null. For permission, it will be null when trying to create a new entity.
  30. if (in_array($attribute, [self::INDEX_ACTION, self::NEW_ACTION, self::PERMISSION])) {
  31. return true;
  32. }
  33. if (in_array($attribute, [self::EDIT_ACTION, self::DELETE_ACTION])) {
  34. return $subject instanceof Quiz;
  35. }
  36. return false;
  37. }
  38. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  39. {
  40. if ($attribute === self::INDEX_ACTION) {
  41. // Allow everyone to list - the entity permissions will still apply and hide entities you are not allowed
  42. // to access.
  43. return true;
  44. }
  45. if ($attribute === self::NEW_ACTION || $attribute === self::PERMISSION && $subject === null) {
  46. // Anyone with cms access should be able to create new quizzes
  47. return true;
  48. }
  49. if (!$subject instanceof Quiz) {
  50. throw new LogicException("Invalid type for voter and attribute.");
  51. }
  52. return $this->checkEntityPermissions($subject);
  53. }
  54. public function checkEntityPermissions(Quiz $subject): bool
  55. {
  56. // ROLE_SUPER_ADMIN inherits ROLE_ADMIN, and will also be included here.
  57. if ($this->security->isGranted('ROLE_ADMIN')) {
  58. return true;
  59. }
  60. $publication = $subject->getPublication();
  61. if ($publication === null) {
  62. try {
  63. // When creating a new quiz in QuizFromPublicationCrudController
  64. $publication = $this->easyAdminService->getPublication($this->requestStack->getCurrentRequest());
  65. } catch (Exception) {}
  66. }
  67. if ($publication !== null) {
  68. return $this->easyAdminService->userCanAccessPublication($publication);
  69. }
  70. return false;
  71. }
  72. }