custom/plugins/SwagB2bPlatform/SwagB2bPlatform/Subscriber/B2bModuleFirewall.php line 38

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SwagB2bPlatform\Subscriber;
  3. use Shopware\B2B\Common\Controller\B2bControllerRedirectToLogin;
  4. use Shopware\B2B\StoreFrontAuthentication\Framework\AuthenticationService;
  5. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  6. use SwagB2bPlatform\Routing\RouteLoader;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use function explode;
  11. class B2bModuleFirewall implements EventSubscriberInterface
  12. {
  13.     const ALLOWED_CONTROLLER = [
  14.         'b2b_contact.password_activation_controller' => true,
  15.     ];
  16.     /**
  17.      * @var AuthenticationService
  18.      */
  19.     private $authenticationService;
  20.     public function __construct(
  21.         AuthenticationService $authenticationService
  22.     ) {
  23.         $this->authenticationService $authenticationService;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             KernelEvents::CONTROLLER => ['redirectOutOfB2b'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_SCOPE_VALIDATE_POST],
  29.         ];
  30.     }
  31.     public function redirectOutOfB2b(ControllerEvent $event): void
  32.     {
  33.         if ($this->isAllowedTo($event)) {
  34.             return;
  35.         }
  36.         throw new B2bControllerRedirectToLogin();
  37.     }
  38.     /**
  39.      * @internal
  40.      */
  41.     protected function isAllowedTo(ControllerEvent $event): bool
  42.     {
  43.         if ($this->authenticationService->isB2b() || !$event->getRequest()->attributes->get(RouteLoader::ROUTE_IS_B2B)) {
  44.             return true;
  45.         }
  46.         $routingData explode('::'$event->getRequest()->get('_controller'));
  47.         $isAllowedController self::ALLOWED_CONTROLLER[$routingData[0]] ?? false;
  48.         return (bool) $isAllowedController;
  49.     }
  50. }