custom/plugins/CioSso/src/Subscriber/SsoSubscriber.php line 38

Open in your IDE?
  1. <?php
  2. namespace CioSso\Subscriber;
  3. use CioSso\Service\Sso;
  4. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  5. use Shopware\Storefront\Event\StorefrontRenderEvent;
  6. use Shopware\Storefront\Framework\Routing\Router;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. class SsoSubscriber implements EventSubscriberInterface
  10. {
  11.     CONST WHITELIST_ROUTES = [
  12.         '/loginpage/sso/email'
  13.     ];
  14.     private Sso $sso;
  15.     private Router $router;
  16.     public function __construct(Sso $ssoRouter $router)
  17.     {
  18.         $this->sso $sso;
  19.         $this->router $router;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  24.         return [
  25.             'Shopware\Storefront\Event\StorefrontRenderEvent' => 'onStorefrontRenderEvent'
  26.         ];
  27.     }
  28.     public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
  29.     {
  30.         if($event->getSalesChannelContext()->getCustomer() === null) {
  31.             return;
  32.         }
  33.         if (in_array($event->getRequest()->getPathInfo(), self::WHITELIST_ROUTES)) {
  34.             return;
  35.         }
  36.         if ($this->sso->validateCustomerEmail($event->getSalesChannelContext()->getCustomer()) === false) {
  37.             $path $this->router->generate('frontend.loginpage.sso.email');
  38.             (new RedirectResponse($path))->send();
  39.         }
  40.     }
  41. }