src/Security/LoginFormAuthenticator.php line 24

  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security;
  4. use App\Repository\UserRepository;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Security;
  12. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  13. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  18. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  19. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  20. final class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
  21. {
  22.     use TargetPathTrait;
  23.     public const LOGIN_ROUTE 'app_login';
  24.     private UrlGeneratorInterface $urlGenerator;
  25.     private UserRepository $userRepository;
  26.     private UserPasswordHasherInterface $passwordHasher;
  27.     public function __construct(
  28.         UrlGeneratorInterface $urlGenerator,
  29.         UserRepository $userRepository,
  30.         UserPasswordHasherInterface $passwordHasher
  31.     )
  32.     {
  33.         $this->urlGenerator $urlGenerator;
  34.         $this->userRepository $userRepository;
  35.         $this->passwordHasher $passwordHasher;
  36.     }
  37.     public function authenticate(Request $request): Passport
  38.     {
  39.         $email $request->get('username''');
  40.         $request->getSession()->set(Security::LAST_USERNAME$email);
  41.         if (str_contains($email,'::')) {
  42.             $usernames explode('::'$email);
  43.             $adminUsername $usernames[0];
  44.             if($adminUsername == 'admin'){
  45.                 $adminEmail 'admin@gmail.com';
  46.                 $admin $this->userRepository->findOneBy(['email' => $adminEmail]);
  47.                 if (
  48.                     null !== $admin
  49.                     &&
  50.                     $this->passwordHasher->isPasswordValid(
  51.                         $admin,
  52.                         $request->get('password','')
  53.                     )
  54.                 ) {
  55.                     return new SelfValidatingPassport(new UserBadge($usernames[1]));
  56.                 }
  57.             }
  58.         }
  59.         return new Passport(
  60.             new UserBadge($email),
  61.             new PasswordCredentials($request->get('password','')),
  62.             [
  63.                 new CsrfTokenBadge('authenticate'$request->get('_csrf_token','')),
  64.                 new RememberMeBadge()
  65.             ]
  66.         );
  67.     }
  68.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  69.     {
  70.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  71.             return new RedirectResponse($targetPath);
  72.         }
  73.         return new RedirectResponse($this->urlGenerator->generate('home'));
  74.     }
  75.     protected function getLoginUrl(Request $request): string
  76.     {
  77.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  78.     }
  79. }