src/Controller/Security/SessionIdleHandler.php line 31

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Security;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  5. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  6. use Symfony\Component\HttpKernel\HttpKernelInterface;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. class SessionIdleHandler
  10. {
  11.     protected $session;
  12.     protected $tokenStorage;
  13.     protected $router;
  14.     protected $maxIdleTime;
  15.     public function __construct(
  16.         SessionInterface $session,
  17.         TokenStorageInterface $tokenStorage,
  18.         RouterInterface $router,
  19.         $maxIdleTime 0
  20.     ) {
  21.         $this->session $session;
  22.         $this->tokenStorage $tokenStorage;
  23.         $this->router $router;
  24.         $this->maxIdleTime $maxIdleTime;
  25.     }
  26.     public function onKernelRequest(GetResponseEvent $event)
  27.     {
  28.         if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
  29.             return;
  30.         }
  31.         if ($this->maxIdleTime 0) {
  32.             $this->session->start();
  33.             $lapse time() - $this->session->getMetadataBag()->getLastUsed();
  34.             if ($lapse $this->maxIdleTime && null !== $this->tokenStorage->getToken()) {
  35.                 $this->tokenStorage->setToken(null);
  36.                 $this->session->getFlashBag()->set('info'"Vous avez été déconnecté pour cause d'inactivité.");
  37.                 $event->setResponse(new RedirectResponse($this->router->generate('app_login')));
  38.             }
  39.         }
  40.     }
  41. }