src/Security/Content/BlogVoter.php line 11

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