vendor/store.shopware.com/swagb2bplatform/components/SalesRepresentative/BridgePlatform/SalesRepresentativeSubscriber.php line 49

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\B2B\SalesRepresentative\BridgePlatform;
  3. use Shopware\B2B\Common\Controller\B2bControllerRedirectException;
  4. use Shopware\B2B\SalesRepresentative\Framework\SalesRepresentativeIdentity;
  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 end;
  11. use function explode;
  12. use function in_array;
  13. class SalesRepresentativeSubscriber implements EventSubscriberInterface
  14. {
  15.     protected static $whitelist = [
  16.         'AuthController' => ['logout'],
  17.         'b2bsalesrepresentative' => ['__all__'],
  18.         'CheckoutController' => ['info'],
  19.         'ajax_search' => ['index'],
  20.         'b2baccount' => ['__all__'],
  21.         'error_controller' => ['__all__'],
  22.         'CookieController' => ['__all__'],
  23.     ];
  24.     private AuthenticationService $authenticationService;
  25.     public function __construct(
  26.         AuthenticationService $authenticationService
  27.     ) {
  28.         $this->authenticationService $authenticationService;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             KernelEvents::CONTROLLER => ['redirectSalesRepresentative'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE],
  34.         ];
  35.     }
  36.     public function redirectSalesRepresentative(ControllerEvent $event): void
  37.     {
  38.         $requestArguments $this->extractRequestArgs($event);
  39.         $requestedController $requestArguments['requestedController'];
  40.         $requestedAction $requestArguments['requestedAction'];
  41.         if (!$this->authenticationService->is(SalesRepresentativeIdentity::class)) {
  42.             if ($requestedController !== 'b2bsalesrepresentative' || $requestedAction === 'salesRepresentativeLogin') {
  43.                 return;
  44.             }
  45.             throw new B2bControllerRedirectException('index''b2bdashboard');
  46.         }
  47.         if (isset(static::$whitelist[$requestedController])
  48.             && (in_array($requestedAction, static::$whitelist[$requestedController], true)
  49.                 || in_array('__all__', static::$whitelist[$requestedController], true))
  50.         ) {
  51.             return;
  52.         }
  53.         throw new B2bControllerRedirectException('index''b2bsalesrepresentative');
  54.     }
  55.     /**
  56.      * @internal
  57.      */
  58.     protected function extractRequestArgs(ControllerEvent $args): array
  59.     {
  60.         $attributes $args->getRequest()->attributes;
  61.         if ($attributes->has('_b2b')) {
  62.             return [
  63.                 'requestedController' => $attributes->get('_b2b_controller_route_name'),
  64.                 'requestedAction' => $attributes->get('_b2b_controller_action'),
  65.             ];
  66.         }
  67.         $controllerRoute $attributes->get('_controller');
  68.         $explodedRoute explode('::'$controllerRoute);
  69.         $explodedControllerPath explode('\\'$explodedRoute[0]);
  70.         return [
  71.             'requestedController' => end($explodedControllerPath),
  72.             'requestedAction' => $explodedRoute[1] ?? 'index',
  73.         ];
  74.     }
  75. }