src/EventSubscriber/SessionIdleSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\User;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  7. use Symfony\Component\HttpKernel\HttpKernelInterface;
  8. use Symfony\Component\Routing\RouterInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\HttpKernel\Event\RequestEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  15. class SessionIdleSubscriber implements EventSubscriberInterface
  16. {
  17.     protected $session;
  18.     protected $tokenStorage;
  19.     protected $router;
  20.     protected $sessionIdleTime;
  21.     protected $flashBag;
  22.     public function __construct(
  23.         SessionInterface $session,
  24.         TokenStorageInterface $tokenStorage,
  25.         RouterInterface $router,
  26.         ParameterBagInterface $params,
  27.         FlashBagInterface $flashBag
  28.     ) {
  29.         $this->session         $session;
  30.         $this->tokenStorage    $tokenStorage;
  31.         $this->router          $router;
  32.         $this->sessionIdleTime $params->get('session_idle_time') ?? 0//$sessionIdleTime equal to session_idle_time if is defined in parameters section, zero else
  33.         $this->flashBag        $flashBag;
  34.     }
  35.     public function onKernelRequest(RequestEvent $event)
  36.     {
  37.        
  38.         if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
  39.             return;
  40.         }
  41.         if ($this->sessionIdleTime 0) {
  42.             // Get the difference between now and the time of last action make it by user
  43.             $lapse time() - $this->session->getMetadataBag()->getLastUsed(); 
  44.             if (null !== $this->tokenStorage->getToken() && "anon." !== $this->tokenStorage->getToken()->getUser()) {// if user is logged in
  45.                 $roles $this->tokenStorage->getToken()->getUser()->getRoles();
  46.                 if ($lapse $this->sessionIdleTime && true === in_array("ROLE_APP",$roles)) {
  47.                     $this->tokenStorage->setToken(null);
  48.                     //$this->flashBag->add('danger', "session destroy");
  49.                     if($event->getRequest()->isXmlHttpRequest()){
  50.                         throw new AccessDeniedHttpException('Session Expired.');// return status 403 for ajax call 
  51.                     } else {
  52.                         $event->setResponse(new RedirectResponse($this->router->generate('app_page_index')));
  53.                     }
  54.                 }
  55.             }
  56.             
  57.         }
  58.     }
  59.       /**
  60.      * @inheritDoc
  61.      */
  62.     public static function getSubscribedEvents()
  63.     {
  64.         return [
  65.             // must be registered before (i.e. with a higher priority than) the default Locale listener
  66.             KernelEvents::REQUEST => [['onKernelRequest'1]]
  67.         ];
  68.     }
  69. }