src/EventSubscriber/SuspendedClientSubscriber.php line 55

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Services\TokenVerifier;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. use Symfony\Component\Routing\RouteCollection;
  10. use Symfony\Component\Routing\RouterInterface;
  11. class SuspendedClientSubscriber implements EventSubscriberInterface
  12. {
  13.     private TokenVerifier $tokenVerifier;
  14.     private RouterInterface $router;
  15.     private RouteCollection $routeCollection;
  16.     private bool $done false;
  17.     /**
  18.      * @param TokenVerifier $tokenVerifier
  19.      * @param RouterInterface $router
  20.      */
  21.     public function __construct(TokenVerifier $tokenVerifierRouterInterface $router)
  22.     {
  23.         $this->tokenVerifier $tokenVerifier;
  24.         $this->router $router;
  25.         $this->routeCollection $router->getRouteCollection();
  26.     }
  27.     /**
  28.      * Redirect to correct locale route if omitted or not concordant with route requirements.
  29.      *
  30.      * @param ResponseEvent $event
  31.      * @return void
  32.      */
  33.     public function onKernelResponse(ResponseEvent $event): void
  34.     {
  35.         //--------------------------------------------------------------------------------------------------------------
  36.         // GOAL:
  37.         //--------------------------------------------------------------------------------------------------------------
  38.         // Redirect all incoming requests to their /locale/route equivalent as long as the route will exists when we do so.
  39.         // If the route required a specific locale so we force it.
  40.         // Do nothing if it already has /locale/ in the route to prevent redirect loops
  41.         //--------------------------------------------------------------------------------------------------------------
  42.     }
  43.     /**
  44.      * @param RequestEvent $event
  45.      * @return void
  46.      */
  47.     public function onKernelRequest(RequestEvent $event): void
  48.     {
  49.         $request $event->getRequest();
  50.         if ($this->done === true || !$request->hasPreviousSession() || $this->tokenVerifier->getClient() === null) {
  51.             return;
  52.         }
  53.         if ($this->tokenVerifier->isTokenValid() === false || ($user $this->tokenVerifier->getClient()) === null) {
  54.             $this->done true;
  55.             $response = new RedirectResponse($this->router->generate('app_login', [
  56.                 '_locale' => $request->getLocale(),
  57.             ]));
  58.             // Empty data.
  59.             $this->tokenVerifier->empty();
  60.             // Add a message to notify that session is expired.
  61.             $request->getSession()->getFlashBag()->add('danger'"Votre session a expiré.");
  62.             // Redirect to the login page.
  63.             $event->setResponse($response);
  64.         }
  65.     }
  66.     public static function getSubscribedEvents()
  67.     {
  68.         return [
  69.             KernelEvents::RESPONSE => [['onKernelResponse'17]],
  70.             // must be registered before (i.e. with a higher priority than) the default Locale listener
  71.             KernelEvents::REQUEST => [['onKernelRequest'20]],
  72.         ];
  73.     }
  74. }