src/EventSubscriber/AuthenticationListener.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Centralstage\CoreBundle\Service\AuthenticationService;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\RouterInterface;
  9. class AuthenticationListener implements EventSubscriberInterface
  10. {
  11.     private $router;
  12.     public function __construct(RouterInterface $router)
  13.     {
  14.         $this->router $router;
  15.     }
  16.     public function onKernelRequest(RequestEvent $event)
  17.     {
  18.         $request $event->getRequest();
  19.         $session $request->getSession();
  20.         $authDetails $session->get('authDetails'null);
  21.         if ($request->attributes->has('_controller')) {
  22.             $currentRoute $request->attributes->get('_route');
  23.             // Check if user is authenticated
  24.             // if ($currentRoute !== null && $currentRoute !== 'login' && !AuthenticationService::isAuthenticated($request, $authDetails)) {
  25.             //     // Remove authDetails from session
  26.             //     $session->remove('authDetails');
  27.             //     // Redirect to login page if not authenticated
  28.             //     $response = new Response('', Response::HTTP_FOUND, ['Location' => $this->router->generate('login')]);
  29.             //     $event->setResponse($response);
  30.             //     return; // Exit early to prevent further processing
  31.             // }
  32.             // Store authDetails in session if it is not null
  33.             if ($authDetails !== null) {
  34.                 $session->set('authDetails'$authDetails);
  35.                 $authDetails1   unserialize($authDetails);
  36.                 $UIpreference   $authDetails1["UIpreference"] ?? 0;
  37.                 $session->set('UIpreference'$UIpreference);
  38.             }
  39.         }
  40.     }
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             KernelEvents::REQUEST => 'onKernelRequest',
  45.         ];
  46.     }
  47. }