custom/plugins/NetiNextAccessManager/src/Subscriber/StorefrontSubscriber.php line 246

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextAccessManager\Subscriber;
  4. use NetInventors\NetiNextAccessManager\Components\StringHelper;
  5. use NetInventors\NetiNextAccessManager\Events\Subscriber\ExtendControllerWhitelist;
  6. use NetInventors\NetiNextAccessManager\Events\Subscriber\ExtendTemplatesEvent;
  7. use NetInventors\NetiNextAccessManager\Service\PluginConfig;
  8. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  9. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  10. use Shopware\Core\Checkout\Customer\CustomerEntity;
  11. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  12. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  13. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  14. use Shopware\Storefront\Controller\AuthController;
  15. use Shopware\Storefront\Controller\CartLineItemController;
  16. use Shopware\Storefront\Controller\CmsController;
  17. use Shopware\Storefront\Controller\ContextController;
  18. use Shopware\Storefront\Controller\CookieController;
  19. use Shopware\Storefront\Controller\CountryStateController;
  20. use Shopware\Storefront\Controller\FormController;
  21. use Shopware\Storefront\Controller\RegisterController;
  22. use Shopware\Storefront\Controller\SearchController;
  23. use Symfony\Bundle\FrameworkBundle\Controller\TemplateController;
  24. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Symfony\Component\HttpFoundation\RedirectResponse;
  27. use Symfony\Component\HttpFoundation\Request;
  28. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  29. use Symfony\Component\HttpKernel\KernelEvents;
  30. use Symfony\Component\Routing\RouterInterface;
  31. class StorefrontSubscriber implements EventSubscriberInterface
  32. {
  33.     /**
  34.      * @var CustomerEntity
  35.      */
  36.     private $customer;
  37.     /**
  38.      * @var SalesChannelEntity
  39.      */
  40.     private $salesChannel;
  41.     /**
  42.      * @var PluginConfig
  43.      */
  44.     private $pluginConfig;
  45.     /**
  46.      * @var RouterInterface
  47.      */
  48.     private $router;
  49.     /**
  50.      * @var TemplateController
  51.      */
  52.     private $templateController;
  53.     /**
  54.      * @var EventDispatcherInterface
  55.      */
  56.     private $eventDispatcher;
  57.     /**
  58.      * @var CartService
  59.      */
  60.     private $cartService;
  61.     /**
  62.      * Storefront constructor.
  63.      *
  64.      * @param PluginConfig       $pluginConfig
  65.      * @param RouterInterface    $router
  66.      * @param TemplateController $templateController
  67.      * @param EventDispatcherInterface    $eventDispatcher
  68.      */
  69.     public function __construct(
  70.         PluginConfig $pluginConfig,
  71.         RouterInterface $router,
  72.         TemplateController $templateController,
  73.         EventDispatcherInterface $eventDispatcher,
  74.         CartService $cartService
  75.     ) {
  76.         $this->pluginConfig       $pluginConfig;
  77.         $this->router             $router;
  78.         $this->templateController $templateController;
  79.         $this->eventDispatcher    $eventDispatcher;
  80.         $this->cartService        $cartService;
  81.     }
  82.     /**
  83.      * @return array|string[]
  84.      */
  85.     public static function getSubscribedEvents(): array
  86.     {
  87.         return [
  88.             SalesChannelContextResolvedEvent::class => 'onSalesChannelResolved',
  89.             KernelEvents::RESPONSE                  => 'onResponse',
  90.             AfterLineItemAddedEvent::class          => 'onItemAdded',
  91.         ];
  92.     }
  93.     /**
  94.      * @param SalesChannelContextResolvedEvent $event
  95.      */
  96.     public function onSalesChannelResolved(SalesChannelContextResolvedEvent $event): void
  97.     {
  98.         if (!$this->pluginConfig->isActive()) {
  99.             return;
  100.         }
  101.         $this->salesChannel $event->getSalesChannelContext()->getSalesChannel();
  102.         $this->customer     $event->getSalesChannelContext()->getCustomer();
  103.     }
  104.     /**
  105.      * @param ResponseEvent $event
  106.      */
  107.     public function onResponse(ResponseEvent $event): void
  108.     {
  109.         if (!$this->pluginConfig->isActive()) {
  110.             return;
  111.         }
  112.         $request $event->getRequest();
  113.         if ($this->isShopHidden($request)) {
  114.             $message['type'] = 'danger';
  115.             $message['text'] = 'login-first';
  116.             if ($request->isXmlHttpRequest()) {
  117.                 $templates = [
  118.                     CartLineItemController::class => '@NetiNextAccessManager/storefront/access-manager/blocked/search.html.twig',
  119.                     SearchController::class       => '@NetiNextAccessManager/storefront/access-manager/blocked/search.html.twig',
  120.                 ];
  121.                 $extendTemplatesEvent = new ExtendTemplatesEvent($this$templates);
  122.                 $this->eventDispatcher->dispatch($extendTemplatesEventExtendTemplatesEvent::NAME);
  123.                 $currentController $request->get('_controller');
  124.                 $currentController substr($currentController0strpos($currentController'::'));
  125.                 if (isset($templates[$currentController])) {
  126.                     $response $this->templateController->templateAction($templates[$currentController]);
  127.                 } else {
  128.                     $response $this->templateController->templateAction(
  129.                         '@NetiNextAccessManager/storefront/access-manager/blocked/xml-rejected.html.twig'
  130.                     );
  131.                 }
  132.             } elseif ('' !== $this->pluginConfig->getRedirectCategory()) {
  133.                 $response = new RedirectResponse(
  134.                     $this->router->generate(
  135.                         'frontend.navigation.page',
  136.                         [ 'navigationId' => $this->pluginConfig->getRedirectCategory() ]
  137.                     )
  138.                 );
  139.             } else {
  140.                 $response = new RedirectResponse(
  141.                     $this->router->generate('frontend.account.login.page', [ 'amMessage' => $message ])
  142.                 );
  143.             }
  144.             $event->setResponse($response);
  145.         }
  146.     }
  147.     /**
  148.      * @param Request $request
  149.      *
  150.      * @return bool
  151.      */
  152.     public function isShopHidden(Request $request): bool
  153.     {
  154.         /** @var RouteScope $routeScope */
  155.         $routeScope $request->get('_routeScope');
  156.         if (
  157.             null === $routeScope
  158.             || $routeScope->hasScope('administration')
  159.             || $routeScope->hasScope('api')
  160.             || $routeScope->hasScope('store-api')
  161.         ) {
  162.             return false;
  163.         }
  164.         if (null !== $this->customer || false === $this->pluginConfig->isHideShop()) {
  165.             return false;
  166.         }
  167.         $navigationId $request->attributes->get('navigationId');
  168.         if (
  169.             \in_array($navigationId$this->pluginConfig->getAllowedCategories(), true)
  170.             || $navigationId === $this->pluginConfig->getRedirectCategory()
  171.             || ($request->getRequestUri() === '/'
  172.                 && \in_array(
  173.                     $this->salesChannel->getNavigationCategoryId(),
  174.                     $this->pluginConfig->getAllowedCategories(),
  175.                     true
  176.                 ))
  177.             || \in_array($request->getRequestUri(), $this->pluginConfig->getWhitelistedPaths())
  178.         ) {
  179.             return false;
  180.         }
  181.         foreach ($this->pluginConfig->getWhitelistedPaths() as $path) {
  182.             if (StringHelper::stringMatchWithWildcard($request->getRequestUri(), $path)) {
  183.                 return false;
  184.             }
  185.         }
  186.         $accessList array_merge([
  187.             AuthController::class,
  188.             ContextController::class,
  189.             CmsController::class,
  190.             FormController::class,
  191.             CountryStateController::class,
  192.             RegisterController::class,
  193.             CookieController::class,
  194.         ], $this->pluginConfig->getWhitelistedControllers());
  195.         $extendWhitelistControllerEvent = new ExtendControllerWhitelist($this$accessList);
  196.         $this->eventDispatcher->dispatch($extendWhitelistControllerEventExtendControllerWhitelist::NAME);
  197.         $currentController $request->get('_controller');
  198.         
  199.         foreach ($accessList as $controller) {
  200.             if (=== strpos($currentController"{$controller}::")) {
  201.                 return false;
  202.             }
  203.             if (StringHelper::stringMatchWithWildcard($currentController$controller)) {
  204.                 return false;
  205.             }
  206.         }
  207.         return true;
  208.     }
  209.     /**
  210.      * @param AfterLineItemAddedEvent $event
  211.      */
  212.     public function onItemAdded(AfterLineItemAddedEvent $event): void
  213.     {
  214.         if (
  215.             !$this->pluginConfig->isHideShop()
  216.             || !$this->pluginConfig->isActive()
  217.             || $event->getSalesChannelContext()->getCustomer() !== null
  218.         ) {
  219.             return;
  220.         }
  221.         $this->cartService->deleteCart($event->getSalesChannelContext());
  222.     }
  223. }