src/Security/Voter/SwitchToCustomerVoter.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\Security;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. /**
  8.  * Permet de gĂ©rer les ROLES qui peuvent switcher (impersonification)
  9.  *
  10.  * Class SwitchToCustomerVoter
  11.  * @package App\Security\Voter
  12.  */
  13. class SwitchToCustomerVoter extends Voter
  14. {
  15.     private $security;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.     }
  20.     protected function supports($attribute$subject)
  21.     {
  22.         return in_array($attribute, ['CAN_SWITCH_USER'])
  23.             && $subject instanceof UserInterface;
  24.     }
  25.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  26.     {
  27.         $user $token->getUser();
  28.         if (!$user instanceof UserInterface || !$subject instanceof UserInterface) {
  29.             return false;
  30.         }
  31.         if ($this->security->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
  32.             return true;
  33.         }
  34.         return false;
  35.     }
  36. }