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. * @var AccessDecisionManagerInterface
  18. */
  19. private AccessDecisionManagerInterface $decisionManager;
  20. /**
  21. * UserVoter constructor.
  22. */
  23. public function __construct(AccessDecisionManagerInterface $decisionManager)
  24. {
  25. $this->decisionManager = $decisionManager;
  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, array(
  31. self::VIEW,
  32. self::VIEW_ANY,
  33. self::CREATE,
  34. self::EDIT,
  35. self::EDIT_ANY,
  36. self::DELETE,
  37. ), true)) {
  38. return false;
  39. }
  40. // only vote on Property objects inside this voter
  41. if ($subject && !$subject instanceof BlogCategory) {
  42. return false;
  43. }
  44. return true;
  45. }
  46. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  47. {
  48. if ($this->decisionManager->decide($token, array('ROLE_SUPER_ADMIN'))) {
  49. return true;
  50. }
  51. $user = $token->getUser();
  52. if (!$user instanceof UserInterface) {
  53. // the user must be logged in; if not, deny access
  54. return false;
  55. }
  56. switch ($attribute) {
  57. case self::VIEW_ANY:
  58. return $this->canViewAny($user);
  59. case self::VIEW:
  60. return $this->canView($subject, $user);
  61. case self::CREATE:
  62. return $this->canCreate($user);
  63. case self::EDIT:
  64. return $this->canEdit($subject, $user);
  65. case self::EDIT_ANY:
  66. return $this->canEditAny($subject, $user);
  67. case self::DELETE:
  68. return $this->canDelete($subject, $user);
  69. }
  70. throw new \LogicException('This code should not be reached!');
  71. }
  72. /**
  73. * Check if logged in User can view Property
  74. */
  75. private function canView(UserInterface $subject, UserInterface $user): bool
  76. {
  77. if ($this->canEdit($subject, $user)) {
  78. return true;
  79. }
  80. if ($this->isOwner($subject, $user)) {
  81. return true;
  82. }
  83. return false;
  84. }
  85. /**
  86. * Check if logged in User can view Property
  87. */
  88. private function canViewAny(UserInterface $user): bool
  89. {
  90. if ($user->hasRight(self::VIEW_ANY)) {
  91. return true;
  92. }
  93. return false;
  94. }
  95. /**
  96. * Check if logged in User can create Property
  97. */
  98. private function canCreate(UserInterface $user): bool
  99. {
  100. if ($user->hasRight(self::CREATE)) {
  101. return true;
  102. }
  103. return $user->hasRole('ROLE_ADMIN');
  104. }
  105. /**
  106. * Check if logged in User can edit Property
  107. */
  108. private function canEdit(UserInterface $user): bool
  109. {
  110. if ($user->hasRight(self::EDIT)) {
  111. return true;
  112. }
  113. return false;
  114. }
  115. /**
  116. * Check if logged in User can print Property
  117. */
  118. private function canEditAny(UserInterface $subject, UserInterface $user): bool
  119. {
  120. if ($user->hasRight(self::EDIT_ANY)) {
  121. return true;
  122. }
  123. if ($this->isOwner($subject, $user)) {
  124. return true;
  125. }
  126. return false;
  127. }
  128. /**
  129. * Check if logged in User can delete Property
  130. */
  131. private function canDelete(UserInterface $subject, UserInterface $user): bool
  132. {
  133. if ($user->hasRight(self::DELETE)) {
  134. return true;
  135. }
  136. if ($this->isOwner($subject, $user)) {
  137. return true;
  138. }
  139. return false;
  140. }
  141. /**
  142. * Check if User if Owner of Subject/Property
  143. */
  144. private function isOwner(UserInterface $subject, UserInterface $user): bool
  145. {
  146. return $user->getId() === $subject->getId();
  147. }
  148. }