src/Security/MainAuthenticator.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\SchoolYear;
  4. use App\Entity\User;
  5. use DateTimeImmutable;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  12. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  13. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  14. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
  18. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\CsrfTokenBadge;
  19. use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
  20. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  21. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  22. class MainAuthenticator extends AbstractLoginFormAuthenticator
  23. {
  24.     use TargetPathTrait;
  25.     public const LOGIN_ROUTE 'app_login';
  26.     private $urlGenerator;
  27.     private $entityManager;
  28.     private $csrfTokenManager;
  29.     public function __construct(EntityManagerInterface $entityManagerUrlGeneratorInterface $urlGeneratorCsrfTokenManagerInterface $csrfTokenManager)
  30.     {
  31.         $this->entityManager $entityManager;
  32.         $this->urlGenerator $urlGenerator;
  33.         $this->csrfTokenManager $csrfTokenManager;
  34.     }
  35.     public function authenticate(Request $request): PassportInterface
  36.     {
  37.         $username $request->request->get('username''');
  38.         $user $this->entityManager->getRepository(User::class)->findOneBy(['username' => $username]);
  39.         if (!$user) {
  40.             // fail authentication with a custom error
  41.             throw new CustomUserMessageAuthenticationException('Accès interdit, utilisateur inconnu.');
  42.         }
  43.         if (!$user->getIsEnabled()) {
  44.             // fail authentication with a custom error
  45.             throw new CustomUserMessageAuthenticationException('Accès interdit, votre compte est inactif.');
  46.         }
  47.         $request->getSession()->set(Security::LAST_USERNAME$username);
  48.         return new Passport(
  49.             new UserBadge($username),
  50.             new PasswordCredentials($request->request->get('password''')),
  51.             [
  52.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  53.             ]
  54.         );
  55.     }
  56.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  57.     {
  58.         /** @var User $user */
  59.         $user $token->getUser();
  60.         $user->setLastLoginAt(new DateTimeImmutable());
  61.         
  62.         $schoolYear $this->entityManager->getRepository(SchoolYear::class)->findOneBy(['is_ongoing' => 1], []);
  63.         if (null == $user->getSchoolYear()) {
  64.             $user->setSchoolYear($schoolYear);
  65.         }
  66.         try {
  67.             $this->entityManager->flush();
  68.         } catch (\Throwable $th) {
  69.             //throw $th;
  70.         }
  71.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  72.             return new RedirectResponse($targetPath);
  73.         }
  74.         // For example:
  75.         return new RedirectResponse($this->urlGenerator->generate('default'));
  76.         //throw new \Exception('TODO: provide a valid redirect inside '.__FILE__);
  77.     }
  78.     protected function getLoginUrl(Request $request): string
  79.     {
  80.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  81.     }
  82. }