<?php declare(strict_types=1);
namespace Okeonline\KejeLogin\Storefront\Page\Subscriber;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class KernelControllerSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService $systemConfigService
*/
private $systemConfigService;
/**
* @var UrlGeneratorInterface $router
*/
private $router;
public function __construct(
SystemConfigService $systemConfigService,
UrlGeneratorInterface $router
)
{
$this->systemConfigService = $systemConfigService;
$this->router = $router;
}
public static function getSubscribedEvents()
{
return [
'kernel.controller' => ['onKernelControllerEvent', -30],
];
}
public function onKernelControllerEvent(KernelEvent $event): void
{
// Get Portal Active configuration variable, if set to false, continue
$portalActive = $this->systemConfigService->get('OkeonlineKejeLogin.config.portalActive') ?? false;
if(!$portalActive){
return;
}
// if _route is null, it is not a page-request, but a functional request. So proceed.
if(!$event->getRequest()->get('_route'))
{
return;
}
if($event->getRequest()->isXmlHttpRequest())
{
return;
}
// apply login page only on the 'frontend.' routename scope, otherwize, continue
// TODO: Check if scoped search is possible. Didnt see it in the attributes
if(strpos($event->getRequest()->get('_route'), 'frontend.') !== 0){
return;
}
// is the Customer logged in?
if($event->getRequest()->get('sw-sales-channel-context')->getCustomer() == null)
{
// if Customer == null:
// Check if the requested route is login, or other acceptable pages
// TODO: Rudi: Make wildcard for future purpose
$acceptableRoutes = [
'frontend.account.login.page', // accept the login page
'frontend.account.login', // accept the POST-handling of the login page
'frontend.account.login.byidentifier', // accept the POST-handling of the login page (SPECIFIC FOR THIS MODULE!)
'frontend.account.logout.page', // accept the logout page
'frontend.account.recover.page', // accept the password request page
'frontend.account.recover.request', // accept the password request page
'frontend.account.recover.password.page', // accept the password request page
'frontend.account.recover.password.reset', // accept the password request page
];
if(!in_array($event->getRequest()->attributes->get('_route'), $acceptableRoutes))
{
// if route not login? redirect to login
$response = new RedirectResponse($this->router->generate('frontend.account.login.page'));
$response->send();
}
// else continue to login page, but without any other shopware-page logic. So set a variable for Twig
$event->getRequest()->attributes->get('sw-sales-channel-context')->addExtension('minimalLayout', new ArrayEntity(["active" => true]));
}
// else, customer is customer, so full acces to other routes
}
}