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.                     isset($controller[1])
  79.                     && $controller[1]
  80.                     && strpos(get_class($controller[0]), "api") === false
  81.                     && strpos(get_class($controller[0]), "Api") === false
  82.                     && strpos($controller[1], "api") === false
  83.                     && strpos($controller[1], "toolbarAction") === false
  84.                 )
  85.             {
  86.                 // Path en session
  87.                 $currentUrl $session->get('currentUrl');
  88.                 if
  89.                 (
  90.                     $token 
  91.                     && !$editorial 
  92.                     && strpos($currentUrl"editorial") === false
  93.                     && strpos($currentUrl"api") === false
  94.                     && strpos($currentUrl"admin") === false
  95.                     && strpos($currentUrl"import") === false
  96.                     && strpos($currentUrl"export") === false
  97.                     && strpos($currentUrl"download") === false
  98.                     && strpos($currentUrl"preview") === false
  99.                     && strpos($currentUrl"compte") === false
  100.                     && strpos($currentUrl"document") === false
  101.                     && strpos($currentUrl"iframe") === false
  102.                     && strpos($currentUrl"vol") === false
  103.                     && strpos($currentUrl"company") === false
  104.                     && strpos($currentUrl"dossiers/tiers") === false
  105.                     && strpos($currentUrl"dossiers/contact") === false
  106.                     && strpos($currentUrl"dolibarr=1") === false
  107.                 )
  108.                 {
  109.                     /*
  110.                     $event->setController(function() use ($currentUrl) {
  111.                         return new RedirectResponse($currentUrl);
  112.                     });
  113.                     */
  114.                 } else
  115.                 {
  116.                     $this->setUrlInSession($event);
  117.                 }
  118.                 if($event->isMasterRequest())
  119.                 {
  120.                     // Passer notifications en session
  121.                     $this->setUserNotificationInSession($event);
  122.                 
  123.                     // Passer page courante en base (pour "sur ma page") 
  124.                     $this->setUserCurrentPage($event);
  125.                 
  126.                     // Passer les users connectés sur la page en session (pour "sur ma page")
  127.                     $this->setCurrentPageUsersInSession($event);
  128.         
  129.                     if (!$session->get('destinations'))
  130.                     {
  131.                         // Destinations pour menet
  132.                         $this->setDestinations($event);
  133.                     }
  134.                 }
  135.             // API
  136.             } else
  137.             {
  138.                 /*
  139.                 if (strpos($controller[1], "periodes") !== false)
  140.                 {
  141.                     $time_pre = microtime(true);
  142.                     var_dump($time_pre);
  143.                 }
  144.                 */
  145.             }
  146.         } else if(
  147.             $controller
  148.             && $controller instanceof ErrorController
  149.             && !$controller instanceof ProfilerController
  150.         )
  151.         {
  152.             /*
  153.             if (strpos($controller[1], "periodes") !== false)
  154.             {
  155.                 $time_pre = microtime(true);
  156.                 var_dump($time_pre);
  157.             }
  158.             */
  159.         }
  160.         
  161.         return;
  162.     }
  163.     public function setDestinations($event) {
  164.         $request $event->getRequest();
  165.         $session $request->getSession();
  166.         if ($this->tokenStorage->getToken()) {
  167.             $user $this->tokenStorage->getToken()->getUser();
  168.             $destinationsList $this->userService->getUserDestinations($user);
  169.             
  170.         } else {
  171.             $destinationsList $this->em->getRepository(Destination::class)->findBy(['actif' => true]);
  172.             
  173.         }
  174.         $voyageDestinationsCategories $this->em->getRepository(VoyageCategorie::class)->findBy(['actif' => true]);
  175.         $dossierDestinationsCategories $this->em->getRepository(DossierType::class)->findBy(['actif' => true]);
  176.         $destinations = [];
  177.         foreach($voyageDestinationsCategories as $categorie) {
  178.             $destinations[$categorie->getId()]['voyages'] = $destinationsList;
  179.             $destinations[$categorie->getId()]['categorie'] = $categorie;  
  180.         }
  181.         $dossierDestinations = [];
  182.         foreach($dossierDestinationsCategories as $categorie) {
  183.             $dossierDestinations[$categorie->getId()]['voyages'] = $destinationsList;
  184.             $dossierDestinations[$categorie->getId()]['categorie'] = $categorie;  
  185.         }
  186.         
  187.         $request $event->getRequest();
  188.         $session $request->getSession();
  189.         $session->set('destinations'$destinations);
  190.         $session->set('dossierDestinations'$dossierDestinations);
  191.         $voyageDestinationsEtats $this->em->getRepository(VoyageEtat::class)->findBy(['actif' => true]);
  192.         $dossierDestinationsEtats $this->em->getRepository(DossierEtat::class)->findBy(['actif' => true]);
  193.         $destinationsEtats = [];
  194.         foreach($voyageDestinationsEtats as $etat) {
  195.             $destinationsEtats[$etat->getId()]['voyages'] = $destinationsList;
  196.             $destinationsEtats[$etat->getId()]['etat'] = $etat;  
  197.         }
  198.         $dossierEtats = [];
  199.         foreach($dossierDestinationsEtats as $etat) {
  200.             $dossierEtats[$etat->getId()]['voyages'] = $destinationsList;
  201.             $dossierEtats[$etat->getId()]['etat'] = $etat;  
  202.         }
  203.         
  204.         $session->set('etats'$destinationsEtats);
  205.         $session->set('dossierEtats'$dossierEtats);
  206.     }
  207.     
  208.     public function setUrlInSession($event) {
  209.         $request $event->getRequest();
  210.         $session $request->getSession();
  211.         $token $request->query->get('token');
  212.         $currentUrl str_replace("token=" $token"login=1"$request->getRequestUri());
  213.         $session->set('currentUrl'$currentUrl);
  214.     }
  215.     
  216.     public function handleDolibarrUser($token)
  217.     {
  218.         $hash base64_decode($token);
  219.      
  220.         $hash ltrim($hash"LM%");
  221.         $hash rtrim($hash"-VV$");
  222.         $userId filter_var($hashFILTER_SANITIZE_NUMBER_INT);
  223.    
  224.         if (is_numeric($userId)) {
  225.             $session = new Session();
  226.             $this->userService->checkDolibarrUser($userId);
  227.             $session->set('userId'$userId); 
  228.             $session->set('token'$token); 
  229.             $session->set('destinations'null);
  230.             $session->set('dossierDestinations'null);
  231.             $session->set('etats'null);
  232.             $session->set('dossierEtats'null);
  233.         } 
  234.     }
  235.     public function setUserNotificationInSession($event)
  236.     {
  237.         if ($this->tokenStorage->getToken()) {
  238.             $user $this->tokenStorage->getToken()->getUser();
  239.             $request $event->getRequest();
  240.             $session $request->getSession();
  241.             $notifications $this->notificationService->findUserNotificationsForPanel($user);
  242.             $session->set('userNotifications'$notifications);
  243.           
  244.             $notifications $this->notificationService->findUserViewedNotificationsForPanel($user);
  245.             $session->set('userViewedNotifications'$notifications);
  246.   
  247.         }
  248.     }
  249.     public function setUserCurrentPage($event)
  250.     {
  251.         if ($this->tokenStorage->getToken()) {
  252.             $user $this->tokenStorage->getToken()->getUser();
  253.            
  254.             $request $event->getRequest(); 
  255.             $session $request->getSession();
  256.            
  257.             $hisoriquePage $this->historiquePageService->updateCurrentPage($user$request);
  258.             $session->set('hisoriquePage'$hisoriquePage);
  259.         }
  260.     }
  261.     public function setCurrentPageUsersInSession($event)
  262.     {
  263.         if ($this->tokenStorage->getToken()) {
  264.             $user $this->tokenStorage->getToken()->getUser();
  265.             $request $event->getRequest();
  266.             $url $request->getPathInfo();
  267.             $session $request->getSession();
  268.          
  269.             $connectedUsers $this->historiquePageService->getCurrentPageConnectedUsers($user$url);
  270.             $session->set('connectedUsers'$connectedUsers);
  271.         }
  272.     }
  273.     
  274.     public static function getSubscribedEvents(){
  275.         return [
  276.             KernelEvents::REQUEST => [['onKernelRequest'1]],
  277.             KernelEvents::CONTROLLER => [['onKernelController'20]],
  278.         ];
  279.     }
  280. }
  281. ?>