src/Security/LoginFormAuthenticator.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\RouterInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  11. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  12. use Symfony\Component\Security\Core\Security;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. use Symfony\Component\Security\Core\User\UserProviderInterface;
  15. use Symfony\Component\Security\Csrf\CsrfToken;
  16. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  17. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  18. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  19. class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
  20. {
  21.     use TargetPathTrait;
  22.     private $entityManager;
  23.     private $router;
  24.     private $csrfTokenManager;
  25.     private $passwordEncoder;
  26.     public function __construct(EntityManagerInterface $entityManagerRouterInterface $routerCsrfTokenManagerInterface $csrfTokenManagerUserPasswordEncoderInterface $passwordEncoder)
  27.     {
  28.         $this->entityManager $entityManager;
  29.         $this->router $router;
  30.         $this->csrfTokenManager $csrfTokenManager;
  31.         $this->passwordEncoder $passwordEncoder;
  32.     }
  33.     public function supports(Request $request)
  34.     {
  35.         return 'security_login' === $request->attributes->get('_route')
  36.             && $request->isMethod('POST');
  37.     }
  38.     public function getCredentials(Request $request)
  39.     {
  40.         $credentials = [
  41.             'username' => $request->request->get('username'),
  42.             'password' => $request->request->get('password'),
  43.             'csrf_token' => $request->request->get('_csrf_token'),
  44.         ];
  45.         $request->getSession()->set(
  46.             Security::LAST_USERNAME,
  47.             $credentials['username']
  48.         );
  49.         return $credentials;
  50.     }
  51.     public function getUser($credentialsUserProviderInterface $userProvider)
  52.     {
  53.         $token = new CsrfToken('authenticate'$credentials['csrf_token']);
  54.         if (!$this->csrfTokenManager->isTokenValid($token)) {
  55.             throw new InvalidCsrfTokenException();
  56.         }
  57.         $user_result $this->entityManager->getRepository(User::class)->findByUsernameOrEmail($credentials['username']);
  58.         if(empty($user_result)) {
  59.             throw new CustomUserMessageAuthenticationException("Cette adresse email est invalide.");
  60.         }
  61.         return $user_result[0];
  62.     }
  63.     public function checkCredentials($credentialsUserInterface $user)
  64.     {
  65.         if($user->isActive() == false) {
  66.             throw new CustomUserMessageAuthenticationException("Ce compte est dĂ©sactivĂ©");
  67.         }
  68.         return $this->passwordEncoder->isPasswordValid($user$credentials['password']);
  69.     }
  70.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey)
  71.     {
  72. //        if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
  73. //            return new RedirectResponse($targetPath);
  74. //        }
  75.         return new RedirectResponse($this->router->generate('security_dispatch_by_role'));
  76.     }
  77.     protected function getLoginUrl()
  78.     {
  79.         return $this->router->generate('security_login');
  80.     }
  81. }