custom/plugins/OkeonlineKejeLogin/src/Storefront/Page/Subscriber/KernelControllerSubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Okeonline\KejeLogin\Storefront\Page\Subscriber;
  3. use Shopware\Core\Framework\Struct\ArrayEntity;
  4. use Symfony\Component\HttpKernel\Event\KernelEvent;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class KernelControllerSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var SystemConfigService $systemConfigService
  14.      */
  15.     private $systemConfigService;
  16.     /**
  17.      * @var UrlGeneratorInterface $router
  18.      */
  19.     private $router;
  20.     public function __construct(
  21.         SystemConfigService $systemConfigService,
  22.         UrlGeneratorInterface $router
  23.     )
  24.     {
  25.         $this->systemConfigService $systemConfigService;
  26.         $this->router $router;
  27.     }
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             'kernel.controller' => ['onKernelControllerEvent', -30],
  32.         ];
  33.     }
  34.     public function onKernelControllerEvent(KernelEvent $event): void
  35.     {
  36.         // Get Portal Active configuration variable, if set to false, continue
  37.         $portalActive $this->systemConfigService->get('OkeonlineKejeLogin.config.portalActive') ?? false;
  38.         if(!$portalActive){
  39.             return;
  40.         }
  41.         // if _route is null, it is not a page-request, but a functional request. So proceed.
  42.         if(!$event->getRequest()->get('_route'))
  43.         {
  44.             return;
  45.         }
  46.         if($event->getRequest()->isXmlHttpRequest())
  47.         {
  48.             return;
  49.         }
  50.         // apply login page only on the 'frontend.' routename scope, otherwize, continue
  51.         // TODO: Check if scoped search is possible. Didnt see it in the attributes
  52.         if(strpos($event->getRequest()->get('_route'), 'frontend.') !== 0){
  53.             return;
  54.         }
  55.         // is the Customer logged in?
  56.         if($event->getRequest()->get('sw-sales-channel-context')->getCustomer() == null)
  57.         {
  58.             // if Customer == null:
  59.             // Check if the requested route is login, or other acceptable pages
  60.             // TODO: Rudi: Make wildcard for future purpose
  61.             $acceptableRoutes = [
  62.                 'frontend.account.login.page',      // accept the login page
  63.                 'frontend.account.login',            // accept the POST-handling of the login page
  64.                 'frontend.account.login.byidentifier',            // accept the POST-handling of the login page   (SPECIFIC FOR THIS MODULE!)
  65.                 'frontend.account.logout.page',            // accept the logout page
  66.                 'frontend.account.recover.page',            // accept the password request page
  67.                 'frontend.account.recover.request',            // accept the password request page
  68.                 'frontend.account.recover.password.page',            // accept the password request page
  69.                 'frontend.account.recover.password.reset',            // accept the password request page
  70.             ];
  71.             if(!in_array($event->getRequest()->attributes->get('_route'), $acceptableRoutes))
  72.             {
  73.                 // if route not login? redirect to login
  74.                 $response = new RedirectResponse($this->router->generate('frontend.account.login.page'));
  75.                 $response->send();
  76.             }
  77.             // else continue to login page, but without any other shopware-page logic. So set a variable for Twig
  78.             $event->getRequest()->attributes->get('sw-sales-channel-context')->addExtension('minimalLayout', new ArrayEntity(["active" => true]));
  79.         }
  80.         // else, customer is customer, so full acces to other routes
  81.     }
  82. }