1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| <?php
// src/Security/CustomAuthenticator.php
namespace App\Security;
use App\Entity\AuthLogin;
use App\Entity\User;
use App\Repository\UserRepository;
use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Http\Authenticator\Token\PostAuthenticationToken;
class LocalAuthenticator extends AbstractAuthenticator
{
public const LOGIN_ROUTE = "security_login";
public function __construct(
private UserRepository $userRepository, private UrlGeneratorInterface $urlGenerator, private EntityManagerInterface $em,
) {
}
public function supports(Request $request): bool
{
return self::LOGIN_ROUTE === $request->attributes->get('_route')
&& $request->isMethod('POST');
}
public function authenticate(Request $request): Passport
{
$username = $request->request->get('_username');
$password = $request->request->get('_password');
// Vérifiez d'abord dans le fournisseur de mémoire
/*dd($this->userProvider);
$user = $this->userProvider->loadUserByUsername($username);
if ($user instanceof UserInterface && $this->passwordEncoder->isPasswordValid($user, $password)) {
return new Passport(new UserBadge($username), new PasswordCredentials($password));
}*/
$user = $this->em->getRepository(User::class)->findOneBy(['username' => $username]);
if (!$user)
{
throw new CustomUserMessageAuthenticationException('[L] Identifiant incorrect');
}
$userBadge = new UserBadge($username, function () use ($user) {
return $user;
});
return new Passport($userBadge , new PasswordCredentials($password));
}
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
{
$user = $passport->getUser();
$roles = $passport->getUser()->getRoles();
$authenticatedToken = new PostAuthenticationToken($user, $firewallName, $roles);
return $authenticatedToken;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
{
$auth = new AuthLogin($request->getClientIp(),$token->getUser()->getUsername(), AuthLogin::$success, "");
$auth->setUser($token->getUser());
$this->em->persist($auth);
$this->em->flush();
return new RedirectResponse($this->urlGenerator->generate(self::LOGIN_ROUTE));
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception): Response
{
$auth = new AuthLogin($request->getClientIp(), $request->request->get('_username'), AuthLogin::$failed, $exception->getMessage());
$this->em->persist($auth);
$this->em->flush();
$response = new Response($exception->getMessage(), Response::HTTP_UNAUTHORIZED);
return $response;
}
protected function getLoginUrl(Request $request): stvring
{
return $this->urlGenerator->generate('security_login');
} |
Partager