src/Security/LoginFormAuthenticator.php line 23
<?phpnamespace App\Security;use App\Entity\User;use App\Repository\UserRepository;use Doctrine\ORM\EntityManagerInterface;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Generator\UrlGeneratorInterface;use Symfony\Component\Routing\RouterInterface;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Exception\AccessDeniedException;use Symfony\Component\Security\Core\Security;use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;use Symfony\Component\Security\Http\Authenticator\Passport\Passport;use Symfony\Component\Security\Http\Util\TargetPathTrait;class LoginFormAuthenticator extends AbstractLoginFormAuthenticator{use TargetPathTrait;public const LOGIN_ROUTE = 'app_login';public const SUCCESS_ROUTE = 'dashboard';public function __construct(private EntityManagerInterface $manager, private RouterInterface $router, private UrlGeneratorInterface $urlGenerator, private UserRepository $userRepository){}public function authenticate(Request $request): Passport{$username = $request->request->get('username', '');$request->getSession()->set(Security::LAST_USERNAME, $username);// Retrieve the authenticated user$user = $this->userRepository->findByEmailOrUsername($request->request->get('username', ''));if ($user) {// Check if the user has the denied rolesif (in_array('ROLE_LECTURER', $user->getRoles()) || in_array('ROLE_STUDENT', $user->getRoles()) || in_array('ROLE_AGENT', $user->getRoles())) {throw new AccessDeniedException('Permission denied!');}}return new Passport(new UserBadge($username, function ($username) {return $this->userRepository->findByEmailOrUsername($username);}),new PasswordCredentials($request->request->get('password', '')),[new CsrfTokenBadge('authenticate', $request->request->get('_csrf_token')),]);}public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response{// if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {// return new RedirectResponse($targetPath);// }if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {return new RedirectResponse($targetPath);}return new RedirectResponse(self::SUCCESS_ROUTE);// For example:// return new RedirectResponse($this->urlGenerator->generate('some_route'));// throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);}protected function getLoginUrl(Request $request): string{return $this->urlGenerator->generate(self::LOGIN_ROUTE);}/** @param $user* @param TokenInterface $token* @param Request $request*/// private function getRoutesForUser(User $user, TokenInterface $token, Request $request): void// {// $this->setUserLastLogin($user);//// $roles = $token->getUser()->getRoles();//// $routes = ['app_login'];//// foreach ($roles as $role) {// $result = $this->lienSecuriseRepository->findRoutesForRoleAndUser($role);//// foreach ($result as $value) {// $routes[] = $value['routeName'];// }// }//// $request->getSession()->set('routes', $routes);// }//// private function setUserLastLogin(User $user)// {// // set last login// $user->setLastLogin(new \DateTime());// $this->manager->flush();//// }}