Skip to content

Instantly share code, notes, and snippets.

@tarlepp
Last active March 21, 2021 13:38
Show Gist options
  • Save tarlepp/067709a269186e37f042824f71119926 to your computer and use it in GitHub Desktop.
Save tarlepp/067709a269186e37f042824f71119926 to your computer and use it in GitHub Desktop.
<?php
namespace App\EventSubscriber;
use App\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\Router;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class Foo implements EventSubscriberInterface
{
public function __construct(
private Router $router,
private TokenStorageInterface $tokenStorage
) {
}
public static function getSubscribedEvents(): array
{
return [
RequestEvent::class => 'onKernelRequest',
];
}
public function onKernelRequest(RequestEvent $event): void
{
$token = $this->tokenStorage->getToken();
if ($token instanceof TokenInterface) {
$user = $token->getUser();
if ($user instanceof User
&& ($user->getCompanyProfile() === null || $user->getFreelancerProfile() === null)
) {
$event->setResponse(new RedirectResponse($this->router->generate('selected_account_type')));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment