src/Security/LoginFormAuthenticator.php line 23
<?phpnamespace App\Security;use App\Repository\UserRepository;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\RouterInterface;use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;use Symfony\Component\Security\Core\Exception\AuthenticationException;use Symfony\Component\Security\Core\Exception\UserNotFoundException;use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;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;use Symfony\Component\HttpFoundation\Session\Session;class LoginFormAuthenticator extends AbstractLoginFormAuthenticator{use TargetPathTrait;private $login_route;public function __construct(private UserRepository $userRepository, private RouterInterface $router){}//removable if you don't need to customize redirectionpublic function supports(Request $request) : bool{$this->login_route = $request->attributes->get('_route');return ($request->attributes->get('_route') === 'app_login')&& $request->isMethod('POST');}// public function onAuthenticationSuccess(Request $request, TokenInterface $token, $firewallName): ?Response// {// if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {// return new RedirectResponse($targetPath);// }// return new RedirectResponse($this->router->generate('app_admin_index'));// }public function onAuthenticationSuccess(Request $request, TokenInterface $token, $firewallName): ?Response{$session = $request->getSession();$roles = $token->getRoleNames();$isStudent = in_array('ROLE_ALUMNO', $roles, true);$adminIndexPath = $this->router->generate('app_admin_index');$adminEventsPath = $this->router->generate('app_events');if ($session instanceof Session) {$session->getFlashBag()->add('success','Sesión iniciada correctamente. Puede cerrar sesión a través de la opción disponible en el menú de usuario.');}if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {if (!$isStudent && $this->isAdminIndexTargetPath($targetPath, $adminIndexPath)) {return new RedirectResponse($adminEventsPath);}return new RedirectResponse($targetPath);}if ($isStudent) {return new RedirectResponse($this->router->generate('public_events'));}return new RedirectResponse($adminEventsPath);}public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response{return parent::onAuthenticationFailure($request, $exception);}protected function getLoginUrl(Request $request): string{return $this->router->generate('app_login');}public function authenticate(Request $request) : Passport{$username = $request->request->get('username');$password = $request->request->get('password');$csrf_token = $request->request->get('_csrf_token');return new Passport(new UserBadge($username, function ($userIdentifier){$user = $this->userRepository->findOneByUsernameOrEmail($userIdentifier);if (!$user){throw new UserNotFoundException();}return $user;}),new PasswordCredentials($password),[new CsrfTokenBadge('authenticate',$csrf_token),(new RememberMeBadge())->enable()]);}private function isAdminIndexTargetPath(string $targetPath, string $adminIndexPath): bool{if ($targetPath === $adminIndexPath) {return true;}$parsedPath = parse_url($targetPath, PHP_URL_PATH);return is_string($parsedPath) && $parsedPath === $adminIndexPath;}}