src/Security/UserAppAuthenticator.php line 79

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\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  11. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  12. use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Component\Security\Core\User\UserProviderInterface;
  16. use Symfony\Component\Security\Csrf\CsrfToken;
  17. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  18. use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
  19. use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
  20. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  21. class UserAppAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
  22. {
  23.     use TargetPathTrait;
  24.     public const LOGIN_ROUTE 'app_login';
  25.     private $entityManager;
  26.     private $urlGenerator;
  27.     private $csrfTokenManager;
  28.     private $passwordEncoder;
  29.     /**
  30.      * @var SessionInterface
  31.      */
  32.     private $session;
  33.     public function __construct(EntityManagerInterface $entityManagerSessionInterface $sessionUrlGeneratorInterface $urlGeneratorCsrfTokenManagerInterface $csrfTokenManagerUserPasswordEncoderInterface $passwordEncoder)
  34.     {
  35.         $this->entityManager $entityManager;
  36.         $this->urlGenerator $urlGenerator;
  37.         $this->csrfTokenManager $csrfTokenManager;
  38.         $this->passwordEncoder $passwordEncoder;
  39.         $this->session $session;
  40.     }
  41.     public function supports(Request $request)
  42.     {
  43.         if (self::LOGIN_ROUTE === $request->attributes->get('_route') && $request->get('user') === null && $request->isMethod('POST')) {
  44.             return self::LOGIN_ROUTE === $request->attributes->get('_route');
  45.         }
  46.     }
  47.     public function getCredentials(Request $request)
  48.     {
  49.         $credentials = [
  50.             'email' => $request->request->get('email'),
  51.             'password' => $request->request->get('password'),
  52.             'csrf_token' => $request->request->get('_csrf_token'),
  53.             'objectif' => $request->request->get('objectif'),
  54.         ];
  55.         $request->getSession()->set(
  56.             Security::LAST_USERNAME,
  57.             $credentials['email']
  58.         );
  59.         return $credentials;
  60.     }
  61.     public function getUser($credentialsUserProviderInterface $userProvider)
  62.     {
  63.         $token = new CsrfToken('authenticate'$credentials['csrf_token']);
  64.         if (!$this->csrfTokenManager->isTokenValid($token)) {
  65.             throw new InvalidCsrfTokenException();
  66.         }
  67.         //$this->entityManager->getUnitOfWork()->clear(User::class);
  68.         $user $this->entityManager->getRepository(User::class)->findOneBy(['email' => $credentials['email']]);
  69.         if (!$user) {
  70.             // fail authentication with a custom error
  71.             throw new CustomUserMessageAuthenticationException('Adresse mail non trouvĂ©e.');
  72.         }
  73.         if (!$user->getIsValid()) {
  74.             throw new CustomUserMessageAuthenticationException('Votre compte est en attente de validation.');
  75.         }
  76.         return $user;
  77.     }
  78.     public function checkCredentials($credentialsUserInterface $user)
  79.     {
  80.         return $this->passwordEncoder->isPasswordValid($user$credentials['password']);
  81.     }
  82.     /**
  83.      * Used to upgrade (rehash) the user's password automatically over time.
  84.      */
  85.     public function getPassword($credentials): ?string
  86.     {
  87.         return $credentials['password'];
  88.     }
  89.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey)
  90.     {
  91.         $redirectUrl $request->query->get('redirect'null);
  92.     
  93.         if ($redirectUrl) {
  94.             return new RedirectResponse($redirectUrl);
  95.         }
  96.         $objectif $request->request->get('objectif');
  97.         if ($objectif) {
  98.             $this->session->set('objectif'$objectif);
  99.         }
  100.         if ($this->session->get('commandInCurse')) {
  101.             $this->session->remove('commandInCurse');
  102.             return new RedirectResponse('/commande/livraison');
  103.         }
  104.         if ($token->getUser()->getRoles()[0] === 'ROLE_USER') {
  105.             $user $token->getUser();
  106.             $user->setStatuCon(true);
  107.             $this->entityManager->persist($user);
  108.             $this->entityManager->flush();
  109.             if ($objectif === "experience") {
  110.                 return new RedirectResponse('/reseau-sociaux');
  111.             }
  112.             return new RedirectResponse('/mon-espace-client/dashboard');
  113.         } elseif ($token->getUser()->getRoles()[0] === 'ROLE_ADMIN') {
  114.             return new RedirectResponse('/admin/dashboard');
  115.         } else {
  116.             return new RedirectResponse('/');
  117.         }
  118.     }
  119.     protected function getLoginUrl()
  120.     {
  121.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  122.     }
  123. }