<?php
namespace MedBrief\MSR\EventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Security;
class JsRoutingAccessListener
{
public function __construct(private readonly Security $security)
{
}
public function onKernelRequest(RequestEvent $event): void
{
$request = $event->getRequest();
// Check if the route matches the JS routing route
// Restrict access if the user is not logged in
if ($request->attributes->get('_route') === 'fos_js_routing_js' && !$this->security->isGranted('ROLE_USER')) {
throw new AccessDeniedHttpException('You must be logged in to access this resource.');
}
}
}