src/Application/Symfony/EventSubscriber/Kernel/IdleSubscriber.php line 73

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the MADIS - RGPD Management application.
  4.  *
  5.  * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
  6.  * @author Donovan Bourlard <donovan@awkan.fr>
  7.  *
  8.  * This program is free software: you can redistribute it and/or modify
  9.  * it under the terms of the GNU Affero General Public License as published by
  10.  * the Free Software Foundation, either version 3 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU Affero General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Affero General Public License
  19.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  20.  */
  21. declare(strict_types=1);
  22. namespace App\Application\Symfony\EventSubscriber\Kernel;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. use Symfony\Component\HttpKernel\Event\RequestEvent;
  25. use Symfony\Component\HttpKernel\KernelEvents;
  26. /**
  27.  * Class IdleSubscriber
  28.  * Check user idle time.
  29.  * If user is idle more than accepted time, invalidate his session.
  30.  */
  31. class IdleSubscriber implements EventSubscriberInterface
  32. {
  33.     /**
  34.      * @var int
  35.      */
  36.     private $expirationTime;
  37.     /**
  38.      * IdleSubscriber constructor.
  39.      *
  40.      * @param int $expirationTime Time in seconds to define idle
  41.      */
  42.     public function __construct(int $expirationTime)
  43.     {
  44.         $this->expirationTime $expirationTime;
  45.     }
  46.     public static function getSubscribedEvents()
  47.     {
  48.         return [
  49.             KernelEvents::REQUEST => [
  50.                 ['onKernelRequest'9],
  51.             ],
  52.         ];
  53.     }
  54.     /**
  55.      * OnKernelRequest check idle since last Request.
  56.      * If idle is over, then invalidate session.
  57.      */
  58.     public function onKernelRequest(RequestEvent $event): void
  59.     {
  60.         if (!$event->isMasterRequest()) {
  61.             return;
  62.         }
  63.         $request $event->getRequest();
  64.         $session $request->getSession();
  65.         $session->start();
  66.         $metaData       $session->getMetadataBag();
  67.         $timeDifference time() - $metaData->getLastUsed();
  68.         if ($timeDifference $this->expirationTime) {
  69.             $session->invalidate();
  70.         }
  71.     }
  72. }