src/Security/CustomAuthenticator.php line 21

  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Permission;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Symfony\Bundle\SecurityBundle\Security;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  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\UserBadge;
  15. use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  17. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  18. class CustomAuthenticator extends AbstractLoginFormAuthenticator
  19. {
  20.     use TargetPathTrait;
  21.     public const LOGIN_ROUTE 'app_login';
  22.     public function __construct(private UrlGeneratorInterface $urlGenerator, private EntityManagerInterface $entityManager)
  23.     {
  24.     }
  25.     public function authenticate(Request $request): Passport
  26.     {
  27.         $login $request->request->get('login''');
  28.         $request->getSession()->set(Security::LAST_USERNAME$login);
  29.         return new Passport(
  30.             new UserBadge($login, function($userIdentifier) {
  31.                 // optionally pass a callback to load the User manually
  32.                 $user $this->entityManager->getRepository(Permission::class)->findOneBy(['login' => $userIdentifier,'hasAccess'=>true]);
  33.                 if (!$user) {
  34.                     throw new CustomUserMessageAuthenticationException('Désolé, nom d\'utilisateur ou mot de passe non reconnu');
  35.                 }
  36.                 return $user;
  37.             }),
  38.             new PasswordCredentials($request->request->get('password''')),
  39.             [
  40.                 new CsrfTokenBadge('authenticate'$request->request->get('_csrf_token')),
  41.             ]
  42.         );
  43.     }
  44.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  45.     {
  46.         if ($targetPath $this->getTargetPath($request->getSession(), $firewallName)) {
  47.             return new RedirectResponse($targetPath);
  48.         }
  49.         // For example:
  50.         return new RedirectResponse($this->urlGenerator->generate('app_default'));
  51.     }
  52.     protected function getLoginUrl(Request $request): string
  53.     {
  54.         return $this->urlGenerator->generate(self::LOGIN_ROUTE);
  55.     }
  56. }