vendor/store.shopware.com/swagb2bplatform/SwagB2bPlatform/Subscriber/FrontendAccountFirewall.php line 70

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SwagB2bPlatform\Subscriber;
  3. use Shopware\B2B\AclRoute\Framework\AclRouteService;
  4. use Shopware\B2B\Common\Controller\B2bControllerRedirectException;
  5. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthenticationService;
  6. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use function array_key_exists;
  11. use function end;
  12. use function explode;
  13. use function in_array;
  14. class FrontendAccountFirewall implements EventSubscriberInterface
  15. {
  16.     private const ERROR_CONTROLLER_ROUTE_NAME 'error_controller';
  17.     /**
  18.      * @internal
  19.      */
  20.     private static array $routes = [
  21.         'AuthController' => [
  22.             'ignore' => ['logout'],
  23.             'redirectTo' => 'b2bdashboard',
  24.         ],
  25.         'AddressController' => [
  26.             'ignore' => ['addressBook'],
  27.             'redirectTo' => 'b2bcompany',
  28.         ],
  29.         'AccountPaymentController' => [
  30.             'ignore' => [],
  31.             'redirectTo' => 'b2bdashboard',
  32.         ],
  33.         'AccountProfileController' => [
  34.             'ignore' => [],
  35.             'redirectTo' => 'b2bdashboard',
  36.         ],
  37.         'RegisterController' => [
  38.             'ignore' => [],
  39.             'redirectTo' => 'b2bdashboard',
  40.         ],
  41.         'AccountOrderController' => [
  42.             'ignore' => ['editOrder''orderChangePayment''updateOrder'],
  43.             'redirectTo' => 'b2border',
  44.         ],
  45.     ];
  46.     private AuthenticationService $authenticationService;
  47.     private AclRouteService $aclRouteService;
  48.     public function __construct(
  49.         AuthenticationService $authenticationService,
  50.         AclRouteService $aclRouteService
  51.     ) {
  52.         $this->authenticationService $authenticationService;
  53.         $this->aclRouteService $aclRouteService;
  54.     }
  55.     public static function getSubscribedEvents(): array
  56.     {
  57.         return [
  58.             KernelEvents::CONTROLLER => ['redirectToController'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  59.         ];
  60.     }
  61.     public function redirectToController(ControllerEvent $args): void
  62.     {
  63.         if (!$this->authenticationService->isB2b()) {
  64.             return;
  65.         }
  66.         if ($this->isError($args)) {
  67.             return;
  68.         }
  69.         $argsData $this->extractArgs($args);
  70.         $requestedController $argsData['requestedController'];
  71.         $requestedAction $argsData['requestedAction'];
  72.         if (!array_key_exists($requestedControllerself::$routes)) {
  73.             return;
  74.         }
  75.         $routingSettings self::$routes[$requestedController];
  76.         $actionsToIgnore $routingSettings['ignore'];
  77.         if (in_array($requestedAction$actionsToIgnoretrue)) {
  78.             return;
  79.         }
  80.         $redirectToController $routingSettings['redirectTo'];
  81.         if ($redirectToController === 'b2borderlist'
  82.             && !$this->aclRouteService->isRouteAllowed($redirectToController'index')) {
  83.             return;
  84.         }
  85.         throw new B2bControllerRedirectException('index'$redirectToController);
  86.     }
  87.     /**
  88.      * @internal
  89.      */
  90.     protected function extractArgs(ControllerEvent $args): array
  91.     {
  92.         $controllerRoute $args->getRequest()->attributes->get('_controller');
  93.         $explodedRoute explode('::'$controllerRoute);
  94.         $explodedControllerPath explode('\\'$explodedRoute[0]);
  95.         $requestedController end($explodedControllerPath);
  96.         $requestedAction $explodedRoute[1];
  97.         return [
  98.             'requestedController' => $requestedController,
  99.             'requestedAction' => $requestedAction,
  100.         ];
  101.     }
  102.     /**
  103.      * @internal
  104.      */
  105.     protected function isError(ControllerEvent $args): bool
  106.     {
  107.         return $args->getRequest()->attributes->get('_controller') === static::ERROR_CONTROLLER_ROUTE_NAME;
  108.     }
  109. }