src/Security/Core/PublisherVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Core;
  3. use App\Entity\Core\Publisher;
  4. use App\Entity\Core\PublisherPermission;
  5. use App\Entity\Core\PublisherPermissionRepository;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class PublisherVoter extends Voter
  11. {
  12. const PERMISSION = 'publisherEntityPermission';
  13. const INDEX_ACTION = 'publisherIndexAction';
  14. const NEW_ACTION = 'publisherNewAction';
  15. const EDIT_ACTION = 'publisherEditAction';
  16. const DELETE_ACTION = 'publisherDeleteAction';
  17. private EntityManagerInterface $em;
  18. private Security $security;
  19. public function __construct(EntityManagerInterface $em, Security $security)
  20. {
  21. $this->em = $em;
  22. $this->security = $security;
  23. }
  24. protected function supports(string $attribute, $subject): bool
  25. {
  26. // For index and new, $subject will always be null. For permission, it will be null when trying to create a new entity.
  27. if (in_array($attribute, [self::INDEX_ACTION, self::NEW_ACTION, self::PERMISSION])) {
  28. return true;
  29. }
  30. if (in_array($attribute, [self::EDIT_ACTION, self::DELETE_ACTION])) {
  31. return $subject instanceof Publisher;
  32. }
  33. return false;
  34. }
  35. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  36. {
  37. if ($attribute === self::INDEX_ACTION) {
  38. // Allow everyone to list - the entity permissions will still apply and hide entities you are not allowed
  39. // to access.
  40. return true;
  41. }
  42. if ($attribute === self::NEW_ACTION || $attribute === self::PERMISSION && $subject === null) {
  43. // Includes ROLE_SUPER_ADMIN by inheritance.
  44. // Editors and authors should not be allowed to create new publishers.
  45. return $this->security->isGranted('ROLE_ADMIN');
  46. }
  47. if (!$subject instanceof Publisher) {
  48. throw new \LogicException("Invalid type for voter and attribute.");
  49. }
  50. return $this->checkEntityPermissions($attribute, $subject, $token);
  51. }
  52. public function checkEntityPermissions(string $attribute, Publisher $subject, TokenInterface $token): bool
  53. {
  54. if ($subject->isDeleted() || $subject->isHidden()) {
  55. return false;
  56. }
  57. // ROLE_SUPER_ADMIN inherits ROLE_ADMIN, and will also be included here.
  58. if ($this->security->isGranted('ROLE_ADMIN')) {
  59. return true;
  60. }
  61. if ($attribute !== self::PERMISSION) {
  62. // Don't allow editors or authors to edit or delete publishers.
  63. return false;
  64. }
  65. if ($this->security->isGranted('ROLE_EDITOR')) {
  66. /** @var PublisherPermissionRepository $repo */
  67. $repo = $this->em->getRepository(PublisherPermission::class);
  68. return $repo->hasEditorPermission($token->getUser(), $subject);
  69. }
  70. if ($this->security->isGranted('ROLE_AUTHOR')) {
  71. /** @var PublisherPermissionRepository $repo */
  72. $repo = $this->em->getRepository(PublisherPermission::class);
  73. return $repo->hasAuthorPermissionForPublisher($token->getUser(), $subject);
  74. }
  75. return false;
  76. }
  77. }