src/Listener/Security/IdlePolicyListener.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Listener\Security;
  3. use App\Entity\User;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\HttpFoundation\Session\Session;
  8. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  9. use Symfony\Component\HttpKernel\Event\RequestEvent;
  10. use Symfony\Component\HttpKernel\HttpKernelInterface;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Contracts\EventDispatcher\Event;
  16. class IdlePolicyListener implements EventSubscriberInterfaceWarmableInterface
  17. {
  18.     /**
  19.      * @var TokenStorageInterface
  20.      */
  21.     private $tokenStorage;
  22.     /**
  23.      * @var RequestStack
  24.      */
  25.     private $requestStack;
  26.     /**
  27.      * @var RouterInterface
  28.      */
  29.     private $router;
  30.     /**
  31.      * @var int
  32.      */
  33.     private $idlePolicy;
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             KernelEvents::REQUEST => [
  38.                 ['onRequest'0],
  39.             ],
  40.         ];
  41.     }
  42.     public function __construct(TokenStorageInterface $tokenStorageRequestStack $requestStackRouterInterface $routerint $idlePolicy)
  43.     {
  44.         $this->tokenStorage $tokenStorage;
  45.         $this->requestStack $requestStack;
  46.         $this->router $router;
  47.         $this->idlePolicy $idlePolicy;
  48.     }
  49.     public function onRequest(RequestEvent $event)
  50.     {
  51.         if (HttpKernelInterface::MASTER_REQUEST != $event->getRequestType()) {
  52.             return;
  53.         }
  54.         if (!$this->tokenStorage->getToken() instanceof TokenInterface) {
  55.             return;
  56.         }
  57.         if (!$this->tokenStorage->getToken()->getUser() instanceof User) {
  58.             return;
  59.         }
  60.         if ($this->idlePolicy 0) {
  61.             $session $this->requestStack->getSession();
  62.             $session->start();
  63.             $lapse time() - $session->getMetadataBag()->getLastUsed();
  64.             if ($lapse $this->idlePolicy) {
  65.                 $this->tokenStorage->setToken(null);
  66.                 if ($session instanceof Session) {
  67.                     $session->getFlashBag()->set('warning''You have been signed out due to inactivity.');
  68.                     $event->setResponse(new RedirectResponse($this->router->generate('security.sign-out')));
  69.                     $dispatcher = new Event();
  70.                     $dispatcher->stopPropagation();
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     public function warmUp($cacheDir)
  76.     {
  77.         return [TokenStorageInterface::class, RequestStack::class, RouterInterface::class];
  78.     }
  79. }