src/EventListener/SecurityListener.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Repository\DatedocumentRepository;
  4. use App\Repository\UserRepository;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class SecurityListener implements EventSubscriberInterface
  10. {
  11.     private $session$dateDocRepo$userRepo;
  12.     public function __construct(SessionInterface $sessionDatedocumentRepository $dateDocRepoUserRepository $userRepo)
  13.     {
  14.         $this->session     $session;
  15.         $this->dateDocRepo $dateDocRepo;
  16.         $this->userRepo    $userRepo;
  17.     }
  18.     public function onKernelRequest(RequestEvent $event)
  19.     {
  20.         $serializedToken $event->getRequest()->getSession()->get('_security_main');
  21.         $token unserialize($serializedToken);
  22.         if ($token)
  23.         {
  24.             $user         $token->getUser();
  25.             $userFound    $this->userRepo->findOneById($user->getId()); 
  26.             $dateAccepted null;
  27.             $plateform    1;
  28.             switch ($userFound->getRoles()[0]) {
  29.                 case 'ROLE_PHARMACIE':
  30.                     $plateform 3;
  31.                     break;
  32.                 case 'ROLE_GROSSISTE':
  33.                     $plateform 4;
  34.                     break;
  35.                 case 'ROLE_PRESCRIPTEUR':
  36.                     $plateform 5;
  37.                     break;
  38.                 default:
  39.                     $plateform 1
  40.                     break;
  41.             }
  42.             $dateModifCGU $this->dateDocRepo->findDateModificationCGU($plateform);
  43.             //dd($plateform);
  44.             if (!is_null($dateModifCGU)) {
  45.                 $dateModifCGU $dateModifCGU[0]['datemodification']->format('d-m-Y');
  46.             }
  47.             
  48.             
  49.             if (!is_null($userFound->isCguaccepted())) {
  50.                 $dateAccepted $userFound->isCguaccepted()->format('d-m-Y');
  51.             }
  52.             if($dateAccepted $dateModifCGU) {
  53.                 $this->session->set('cgu_session'true);
  54.             }else{
  55.                 $this->session->set('cgu_session'false);
  56.             } 
  57.         }
  58.     }
  59.     public static function getSubscribedEvents()
  60.     {
  61.         return [
  62.             KernelEvents::REQUEST => 'onKernelRequest',
  63.         ];
  64.     }
  65. }