src/Security/Voter/ExactRoleVoter.php line 10

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class ExactRoleVoter extends Voter
  7. {
  8.     protected function supports(string $attribute, mixed $subject): bool
  9.     {
  10.         return str_starts_with($attribute, 'ROLE_EXACT_');
  11.     }
  12.     protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
  13.     {
  14.         $role = 'ROLE_' . substr($attribute, \strlen('ROLE_EXACT_'));
  15.         $roles = $token->getRoleNames();
  16.         return \in_array($role, $roles, true);
  17.     }
  18. }