<?php
namespace CioSso\Subscriber;
use CioSso\Service\Sso;
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Framework\Routing\Router;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class SsoSubscriber implements EventSubscriberInterface
{
CONST WHITELIST_ROUTES = [
'/loginpage/sso/email'
];
private Sso $sso;
private Router $router;
public function __construct(Sso $sso, Router $router)
{
$this->sso = $sso;
$this->router = $router;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
'Shopware\Storefront\Event\StorefrontRenderEvent' => 'onStorefrontRenderEvent'
];
}
public function onStorefrontRenderEvent(StorefrontRenderEvent $event)
{
if($event->getSalesChannelContext()->getCustomer() === null) {
return;
}
if (in_array($event->getRequest()->getPathInfo(), self::WHITELIST_ROUTES)) {
return;
}
if ($this->sso->validateCustomerEmail($event->getSalesChannelContext()->getCustomer()) === false) {
$path = $this->router->generate('frontend.loginpage.sso.email');
(new RedirectResponse($path))->send();
}
}
}