Last active
March 21, 2021 13:38
-
-
Save tarlepp/067709a269186e37f042824f71119926 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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