src/EventSubscriber/PlatformModeAccessSubscriber.php line 34

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use App\Service\ParameterService;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. final class PlatformModeAccessSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @param list<string> $protectedPathPrefixes
  16.      */
  17.     public function __construct(
  18.         private readonly ParameterService $parameterService,
  19.         private readonly AuthorizationCheckerInterface $authorizationChecker,
  20.         private readonly UrlGeneratorInterface $urlGenerator,
  21.         private readonly array $protectedPathPrefixes,
  22.     ) {}
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         // Run after routing and firewall authentication so the route and user token are available.
  26.         return [KernelEvents::REQUEST => ['onKernelRequest', 0]];
  27.     }
  28.     public function onKernelRequest(RequestEvent $event): void
  29.     {
  30.         if (!$event->isMainRequest()) {
  31.             return;
  32.         }
  33.         $request = $event->getRequest();
  34.         if (
  35.             !$this->matchesProtectedPath($request->getPathInfo())
  36.             || !$this->parameterService->isB2BMode()
  37.         ) {
  38.             return;
  39.         }
  40.         // B2B product routes require a fully authenticated session, including completed 2FA.
  41.         if ($this->authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY')) {
  42.             return;
  43.         }
  44.         $loginUrl = $this->urlGenerator->generate('app_login', ['type' => 'btob']);
  45.         $accept = $request->headers->get('Accept', '');
  46.         if ($request->isXmlHttpRequest() || str_contains($accept, 'application/json')) {
  47.             $event->setResponse(new JsonResponse([
  48.                 'error' => 'Authentication required',
  49.                 'loginUrl' => $loginUrl,
  50.             ], JsonResponse::HTTP_UNAUTHORIZED));
  51.             return;
  52.         }
  53.         $event->setResponse(new RedirectResponse($loginUrl));
  54.     }
  55.     private function matchesProtectedPath(string $path): bool
  56.     {
  57.         foreach ($this->protectedPathPrefixes as $prefix) {
  58.             $prefix = '/' . trim($prefix, '/');
  59.             if ($path === $prefix || str_starts_with($path, $prefix . '/')) {
  60.                 return true;
  61.             }
  62.         }
  63.         return false;
  64.     }
  65. }