custom/plugins/SwagB2bPlatform/components/AclRoute/BridgePlatform/RequestInterceptorSubscriber.php line 36

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.     const ERROR_ACTION 'error';
  13.     const ERROR_CONTROLLER 'b2bacl';
  14.     /**
  15.      * @var AclRouteService
  16.      */
  17.     private $aclRoutingService;
  18.     public function __construct(
  19.         AclRouteService $aclRoutingService
  20.     ) {
  21.         $this->aclRoutingService $aclRoutingService;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             KernelEvents::CONTROLLER => ['redirectIfInaccessible'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  27.         ];
  28.     }
  29.     public function redirectIfInaccessible(KernelEvent $event): void
  30.     {
  31.         $request $event->getRequest();
  32.         $controller $request->attributes->get(RouteLoader::ROUTE_CONTROLLER_ROUTE_NAME) ?? '';
  33.         $action $request->attributes->get(RouteLoader::ROUTE_CONTROLLER_ACTION) ?? '';
  34.         if ($action === self::ERROR_ACTION && $controller === self::ERROR_CONTROLLER) {
  35.             return;
  36.         }
  37.         $allowed $this->aclRoutingService
  38.             ->isRouteAllowed($controller$action);
  39.         if (!$allowed) {
  40.             $event->setController(function (): void {
  41.                 throw new B2bControllerRedirectException(self::ERROR_ACTIONself::ERROR_CONTROLLER);
  42.             });
  43.         }
  44.     }
  45. }