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