src/Security/IsEmployeeVoter.php line 12

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security;
  4. use App\Entity\System\Employee;
  5. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class IsEmployeeVoter extends Voter
  9. {
  10.     public const IS_EMPLOYEE 'IS_EMPLOYEE';
  11.     protected function supports($attribute$subject): bool
  12.     {
  13.         return $attribute === self::IS_EMPLOYEE;
  14.     }
  15.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  16.     {
  17.         $user $token->getUser();
  18.         if ($token instanceof SwitchUserToken) {
  19.             $user $token->getOriginalToken()->getUser();
  20.         }
  21.         if (!$user instanceof Employee) {
  22.             return false;
  23.         }
  24.         return true;
  25.     }
  26. }