src/Package/Admin/Tools/EventSubscriber/LogoutInactiveAdminSubscriber/LogoutInactiveAdminSubscriber.php line 91

Open in your IDE?
  1. <?php
  2. namespace App\Package\Admin\Tools\EventSubscriber\LogoutInactiveAdminSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface,
  4.     Symfony\Component\HttpFoundation\RedirectResponse,
  5.     Symfony\Component\HttpKernel\KernelEvents,
  6.     Symfony\Component\HttpKernel\Event\ControllerEvent,
  7.     Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. use App\Package\Admin\Main\EntityInterface\AdminInterface;
  9. use App\Package\Toolkit\ApplicationMode\ApplicationMode,
  10.     App\Package\Toolkit\RouteLocalizer\RouteLocalizer;
  11. /**
  12.  * LogoutInactiveAdminSubscriber
  13.  *
  14.  * Redirects user to logout if inactive
  15.  *
  16.  * @todo       include handling xmlHttpRequest and return response to reload front page
  17.  *             (currently xmlHttpRequests are ignored)
  18.  *
  19.  * @author     Daniel Balowski <d.balowski@openform.pl> (_creator)
  20.  * @copyright  Openform
  21.  * @since      03.2019
  22.  */
  23. class LogoutInactiveAdminSubscriber implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var ApplicationMode
  27.      */
  28.     protected $applicationMode;
  29.     /**
  30.      * @var AdminInterface
  31.      */
  32.     protected $admin;
  33.     /**
  34.      * @var RouteLocalizer
  35.      */
  36.     protected $routeLocalizer;
  37.     /**
  38.      * @var integer
  39.      */
  40.     protected $adminLoginSessionMinutes;
  41.     /**
  42.      * @param ApplicationMode        $applicationMode
  43.      * @param TokenStorageInterface  $tokenStorage
  44.      * @param RouteLocalizer         $routeLocalizer
  45.      * @param integer                $adminLoginSessionMinutes
  46.      */
  47.     public function __construct(
  48.         ApplicationMode        $applicationMode,
  49.         TokenStorageInterface  $tokenStorage,
  50.         RouteLocalizer         $routeLocalizer,
  51.         int                    $adminLoginSessionMinutes
  52.     ) {
  53.         $this->applicationMode $applicationMode;
  54.         $this->admin =
  55.             $tokenStorage->getToken() ?
  56.             $tokenStorage->getToken()->getUser() :
  57.             null;
  58.         $this->routeLocalizer $routeLocalizer;
  59.         $this->adminLoginSessionMinutes $adminLoginSessionMinutes;
  60.     }
  61.     /**
  62.      * @return array
  63.      */
  64.     public static function getSubscribedEvents() : array
  65.     {
  66.         return [
  67.             KernelEvents::CONTROLLER => [
  68.                 [ 'logoutInactiveAdmin'384 ],
  69.             ]
  70.         ];
  71.     }
  72.     /**
  73.      * Redirects user to logout if inactive
  74.      *
  75.      * @param ControllerEvent $event
  76.      *
  77.      * @return void
  78.      */
  79.     public function logoutInactiveAdmin(ControllerEvent $event) : void
  80.     {
  81.     if ($this->applicationMode->getCurrentMode() !== 'admin') {
  82.             return;
  83.         }
  84.         if ($event->getRequest()->isXmlHttpRequest()) {
  85.             return;
  86.         }
  87.         if (
  88.             ! $this->admin ||
  89.             gettype($this->admin) == 'string' ||
  90.             ! $this->admin->getLastSeen()
  91.         ) {
  92.             return;
  93.         }
  94.         $lastSeen =
  95.             (clone $this->admin->getLastSeen())
  96.             ->modify('+' $this->adminLoginSessionMinutes 'mins');
  97.         $now = new \DateTime();
  98.         $timeLeft strtotime$lastSeen->format('Y-m-d H:i:s') ) - strtotime$now->format('Y-m-d H:i:s') );
  99.         if ($timeLeft 0) {
  100.             return;
  101.         }
  102.         $logoutRoute $this->routeLocalizer->generate('admin_logout', [], $event->getRequest()->getLocale());
  103.         $event->setController(function() use ($logoutRoute) {
  104.             return new RedirectResponse$logoutRoute );
  105.         });
  106.         $event->stopPropagation();
  107.         return;
  108.     }
  109. }