src/Security/Content/BlogCommentVoter.php line 11

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