src/Security/Voter/SwitchToCustomerVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Users\Child as Student;
  4. use App\Entity\Users\StudentsParents;
  5. use App\Entity\Users\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. class SwitchToCustomerVoter extends Voter
  14. {
  15.     private $security;
  16.     private $request;
  17.     private $entityManager;
  18.     public function __construct(Security $security,RequestStack $requestStack,EntityManagerInterface $entityManager)
  19.     {
  20.         $this->security = $security;
  21.         $this->request = $requestStack;
  22.         $this->entityManager = $entityManager;
  23.     }
  24.     /**
  25.      * @inheritDoc
  26.      */
  27.     protected function supports(string $attribute, $subject)
  28.     {
  29.         // TODO: Implement supports() method.
  30.         return in_array($attribute, ['ROLE_SWITCH_USER','ROLE_ADMIN']) && $subject instanceof UserInterface;
  31.     }
  32.     /**
  33.      * @inheritDoc
  34.      */
  35.     protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token)
  36.     {
  37.             $target = $this->request->getCurrentRequest()->get('_switch_user');
  38.             $code = $this->request->getCurrentRequest()->get('code');
  39.             if($code === "LBP"){
  40.                 return true;
  41.             }
  42.             $user   = $this->security->getToken()->getUser();
  43.             $targetEntity = $this->entityManager->getRepository(User::class)->findUserByUsernameOrEmail($target);
  44.             $val = -1;
  45.             if(in_array('ROLE_ADMIN',$user->getRoles())){
  46.                 return true;
  47.             }
  48.             if(/*in_array('ROLE_STUDENT',$user->getRoles()) ||*/ in_array('ROLE_PARENTS',$user->getRoles()) ){
  49.              /*   if(in_array('ROLE_STUDENT',$user->getRoles())){
  50.                     $val = $this->entityManager->getRepository(StudentsParents::class)->toSwitchUserStudent($user,$targetEntity);
  51.                 }*/
  52.                 if(in_array('ROLE_PARENTS',$user->getRoles())){
  53.                     if($user->getId()==$targetEntity->getId()){
  54.                         return true;
  55.                     }
  56.                     $val = $this->entityManager->getRepository(StudentsParents::class)->toSwitchUserParents($user,$targetEntity);
  57.                 }
  58.             }
  59.             if($this->request->getSession()->has('Application')){
  60.                 $this->request->getSession()->remove('Application');
  61.                 $this->request->getSession()->save();
  62.             }
  63.             return 0 < $val;
  64.         // TODO: Implement voteOnAttribute() method.
  65.     }
  66. }