src/EventSubscriber/KernelSubscriber.php line 43

Open in your IDE?
  1. <?PHP
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  4. use Symfony\Component\HttpKernel\KernelEvents;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Security\Http\SecurityEvents;
  7. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  8. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\HttpFoundation\Session\Session;
  11. use App\Entity\Destination;
  12. use App\Entity\DossierEtat;
  13. use App\Entity\VoyageCategorie;
  14. use App\Entity\DossierType;
  15. use App\Entity\VoyageEtat;
  16. use App\Service\UserService;
  17. use App\Service\NotificationService;
  18. use App\Service\HistoriquePageService;
  19. use Doctrine\ORM\EntityManagerInterface;
  20. use Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController;
  21. use Symfony\Component\HttpKernel\Controller\ErrorController;
  22. use Symfony\Component\HttpFoundation\RedirectResponse;
  23. class KernelSubscriber implements EventSubscriberInterface{
  24.     
  25.     private $notificationService;
  26.     private $historiquePageService;
  27.     private $userService;
  28.     private $tokenStorage;
  29.     private $em;
  30.     public function __construct(NotificationService $notificationServiceHistoriquePageService $historiquePageServiceUserService $userServiceTokenStorageInterface $tokenStorageEntityManagerInterface $em){
  31.         $this->notificationService $notificationService;
  32.         $this->historiquePageService $historiquePageService;
  33.         $this->userService $userService;
  34.         $this->tokenStorage $tokenStorage;
  35.         $this->em $em;
  36.     }
  37.     public function onKernelRequest(GetResponseEvent $event)
  38.     {
  39.         return;
  40.     }
  41.     public function onKernelController(FilterControllerEvent $event)
  42.     {
  43.         // Params
  44.         $controller $event->getController();
  45.         $request $event->getRequest();
  46.         $session $request->getSession();
  47.         $token $request->query->get('token');
  48.         $editorial $request->query->get('editorial');
  49.         $dolibarr $request->query->get('dolibarr');
  50.    
  51.         // Récupération User de Dolibarr
  52.         if ($token && $this->tokenStorage->getToken() && $this->tokenStorage->getToken()->getUser() == "anon."
  53.         {
  54.             $session->set('token'$token); 
  55.             $this->handleDolibarrUser($token);
  56.             $redirectUrl str_replace("token=" $token"login=1"$request->getRequestUri());
  57.             $event->setController(function() use ($redirectUrl) {
  58.                 return new RedirectResponse($redirectUrl);
  59.             });
  60.         } else if($token)
  61.         {
  62.             $session->set('token'$token); 
  63.             $session->set('destinations'null);
  64.             $session->set('dossierDestinations'null);
  65.             $session->set('etats'null);
  66.             $session->set('dossierEtats'null);
  67.         }
  68.         // Si Pas ErrorController
  69.         if 
  70.             (
  71.                 $controller
  72.                 && !$controller instanceof ErrorController
  73.                 && !$controller instanceof ProfilerController
  74.             )
  75.         {
  76.             // Interfaces
  77.             if (
  78.             is_array($controller) &&
  79.                     isset($controller[1])
  80.                     && $controller[1]
  81.                     && strpos(get_class($controller[0]), "api") === false
  82.                     && strpos(get_class($controller[0]), "Api") === false
  83.                     && strpos($controller[1], "api") === false
  84.                     && strpos($controller[1], "toolbarAction") === false
  85.                 )
  86.             {
  87.                 // Path en session
  88.                 $currentUrl $session->get('currentUrl');
  89.                 if
  90.                 (
  91.                     $token 
  92.                     && !$editorial 
  93.                     && strpos($currentUrl"editorial") === false
  94.                     && strpos($currentUrl"api") === false
  95.                     && strpos($currentUrl"admin") === false
  96.                     && strpos($currentUrl"import") === false
  97.                     && strpos($currentUrl"export") === false
  98.                     && strpos($currentUrl"download") === false
  99.                     && strpos($currentUrl"preview") === false
  100.                     && strpos($currentUrl"compte") === false
  101.                     && strpos($currentUrl"document") === false
  102.                     && strpos($currentUrl"iframe") === false
  103.                     && strpos($currentUrl"vol") === false
  104.                     && strpos($currentUrl"company") === false
  105.                     && strpos($currentUrl"dossiers/tiers") === false
  106.                     && strpos($currentUrl"dossiers/contact") === false
  107.                     && strpos($currentUrl"dolibarr=1") === false
  108.                 )
  109.                 {
  110.                     /*
  111.                     $event->setController(function() use ($currentUrl) {
  112.                         return new RedirectResponse($currentUrl);
  113.                     });
  114.                     */
  115.                 } else
  116.                 {
  117.                     $this->setUrlInSession($event);
  118.                 }
  119.                 if($event->isMasterRequest())
  120.                 {
  121.                     // Passer notifications en session
  122.                     $this->setUserNotificationInSession($event);
  123.                 
  124.                     // Passer page courante en base (pour "sur ma page") 
  125.                     $this->setUserCurrentPage($event);
  126.                 
  127.                     // Passer les users connectés sur la page en session (pour "sur ma page")
  128.                     $this->setCurrentPageUsersInSession($event);
  129.         
  130.                     if (!$session->get('destinations'))
  131.                     {
  132.                         // Destinations pour menet
  133.                         $this->setDestinations($event);
  134.                     }
  135.                 }
  136.             // API
  137.             } else
  138.             {
  139.                 /*
  140.                 if (strpos($controller[1], "periodes") !== false)
  141.                 {
  142.                     $time_pre = microtime(true);
  143.                     var_dump($time_pre);
  144.                 }
  145.                 */
  146.             }
  147.         } else if(
  148.             $controller
  149.             && $controller instanceof ErrorController
  150.             && !$controller instanceof ProfilerController
  151.         )
  152.         {
  153.             /*
  154.             if (strpos($controller[1], "periodes") !== false)
  155.             {
  156.                 $time_pre = microtime(true);
  157.                 var_dump($time_pre);
  158.             }
  159.             */
  160.         }
  161.         
  162.         return;
  163.     }
  164.     public function setDestinations($event) {
  165.         $request $event->getRequest();
  166.         $session $request->getSession();
  167.         if ($this->tokenStorage->getToken()) {
  168.             $user $this->tokenStorage->getToken()->getUser();
  169.             $destinationsList $this->userService->getUserDestinations($user);
  170.             
  171.         } else {
  172.             $destinationsList $this->em->getRepository(Destination::class)->findBy(['actif' => true]);
  173.             
  174.         }
  175.         $voyageDestinationsCategories $this->em->getRepository(VoyageCategorie::class)->findBy(['actif' => true]);
  176.         $dossierDestinationsCategories $this->em->getRepository(DossierType::class)->findBy(['actif' => true]);
  177.         $destinations = [];
  178.         foreach($voyageDestinationsCategories as $categorie) {
  179.             $destinations[$categorie->getId()]['voyages'] = $destinationsList;
  180.             $destinations[$categorie->getId()]['categorie'] = $categorie;  
  181.         }
  182.         $dossierDestinations = [];
  183.         foreach($dossierDestinationsCategories as $categorie) {
  184.             $dossierDestinations[$categorie->getId()]['voyages'] = $destinationsList;
  185.             $dossierDestinations[$categorie->getId()]['categorie'] = $categorie;  
  186.         }
  187.         
  188.         $request $event->getRequest();
  189.         $session $request->getSession();
  190.         $session->set('destinations'$destinations);
  191.         $session->set('dossierDestinations'$dossierDestinations);
  192.         $voyageDestinationsEtats $this->em->getRepository(VoyageEtat::class)->findBy(['actif' => true]);
  193.         $dossierDestinationsEtats $this->em->getRepository(DossierEtat::class)->findBy(['actif' => true]);
  194.         $destinationsEtats = [];
  195.         foreach($voyageDestinationsEtats as $etat) {
  196.             $destinationsEtats[$etat->getId()]['voyages'] = $destinationsList;
  197.             $destinationsEtats[$etat->getId()]['etat'] = $etat;  
  198.         }
  199.         $dossierEtats = [];
  200.         foreach($dossierDestinationsEtats as $etat) {
  201.             $dossierEtats[$etat->getId()]['voyages'] = $destinationsList;
  202.             $dossierEtats[$etat->getId()]['etat'] = $etat;  
  203.         }
  204.         
  205.         $session->set('etats'$destinationsEtats);
  206.         $session->set('dossierEtats'$dossierEtats);
  207.     }
  208.     
  209.     public function setUrlInSession($event) {
  210.         $request $event->getRequest();
  211.         $session $request->getSession();
  212.         $token $request->query->get('token');
  213.         $currentUrl str_replace("token=" $token"login=1"$request->getRequestUri());
  214.         $session->set('currentUrl'$currentUrl);
  215.     }
  216.     
  217.     public function handleDolibarrUser($token)
  218.     {
  219.         $hash base64_decode($token);
  220.      
  221.         $hash ltrim($hash"LM%");
  222.         $hash rtrim($hash"-VV$");
  223.         $userId filter_var($hashFILTER_SANITIZE_NUMBER_INT);
  224.    
  225.         if (is_numeric($userId)) {
  226.             $session = new Session();
  227.             $this->userService->checkDolibarrUser($userId);
  228.             $session->set('userId'$userId); 
  229.             $session->set('token'$token); 
  230.             $session->set('destinations'null);
  231.             $session->set('dossierDestinations'null);
  232.             $session->set('etats'null);
  233.             $session->set('dossierEtats'null);
  234.         } 
  235.     }
  236.     public function setUserNotificationInSession($event)
  237.     {
  238.         if ($this->tokenStorage->getToken()) {
  239.             $user $this->tokenStorage->getToken()->getUser();
  240.             $request $event->getRequest();
  241.             $session $request->getSession();
  242.             $notifications $this->notificationService->findUserNotificationsForPanel($user);
  243.             $session->set('userNotifications'$notifications);
  244.           
  245.             $notifications $this->notificationService->findUserViewedNotificationsForPanel($user);
  246.             $session->set('userViewedNotifications'$notifications);
  247.   
  248.         }
  249.     }
  250.     public function setUserCurrentPage($event)
  251.     {
  252.         if ($this->tokenStorage->getToken()) {
  253.             $user $this->tokenStorage->getToken()->getUser();
  254.            
  255.             $request $event->getRequest(); 
  256.             $session $request->getSession();
  257.            
  258.             $hisoriquePage $this->historiquePageService->updateCurrentPage($user$request);
  259.             $session->set('hisoriquePage'$hisoriquePage);
  260.         }
  261.     }
  262.     public function setCurrentPageUsersInSession($event)
  263.     {
  264.         if ($this->tokenStorage->getToken()) {
  265.             $user $this->tokenStorage->getToken()->getUser();
  266.             $request $event->getRequest();
  267.             $url $request->getPathInfo();
  268.             $session $request->getSession();
  269.          
  270.             $connectedUsers $this->historiquePageService->getCurrentPageConnectedUsers($user$url);
  271.             $session->set('connectedUsers'$connectedUsers);
  272.         }
  273.     }
  274.     
  275.     public static function getSubscribedEvents(){
  276.         return [
  277.             KernelEvents::REQUEST => [['onKernelRequest'1]],
  278.             KernelEvents::CONTROLLER => [['onKernelController'20]],
  279.         ];
  280.     }
  281. }
  282. ?>