src/EventSubscriber/EventSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\RequestEvent;
  5. use Symfony\Component\HttpKernel\KernelEvents;
  6. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  7. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  8. use Symfony\Component\Security\Core\Security;
  9. class EventSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var Security
  13.      */
  14.     private $security;
  15.     private $tokenStorage;
  16.     public function __construct(Security $security, TokenStorageInterface $tokenStorage)
  17.     {
  18.         $this->security = $security;
  19.         $this->tokenStorage = $tokenStorage;
  20.     }
  21.     public function onKernelRequest(RequestEvent $event): void
  22.     {
  23.         try {
  24.             // trigger only for logged in users
  25.             if ($this->security->isGranted('IS_AUTHENTICATED_FULLY')) {
  26.                 $token = $this->security->getToken();
  27.                 /**
  28.                  * @var User $user
  29.                  */
  30.                 $user = $token->getUser();
  31.                 if ($user != null && !$user->getIsConnected()) {
  32.                     // $this->tokenStorage->setToken(null);
  33.                     // $event->getRequest()->getSession()->clear();
  34.                 }
  35.             }
  36.         } catch (AuthenticationCredentialsNotFoundException $e) {
  37.             // don't do anything here... or do whatever you want.
  38.         }
  39.     }
  40.     public static function getSubscribedEvents(): array
  41.     {
  42.         return [
  43.             KernelEvents::REQUEST => 'onKernelRequest',
  44.         ];
  45.     }
  46. }