src/Security/Voter/BoAccessVoter.php line 9

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. class BoAccessVoter extends Voter
  7. {
  8.     public const BO_ACCESS 'BO_ACCESS';
  9.     private const ALLOWED_ROLES = [
  10.         'ROLE_ADMIN',
  11.         'ROLE_SUPER_ADMIN',
  12.         'ROLE_DIRECTEUR_ORGANISATION',
  13.         'ROLE_DIRECTEUR_DOMAINES',
  14.         'ROLE_DIRECTEUR_SOUS_DOMAINES',
  15.         'ROLE_DIRECTEUR_RUBRIQUE',
  16.     ];
  17.     public function __construct()
  18.     {
  19.     }
  20.     protected function supports(string $attributemixed $subject): bool
  21.     {
  22.         return $attribute === self::BO_ACCESS;
  23.     }
  24.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  25.     {
  26.         $user $token->getUser();
  27.         if (!$user instanceof User) {
  28.             return false;
  29.         }
  30.         foreach ($user->getRoles() as $roleCode) {
  31.             if (in_array($roleCodeself::ALLOWED_ROLEStrue)) {
  32.                 return true;
  33.             }
  34.         }
  35.         return false;
  36.     }
  37. }