src/Security/Voter/ImpersonateVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Model\DataObject\Customer;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Security;
  7. class ImpersonateVoter extends Voter
  8. {
  9.     public function __construct(
  10.         private Security $security,
  11.     ) {
  12.     }
  13.     protected function supports($attribute, $subject)
  14.     {
  15.         return $attribute === 'CAN_SWITCH_USER';
  16.     }
  17.     protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
  18.     {
  19.         if ($this->security->isGranted('ROLE_REPRESENTATIVE')) {
  20.             return true;
  21.         }
  22.         /** @var Customer $customer */
  23.         $customer = $token->getUser();
  24.         $linkedCustomersIds = $customer->getLinkedPapyrusIds();
  25.         if( !$linkedCustomersIds ) {
  26.             return false;
  27.         }
  28.         $linkedCustomersIds = explode(';', $linkedCustomersIds);
  29.         $subjectPapyrusId = $subject->getPapyrusId();
  30.         return in_array($subjectPapyrusId, $linkedCustomersIds);
  31.     }
  32. }