<?php
namespace App\EventSubscriber;
use App\Centralstage\CoreBundle\Service\AuthenticationService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\RouterInterface;
class AuthenticationListener implements EventSubscriberInterface
{
private $router;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
$session = $request->getSession();
$authDetails = $session->get('authDetails', null);
if ($request->attributes->has('_controller')) {
$currentRoute = $request->attributes->get('_route');
// Check if user is authenticated
// if ($currentRoute !== null && $currentRoute !== 'login' && !AuthenticationService::isAuthenticated($request, $authDetails)) {
// // Remove authDetails from session
// $session->remove('authDetails');
// // Redirect to login page if not authenticated
// $response = new Response('', Response::HTTP_FOUND, ['Location' => $this->router->generate('login')]);
// $event->setResponse($response);
// return; // Exit early to prevent further processing
// }
// Store authDetails in session if it is not null
if ($authDetails !== null) {
$session->set('authDetails', $authDetails);
$authDetails1 = unserialize($authDetails);
$UIpreference = $authDetails1["UIpreference"] ?? 0;
$session->set('UIpreference', $UIpreference);
}
}
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
}