src/Security/FormBackendAuthenticator.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Repository\UserRepository;
  4. use App\Security\Badge\Backend\UserStatusBadge;
  5. use App\Security\Badge\Backend\UserTypeBadge;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  18. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
  19. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  20. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  21. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  22. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  23. class FormBackendAuthenticator extends AbstractLoginFormAuthenticator
  24. {
  25.     use TargetPathTrait;
  26.     public const LOGIN_ROUTE 'app_login';
  27.     public function __construct(
  28.         private UrlGeneratorInterface $urlGenerator,
  29.         private UserRepository $userRepository,
  30.         private ParameterBagInterface $parameters,
  31.         private UserPasswordHasherInterface $passwordHasher,
  32.         private EntityManagerInterface $em
  33.     ) {}
  34.     public function supports(Request $request): bool
  35.     {
  36.         return 'app_login' === $request->attributes->get('_route')
  37.             && $request->isMethod('POST');
  38.     }
  39.     public function authenticate(Request $request): Passport
  40.     {
  41.         $username $request->request->get('email''');
  42.         $request->getSession()->set(Security::LAST_USERNAME$username);
  43.         return new Passport(
  44.             //new UserBadge($username),
  45.             new UserBadge($username, function($username) {
  46.                 $user $this->userRepository->loginBackendGetUser($username);
  47.                 if (!$user) {
  48.                     throw new UserNotFoundException();
  49.                 }
  50.                 return $user;
  51.             }),
  52.             new PasswordCredentials($request->request->get('password''')),
  53.             [
  54.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  55.                 new UserTypeBadge(),
  56.                 new UserStatusBadge(),
  57.                 new RememberMeBadge(),
  58.             ]
  59.         );
  60.     }
  61.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  62.     {
  63.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  64.             return new RedirectResponse($targetPath);
  65.         }
  66.         return new RedirectResponse($this->urlGenerator->generate('app_homepage'));
  67.     }
  68.     protected function getLoginUrl(Request $request): string
  69.     {
  70.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  71.     }
  72. }