src/Security/Core/PublisherVoter.php line 14

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