<?php
/*
* This file is part of the sdk-bundle-source package.
*
* (c) Benjamin Georgeault
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nellapp\Bundle\SDKBundle\EventSubscriber;
use Nellapp\Bundle\SDKBundle\Routing\UrlGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Class AbstractForceUserFromSetupSubscriber
*
* @author Benjamin Georgeault
*/
abstract class AbstractForceUserFromSetupSubscriber implements EventSubscriberInterface
{
public function __construct(
private Security $security,
private UrlGeneratorInterface $urlGenerator,
) {}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => '__invoke',
];
}
public function __invoke(RequestEvent $event): void
{
if (!$event->isMainRequest()) {
return;
}
if (
'security.firewall.map.context.dev' === $event->getRequest()->attributes->get('_firewall_context')
|| (null === $user = $this->security->getUser())
|| $this->preventDo($event, $user)
|| !$this->hasUserFromChannel($user)
) {
return;
}
$event->setResponse(new RedirectResponse($this->urlGenerator->generate('core', 'app_account_edit')));
}
protected function preventDo(RequestEvent $event, UserInterface $user): bool
{
return false;
}
abstract protected function hasUserFromChannel(UserInterface $user): bool;
}