src/Security/LoginAuthenticator.php line 45

  1. <?php
  2. namespace App\Security;
  3. use Symfony\Bundle\SecurityBundle\Security;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  10. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  11. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  12. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  14. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  15. use App\Entity\UserEntity;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. class LoginAuthenticator extends AbstractLoginFormAuthenticator
  20. {
  21.     use TargetPathTrait;
  22.     private $entityManager;
  23.     private $translator;
  24.     public const LOGIN_ROUTE 'login';
  25.     public function __construct(private UrlGeneratorInterface $urlGeneratorEntityManagerInterface $entityManagerTranslatorInterface $translator)
  26.     {   
  27.         $this->entityManager $entityManager;
  28.         $this->translator $translator;
  29.     }
  30.     public function authenticate(Request $request): Passport
  31.     {
  32.         $username $request->request->get('username''');
  33.         $user $this->entityManager->getRepository(UserEntity::class)->findOneBy(['username' => $username]);
  34.         
  35.         if (!$user instanceof UserEntity || !$user->getActive()) {
  36.             $message $this->translator->trans('user_not_active');
  37.             throw new CustomUserMessageAuthenticationException($message);
  38.         }
  39.         $request->getSession()->set(Security::LAST_USERNAME$username);
  40.         return new Passport(
  41.             new UserBadge($username),
  42.             new PasswordCredentials($request->request->get('password''')),
  43.             [
  44.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  45.             ]
  46.         );
  47.     }
  48.     public function redirectToLogin(): ?Response
  49.     {
  50.         return new RedirectResponse($this->urlGenerator->generate('login'));
  51.     }
  52.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  53.     {
  54.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  55.             return new RedirectResponse($targetPath);
  56.         }
  57.         return new RedirectResponse($this->urlGenerator->generate('index', ['_locale' => $request->getLocale()]));
  58.     }
  59.     protected function getLoginUrl(Request $request): string
  60.     {
  61.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  62.     }
  63. }