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. * @var AccessDecisionManagerInterface
  19. */
  20. private $decisionManager;
  21. /**
  22. * UserVoter constructor.
  23. *
  24. * @param AccessDecisionManagerInterface $decisionManager
  25. */
  26. public function __construct(AccessDecisionManagerInterface $decisionManager)
  27. {
  28. $this->decisionManager = $decisionManager;
  29. }
  30. /**
  31. * @param string $attribute
  32. * @param mixed $subject
  33. *
  34. * @return bool
  35. */
  36. protected function supports($attribute, $subject): bool
  37. {
  38. // if the attribute isn't one we support, return false
  39. if (!in_array($attribute, array(
  40. self::VIEW,
  41. self::VIEW_ANY,
  42. self::CREATE,
  43. self::EDIT,
  44. self::EDIT_ANY,
  45. self::PRINT,
  46. self::DELETE,
  47. ), true)) {
  48. return false;
  49. }
  50. // only vote on Property objects inside this voter
  51. if ($subject && !$subject instanceof Blog) {
  52. return false;
  53. }
  54. return true;
  55. }
  56. /**
  57. * @param string $attribute
  58. * @param mixed $subject
  59. * @param TokenInterface $token
  60. *
  61. * @return bool
  62. */
  63. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  64. {
  65. if ($this->decisionManager->decide($token, array('ROLE_SUPER_ADMIN'))) {
  66. return true;
  67. }
  68. $user = $token->getUser();
  69. if (!$user instanceof UserInterface) {
  70. // the user must be logged in; if not, deny access
  71. return false;
  72. }
  73. switch ($attribute) {
  74. case self::VIEW_ANY:
  75. return $this->canViewAny($user);
  76. case self::VIEW:
  77. return $this->canView($subject, $user);
  78. case self::CREATE:
  79. return $this->canCreate($user);
  80. case self::EDIT:
  81. return $this->canEdit($subject, $user);
  82. case self::EDIT_ANY:
  83. return $this->canEditAny($subject, $user);
  84. case self::DELETE:
  85. return $this->canDelete($subject, $user);
  86. }
  87. throw new \LogicException('This code should not be reached!');
  88. }
  89. /**
  90. * Check if logged in User can view Property
  91. *
  92. * @param UserInterface $subject
  93. * @param UserInterface $user
  94. *
  95. * @return bool
  96. */
  97. private function canView(UserInterface $subject, UserInterface $user): bool
  98. {
  99. if ($this->canEdit($subject, $user)) {
  100. return true;
  101. }
  102. if ($this->isOwner($subject, $user)) {
  103. return true;
  104. }
  105. return false;
  106. }
  107. /**
  108. * Check if logged in User can view Property
  109. *
  110. * @param UserInterface $user
  111. *
  112. * @return bool
  113. */
  114. private function canViewAny(UserInterface $user): bool
  115. {
  116. if ($user->hasRight(self::VIEW_ANY)) {
  117. return true;
  118. }
  119. return false;
  120. }
  121. /**
  122. * Check if logged in User can create Property
  123. *
  124. * @param UserInterface $user
  125. *
  126. * @return bool
  127. */
  128. private function canCreate(UserInterface $user): bool
  129. {
  130. if ($user->hasRight(self::CREATE)) {
  131. return true;
  132. }
  133. return $user->hasRole('ROLE_ADMIN');
  134. }
  135. /**
  136. * Check if logged in User can edit Property
  137. *
  138. * @param UserInterface $subject
  139. * @param UserInterface $user
  140. *
  141. * @return bool
  142. */
  143. private function canEdit(UserInterface $subject, UserInterface $user): bool
  144. {
  145. if ($user->hasRight(self::EDIT)) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. /**
  151. * Check if logged in User can print Property
  152. *
  153. * @param UserInterface $subject
  154. * @param UserInterface $user
  155. *
  156. * @return bool
  157. */
  158. private function canEditAny(UserInterface $subject, UserInterface $user): bool
  159. {
  160. if ($user->hasRight(self::EDIT_ANY)) {
  161. return true;
  162. }
  163. if ($this->isOwner($subject, $user)) {
  164. return true;
  165. }
  166. return false;
  167. }
  168. /**
  169. * Check if logged in User can delete Property
  170. *
  171. * @param UserInterface $subject
  172. * @param UserInterface $user
  173. *
  174. * @return bool
  175. */
  176. private function canDelete(UserInterface $subject, UserInterface $user): bool
  177. {
  178. if ($user->hasRight(self::DELETE)) {
  179. return true;
  180. }
  181. if ($this->isOwner($subject, $user)) {
  182. return true;
  183. }
  184. return false;
  185. }
  186. /**
  187. * Check if User if Owner of Subject/Property
  188. * @param UserInterface $subject
  189. * @param UserInterface $user
  190. *
  191. * @return bool
  192. */
  193. private function isOwner(UserInterface $subject, UserInterface $user): bool
  194. {
  195. return $user->getId() === $subject->getId();
  196. }
  197. }