src/Security/Content/BlogCategoryVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Content;
  3. use App\Entity\Content\BlogCategory;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class BlogCategoryVoter extends Voter
  9. {
  10. const VIEW = 'BLOG_CATEGORY_VIEW';
  11. const VIEW_ANY = 'BLOG_CATEGORY_VIEW_ANY';
  12. const CREATE = 'BLOG_CATEGORY_CREATE';
  13. const EDIT = 'BLOG_CATEGORY_EDIT';
  14. const EDIT_ANY = 'BLOG_CATEGORY_EDIT_ANY';
  15. const DELETE = 'BLOG_CATEGORY_DELETE';
  16. /**
  17. * UserVoter constructor.
  18. */
  19. public function __construct(private readonly AccessDecisionManagerInterface $decisionManager)
  20. {
  21. }
  22. protected function supports($attribute, $subject): bool
  23. {
  24. // if the attribute isn't one we support, return false
  25. if (!in_array($attribute, [
  26. self::VIEW,
  27. self::VIEW_ANY,
  28. self::CREATE,
  29. self::EDIT,
  30. self::EDIT_ANY,
  31. self::DELETE,
  32. ], true)) {
  33. return false;
  34. }
  35. // only vote on Property objects inside this voter
  36. return !($subject && !$subject instanceof BlogCategory);
  37. }
  38. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  39. {
  40. if ($this->decisionManager->decide($token, ['ROLE_SUPER_ADMIN'])) {
  41. return true;
  42. }
  43. $user = $token->getUser();
  44. if (!$user instanceof UserInterface) {
  45. // the user must be logged in; if not, deny access
  46. return false;
  47. }
  48. return match ($attribute) {
  49. self::VIEW_ANY => $this->canViewAny($user),
  50. self::VIEW => $this->canView($subject, $user),
  51. self::CREATE => $this->canCreate($user),
  52. self::EDIT => $this->canEdit($subject),
  53. self::EDIT_ANY => $this->canEditAny($subject, $user),
  54. self::DELETE => $this->canDelete($subject, $user),
  55. default => throw new \LogicException('This code should not be reached!'),
  56. };
  57. }
  58. /**
  59. * Check if logged in User can view Property
  60. */
  61. private function canView(UserInterface $subject, UserInterface $user): bool
  62. {
  63. if ($this->canEdit($subject)) {
  64. return true;
  65. }
  66. return $this->isOwner($subject, $user);
  67. }
  68. /**
  69. * Check if logged in User can view Property
  70. */
  71. private function canViewAny(UserInterface $user): bool
  72. {
  73. return (bool) $user->hasRight(self::VIEW_ANY);
  74. }
  75. /**
  76. * Check if logged in User can create Property
  77. */
  78. private function canCreate(UserInterface $user): bool
  79. {
  80. if ($user->hasRight(self::CREATE)) {
  81. return true;
  82. }
  83. return $user->hasRole('ROLE_ADMIN');
  84. }
  85. /**
  86. * Check if logged in User can edit Property
  87. */
  88. private function canEdit(UserInterface $user): bool
  89. {
  90. return (bool) $user->hasRight(self::EDIT);
  91. }
  92. /**
  93. * Check if logged in User can print Property
  94. */
  95. private function canEditAny(UserInterface $subject, UserInterface $user): bool
  96. {
  97. if ($user->hasRight(self::EDIT_ANY)) {
  98. return true;
  99. }
  100. return $this->isOwner($subject, $user);
  101. }
  102. /**
  103. * Check if logged in User can delete Property
  104. */
  105. private function canDelete(UserInterface $subject, UserInterface $user): bool
  106. {
  107. if ($user->hasRight(self::DELETE)) {
  108. return true;
  109. }
  110. return $this->isOwner($subject, $user);
  111. }
  112. /**
  113. * Check if User if Owner of Subject/Property
  114. */
  115. private function isOwner(UserInterface $subject, UserInterface $user): bool
  116. {
  117. return $user->getId() === $subject->getId();
  118. }
  119. }