src/EventListener/JsRoutingAccessListener.php line 15

Open in your IDE?
  1. <?php
  2. namespace MedBrief\MSR\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  5. use Symfony\Component\Security\Core\Security;
  6. class JsRoutingAccessListener
  7. {
  8. public function __construct(private readonly Security $security)
  9. {
  10. }
  11. public function onKernelRequest(RequestEvent $event): void
  12. {
  13. $request = $event->getRequest();
  14. // Check if the route matches the JS routing route
  15. // Restrict access if the user is not logged in
  16. if ($request->attributes->get('_route') === 'fos_js_routing_js' && !$this->security->isGranted('ROLE_USER')) {
  17. throw new AccessDeniedHttpException('You must be logged in to access this resource.');
  18. }
  19. }
  20. }