src/Security/Voter/ClientVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Client\Client;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. class ClientVoter extends Voter
  9. {
  10. private const VIEW = 'view';
  11. private const EDIT = 'edit';
  12. private const SELECT = 'select';
  13. private const DELETE = 'delete';
  14. /**
  15. * @var Security
  16. */
  17. private Security $security;
  18. /**
  19. * ClientVoter constructor.
  20. * @param Security $security
  21. */
  22. public function __construct(Security $security)
  23. {
  24. $this->security = $security;
  25. }
  26. /**
  27. * @inheritDoc
  28. */
  29. protected function supports(string $attribute, $subject): bool
  30. {
  31. // if the attribute isn't one we support, return false
  32. if (!in_array($attribute, [self::VIEW, self::EDIT, self::DELETE, self::SELECT], true)) {
  33. return false;
  34. }
  35. // only vote on `Client` objects
  36. if ($subject instanceof Client === false) {
  37. return false;
  38. }
  39. return true;
  40. }
  41. /**
  42. * @inheritDoc
  43. */
  44. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  45. {
  46. $loggedUser = $token->getUser();
  47. if (!$loggedUser instanceof User) {
  48. // the user must be logged in; if not, deny access
  49. return false;
  50. }
  51. switch ($attribute) {
  52. case self::VIEW:
  53. return $this->canSee($subject, $loggedUser);
  54. case self::EDIT:
  55. case self::DELETE:
  56. return $this->canEdit($subject, $loggedUser);
  57. case self::SELECT:
  58. return $this->canSelect($subject, $loggedUser);
  59. }
  60. throw new \LogicException('This code should not be reached!');
  61. }
  62. /**
  63. * @param Client $client
  64. * @param User $loggedUser
  65. * @return bool
  66. */
  67. private function canSee(Client $client, User $loggedUser): bool
  68. {
  69. if ($this->security->isGranted('ROLE_ADMIN')) {
  70. return true;
  71. }
  72. if ($this->security->isGranted('ROLE_CAN_SEE_ALL_CLIENTS')) {
  73. return true;
  74. }
  75. if ($this->security->isGranted('ROLE_ADMIN_MANAGER')) {
  76. if ($loggedUser->getClient()->getId() === $client->getId()) {
  77. return true;
  78. }
  79. if ($loggedUser->getClientGroup() !== null && $client->getClientGroup() !== null) {
  80. if ($loggedUser->getClientGroup() === $client->getClientGroup()) {
  81. // client is from allowed group of clients of logged user
  82. return true;
  83. }
  84. }
  85. }
  86. if ($this->security->isGranted('ROLE_CAN_SEE_CLIENT_DETAIL') && $loggedUser->getClient()->getId() === $client->getId()) {
  87. return true;
  88. }
  89. return false;
  90. }
  91. /**
  92. * @param Client $client
  93. * @param User $loggedUser
  94. * @return bool
  95. */
  96. private function canEdit(Client $client, User $loggedUser): bool
  97. {
  98. if ($this->canSee($client, $loggedUser) && $this->security->isGranted('ROLE_CAN_EDIT_CLIENT')) {
  99. return true;
  100. }
  101. return false;
  102. }
  103. /**
  104. * @param Client $client
  105. * @param User $loggedUser
  106. * @return bool
  107. */
  108. private function canSelect(Client $client, User $loggedUser): bool
  109. {
  110. if (!$this->security->isGranted('ROLE_CAN_ASSIGN_CLIENT')
  111. || !$this->canSee($client, $loggedUser)
  112. ) {
  113. return false;
  114. }
  115. return true;
  116. }
  117. }