src/Security/Voter/ImpersonatorAdminVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  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 ImpersonatorAdminVoter extends Voter
  9. {
  10.     private $security;
  11.     public function __construct(Security $security)
  12.     {
  13.         $this->security $security;
  14.     }
  15.     protected function supports($attribute$subject): bool
  16.     {
  17.         return $attribute === 'IS_IMPERSONATOR_ADMIN';
  18.     }
  19.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  20.     {
  21.         if (!$token instanceof SwitchUserToken) {
  22.             return false;
  23.         }
  24.         $impersonatorUser $token->getOriginalToken()->getUser();
  25.         if ($impersonatorUser instanceof User && in_array('ROLE_ADMIN'$impersonatorUser->getRoles())) {
  26.             return true;
  27.         }
  28.         return false;
  29.     }
  30. }