src/Security/LoginFormAuthenticator.php line 23

  1. <?php
  2. namespace App\Security;
  3. use App\Repository\UserRepository;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  10. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  11. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  17. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  18. use Symfony\Component\HttpFoundation\Session\Session;
  19. class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
  20. {
  21.     use TargetPathTrait;
  22.     private $login_route;
  23.     public function __construct(private UserRepository $userRepository, private RouterInterface $router)
  24.     {
  25.     }
  26.     //removable if you don't need to customize redirection
  27.     public function supports(Request $request) : bool
  28.     {
  29.         $this->login_route $request->attributes->get('_route');
  30.         return ($request->attributes->get('_route') === 'app_login')
  31.             && $request->isMethod('POST');
  32.     }
  33.     // public function onAuthenticationSuccess(Request $request, TokenInterface $token, $firewallName): ?Response
  34.     // {
  35.     //     if ($targetPath = $this->getTargetPath($request->getSession(), $firewallName)) {
  36.     //         return new RedirectResponse($targetPath);
  37.     //     }
  38.     //     return new RedirectResponse($this->router->generate('app_admin_index'));
  39.     // }
  40.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$firewallName): ?Response
  41.     {
  42.         $session $request->getSession();
  43.         $roles $token->getRoleNames();
  44.         $isStudent in_array('ROLE_ALUMNO'$rolestrue);
  45.         $adminIndexPath $this->router->generate('app_admin_index');
  46.         $adminEventsPath $this->router->generate('app_events');
  47.         if ($session instanceof Session) {
  48.             $session->getFlashBag()->add(
  49.                 'success',
  50.                 'Sesión iniciada correctamente. Puede cerrar sesión a través de la opción disponible en el menú de usuario.'
  51.             );
  52.         }
  53.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  54.             if (!$isStudent && $this->isAdminIndexTargetPath($targetPath$adminIndexPath)) {
  55.                 return new RedirectResponse($adminEventsPath);
  56.             }
  57.             return new RedirectResponse($targetPath);
  58.         }
  59.         if ($isStudent) {
  60.             return new RedirectResponse($this->router->generate('public_events'));
  61.         }
  62.         return new RedirectResponse($adminEventsPath);
  63.     }
  64.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): Response
  65.     {
  66.         return parent::onAuthenticationFailure($request$exception);
  67.     }
  68.     protected function getLoginUrl(Request $request): string
  69.     {
  70.         return $this->router->generate('app_login');
  71.     }
  72.     public function authenticate(Request $request) : Passport
  73.     {
  74.         $username $request->request->get('username');
  75.         $password $request->request->get('password');
  76.         $csrf_token $request->request->get('_csrf_token');
  77.         return new Passport(
  78.             new UserBadge($username, function ($userIdentifier){
  79.                 $user $this->userRepository->findOneByUsernameOrEmail($userIdentifier);
  80.                 if (!$user){
  81.                     throw new UserNotFoundException();
  82.                 }
  83.                 return $user;
  84.             }),
  85.             new PasswordCredentials($password),
  86.             [
  87.                 new CsrfTokenBadge('authenticate',$csrf_token),
  88.                 (new RememberMeBadge())->enable()
  89.             ]
  90.         );
  91.     }
  92.     private function isAdminIndexTargetPath(string $targetPathstring $adminIndexPath): bool
  93.     {
  94.         if ($targetPath === $adminIndexPath) {
  95.             return true;
  96.         }
  97.         $parsedPath parse_url($targetPathPHP_URL_PATH);
  98.         return is_string($parsedPath) && $parsedPath === $adminIndexPath;
  99.     }
  100. }