custom/plugins/SwagB2bPlatform/SwagB2bPlatform/Subscriber/FrontendAccountFirewall.php line 76

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 $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.     /**
  47.      * @var AuthenticationService
  48.      */
  49.     private $authenticationService;
  50.     /**
  51.      * @var AclRouteService
  52.      */
  53.     private $aclRouteService;
  54.     public function __construct(
  55.         AuthenticationService $authenticationService,
  56.         AclRouteService $aclRouteService
  57.     ) {
  58.         $this->authenticationService $authenticationService;
  59.         $this->aclRouteService $aclRouteService;
  60.     }
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             KernelEvents::CONTROLLER => ['redirectToController'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  65.         ];
  66.     }
  67.     public function redirectToController(ControllerEvent $args): void
  68.     {
  69.         if (!$this->authenticationService->isB2b()) {
  70.             return;
  71.         }
  72.         if ($this->isError($args)) {
  73.             return;
  74.         }
  75.         $argsData $this->extractArgs($args);
  76.         $requestedController $argsData['requestedController'];
  77.         $requestedAction $argsData['requestedAction'];
  78.         if (!array_key_exists($requestedControllerself::$routes)) {
  79.             return;
  80.         }
  81.         $routingSettings self::$routes[$requestedController];
  82.         $actionsToIgnore $routingSettings['ignore'];
  83.         if (in_array($requestedAction$actionsToIgnoretrue)) {
  84.             return;
  85.         }
  86.         $redirectToController $routingSettings['redirectTo'];
  87.         if ($redirectToController === 'b2borderlist'
  88.             && !$this->aclRouteService->isRouteAllowed($redirectToController'index')) {
  89.             return;
  90.         }
  91.         throw new B2bControllerRedirectException('index'$redirectToController);
  92.     }
  93.     /**
  94.      * @internal
  95.      */
  96.     protected function extractArgs(ControllerEvent $args): array
  97.     {
  98.         $controllerRoute $args->getRequest()->attributes->get('_controller');
  99.         $explodedRoute explode('::'$controllerRoute);
  100.         $explodedControllerPath explode('\\'$explodedRoute[0]);
  101.         $requestedController end($explodedControllerPath);
  102.         $requestedAction $explodedRoute[1];
  103.         return [
  104.             'requestedController' => $requestedController,
  105.             'requestedAction' => $requestedAction,
  106.         ];
  107.     }
  108.     /**
  109.      * @internal
  110.      */
  111.     protected function isError(ControllerEvent $args): bool
  112.     {
  113.         return $args->getRequest()->attributes->get('_controller') === static::ERROR_CONTROLLER_ROUTE_NAME;
  114.     }
  115. }