<?php
namespace App\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
{
private $urlGenerator;
private $security;
private $tokenStorage;
public function __construct(UrlGeneratorInterface $urlGenerator, TokenStorageInterface $tokenStorage, Security $security)
{
$this->urlGenerator = $urlGenerator;
$this->security = $security;
$this->tokenStorage = $tokenStorage;
}
public function start(Request $request, AuthenticationException $authException = null): RedirectResponse
{
// add a custom flash message and redirect to the login page
$request->getSession()->getFlashBag()->add('unauthorized_notice', 'La modification de vos informations est requise lors de votre première connexion.');
$user = $this->security->getUser();
if ($user === null || !in_array('ROLE_EMPLOYEE', $user->getRoles())) {
$this->tokenStorage->setToken();
return new RedirectResponse($this->urlGenerator->generate('app_login'));
}
}
}