custom/plugins/SwagB2bPlatform/components/SalesRepresentative/BridgePlatform/SalesRepresentativeSubscriber.php line 62

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\Shop\Framework\StorageInterface;
  6. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthenticationService;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerLogoutEvent;
  8. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use function end;
  13. use function explode;
  14. use function in_array;
  15. class SalesRepresentativeSubscriber implements EventSubscriberInterface
  16. {
  17.     protected static $whitelist = [
  18.         'AuthController' => ['logout'],
  19.         'b2bsalesrepresentative' => ['__all__'],
  20.         'CheckoutController' => ['info'],
  21.         'ajax_search' => ['index'],
  22.         'b2baccount' => ['__all__'],
  23.         'error_controller' => ['__all__'],
  24.         'CookieController' => ['__all__'],
  25.     ];
  26.     /**
  27.      * @var AuthenticationService
  28.      */
  29.     private $authenticationService;
  30.     /**
  31.      * @var StorageInterface
  32.      */
  33.     private $storage;
  34.     public function __construct(
  35.         AuthenticationService $authenticationService,
  36.         StorageInterface $storage
  37.     ) {
  38.         $this->authenticationService $authenticationService;
  39.         $this->storage $storage;
  40.     }
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             KernelEvents::CONTROLLER => ['redirectSalesRepresentative'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE],
  45.             CustomerLogoutEvent::class => 'unsetIdentity',
  46.         ];
  47.     }
  48.     public function redirectSalesRepresentative(ControllerEvent $event): void
  49.     {
  50.         $requestArguments $this->extractRequestArgs($event);
  51.         $requestedController $requestArguments['requestedController'];
  52.         $requestedAction $requestArguments['requestedAction'];
  53.         if (!$this->authenticationService->is(SalesRepresentativeIdentity::class)) {
  54.             if ($requestedController !== 'b2bsalesrepresentative' || $requestedAction === 'salesRepresentativeLogin') {
  55.                 return;
  56.             }
  57.             throw new B2bControllerRedirectException('index''b2bdashboard');
  58.         }
  59.         if (isset(static::$whitelist[$requestedController])
  60.             && (in_array($requestedAction, static::$whitelist[$requestedController], true)
  61.                 || in_array('__all__', static::$whitelist[$requestedController], true))
  62.         ) {
  63.             return;
  64.         }
  65.         throw new B2bControllerRedirectException('index''b2bsalesrepresentative');
  66.     }
  67.     public function unsetIdentity(): void
  68.     {
  69.         $this->storage->remove(SalesRepresentativeIdentity::AUTH_ID_KEY);
  70.     }
  71.     /**
  72.      * @internal
  73.      */
  74.     protected function extractRequestArgs(ControllerEvent $args): array
  75.     {
  76.         $attributes $args->getRequest()->attributes;
  77.         if ($attributes->has('_b2b')) {
  78.             return [
  79.                 'requestedController' => $attributes->get('_b2b_controller_route_name'),
  80.                 'requestedAction' => $attributes->get('_b2b_controller_action'),
  81.             ];
  82.         }
  83.         $controllerRoute $attributes->get('_controller');
  84.         $explodedRoute explode('::'$controllerRoute);
  85.         $explodedControllerPath explode('\\'$explodedRoute[0]);
  86.         return [
  87.             'requestedController' => end($explodedControllerPath),
  88.             'requestedAction' => $explodedRoute[1] ?? 'index',
  89.         ];
  90.     }
  91. }