src/Biceps/DocumentBundle/EventListener/SessionLockListener.php line 40

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (c) 2022 Biceps
  4.  */
  5. namespace Biceps\DocumentBundle\EventListener;
  6. use Biceps\DocumentBundle\Controller\ListController;
  7. use Biceps\DocumentBundle\Controller\SecurityController;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  11. use Symfony\Component\Routing\RouterInterface;
  12. class SessionLockListener
  13. {
  14.     /**
  15.      * @var RouterInterface
  16.      */
  17.     protected RouterInterface $router;
  18.     /**
  19.      * @var int
  20.      */
  21.     protected int $time;
  22.     /**
  23.      * ControllerListener constructor.
  24.      * @param RouterInterface $router
  25.      */
  26.     public function __construct(RouterInterface $router)
  27.     {
  28.         $this->router $router;
  29.         $this->time time();
  30.     }
  31.     /**
  32.      * @param ControllerEvent $event
  33.      */
  34.     public function onKernelController(ControllerEvent $event): void
  35.     {
  36.         $controller $event->getController();
  37.         if (!is_array($controller)) {
  38.             return;
  39.         }
  40.         $route $event->getRequest()->get('_route');
  41.         $session $event->getRequest()->getSession();
  42.         $lock $session->get('lock'false);
  43.         if (
  44.             $event->isMainRequest() &&
  45.             !$this->isBlacklistedController($controller) &&
  46.             !$this->isBlacklistedRoute($route) &&
  47.             ($this->sessionShouldBeLocked($session) || $lock)
  48.         ) {
  49.             if (!$lock) {
  50.                 $session->set('lock'$route);
  51.             }
  52.             $redirectUrl $this->router->generate('lock');
  53.             $event->setController(function () use ($redirectUrl) {
  54.                 return new RedirectResponse($redirectUrl);
  55.             });
  56.         }
  57.         $session->set('lastActivity'$this->time);
  58.     }
  59.     /**
  60.      * @param SessionInterface $session
  61.      *
  62.      * @return bool
  63.      */
  64.     protected function sessionShouldBeLocked(SessionInterface $session) : bool
  65.     {
  66.         return $this->time $session->get('lastActivity'0) > 1200;
  67.     }
  68.     /**
  69.      * @param array $controller
  70.      *
  71.      * @return bool
  72.      */
  73.     protected function isBlacklistedController(array $controller) : bool
  74.     {
  75.         return $controller[0] instanceof ListController ||
  76.         $controller[0] instanceof SecurityController;
  77.     }
  78.     /**
  79.      * @param $route
  80.      *
  81.      * @return bool
  82.      */
  83.     protected function isBlacklistedRoute($route) : bool
  84.     {
  85.         if (preg_match('#^(_.*|document-preview|document-download)$#'$route)) {
  86.             return true;
  87.         }
  88.         return false;
  89.     }
  90. }