vendor/crea/security-bundle/src/Voter/RightVoter.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Crea\SecurityBundle\Voter;
  4. use Crea\SecurityBundle\Entity\Right;
  5. use Crea\SecurityBundle\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\User\UserInterface;
  9. /**
  10. * @extends Voter<string, Right>
  11. */
  12. class RightVoter extends Voter
  13. {
  14. public const RIGHT_LIST = 'SECURITY_RIGHT_LIST';
  15. protected function supports($attribute, $subject): bool
  16. {
  17. return self::RIGHT_LIST == $attribute && (null === $subject || $subject instanceof Right);
  18. }
  19. protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
  20. {
  21. /** @var User $loggedUser */
  22. $loggedUser = $token->getUser();
  23. if (!$loggedUser instanceof UserInterface) {
  24. return false;
  25. }
  26. if (self::RIGHT_LIST == $attribute) {
  27. return $this->voteOnList($loggedUser);
  28. }
  29. return false;
  30. }
  31. private function voteOnList(UserInterface $loggedUser): bool
  32. {
  33. if (in_array(self::RIGHT_LIST, $loggedUser->getRoles())) {
  34. return true;
  35. }
  36. return false;
  37. }
  38. }