vendor/store.shopware.com/swagb2bplatform/components/AclRoute/BridgePlatform/RequestInterceptorSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\AclRoute\BridgePlatform;
  3. use Shopware\B2B\AclRoute\Framework\AclRouteService;
  4. use Shopware\B2B\Common\Controller\B2bControllerRedirectException;
  5. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  6. use SwagB2bPlatform\Routing\RouteLoader;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\KernelEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. class RequestInterceptorSubscriber implements EventSubscriberInterface
  11. {
  12.     public const ERROR_ACTION 'error';
  13.     public const ERROR_CONTROLLER 'b2bacl';
  14.     private AclRouteService $aclRoutingService;
  15.     public function __construct(
  16.         AclRouteService $aclRoutingService
  17.     ) {
  18.         $this->aclRoutingService $aclRoutingService;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             KernelEvents::CONTROLLER => ['redirectIfInaccessible'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  24.         ];
  25.     }
  26.     public function redirectIfInaccessible(KernelEvent $event): void
  27.     {
  28.         $request $event->getRequest();
  29.         $controller $request->attributes->get(RouteLoader::ROUTE_CONTROLLER_ROUTE_NAME) ?? '';
  30.         $action $request->attributes->get(RouteLoader::ROUTE_CONTROLLER_ACTION) ?? '';
  31.         if ($action === self::ERROR_ACTION && $controller === self::ERROR_CONTROLLER) {
  32.             return;
  33.         }
  34.         $allowed $this->aclRoutingService
  35.             ->isRouteAllowed($controller$action);
  36.         if (!$allowed) {
  37.             $event->setController(function (): void {
  38.                 throw new B2bControllerRedirectException(self::ERROR_ACTIONself::ERROR_CONTROLLER);
  39.             });
  40.         }
  41.     }
  42. }