gestion de la connexion de l'utilisateur
This commit is contained in:
@@ -2,38 +2,63 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use LogicException;
|
||||
use App\Repository\UserRepository; // 👈 AJOUTE CETTE LIGNE ICI !
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; // 👈 ET CELLE-CI AUSSI !
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
||||
|
||||
class SecurityController extends AbstractController
|
||||
{
|
||||
#[Route(path: '/login', name: 'app_login', methods: ['GET','POST'])]
|
||||
public function login(AuthenticationUtils $authenticationUtils): Response
|
||||
#[Route(path: '/test-password', name: 'test_password')]
|
||||
public function testPassword(UserRepository $userRepo, UserPasswordHasherInterface $hasher): Response
|
||||
{
|
||||
// 1. On va chercher ton admin directement en base
|
||||
$admin = $userRepo->findOneBy(['email' => 'admin@kaz.fr']);
|
||||
|
||||
if (!$admin) {
|
||||
dd("L'utilisateur n'existe pas dans la base lue par ce contrôleur !");
|
||||
}
|
||||
|
||||
// 2. On vérifie si "password" est bien le bon mot de passe
|
||||
$isValid = $hasher->isPasswordValid($admin, 'password');
|
||||
|
||||
// 3. On affiche le résultat brut
|
||||
dd([
|
||||
'email' => $admin->getEmail(),
|
||||
'mot_de_passe_hache' => $admin->getPassword(),
|
||||
'est_ce_que_password_est_valide' => $isValid
|
||||
]);
|
||||
}
|
||||
#[Route(path: '/login', name: 'app_login')]
|
||||
public function login(AuthenticationUtils $authenticationUtils, Request $request): Response // 👈 2. ON AJOUTE $request ICI
|
||||
{
|
||||
// 🚨 3. NOTRE CODE DE DÉTECTIVE POUR VOIR CE QUI EST ENVOYÉ :
|
||||
if ($request->isMethod('POST')) {
|
||||
dd($request->request->all());
|
||||
}
|
||||
// 🚨 FIN DU CODE DE DÉTECTIVE
|
||||
|
||||
// si on a un utilisateur déjà connecté, alors on le redirige sur la page d'accueil
|
||||
if ($this->getUser()) {
|
||||
return $this->redirectToRoute('app_home');
|
||||
}
|
||||
|
||||
// get the login error if there is one
|
||||
// Récupération de l'erreur de connexion (s'il y en a une)
|
||||
$error = $authenticationUtils->getLastAuthenticationError();
|
||||
|
||||
// last username entered by the user
|
||||
// Récupération du dernier nom d'utilisateur saisi par l'adhérent
|
||||
$lastUsername = $authenticationUtils->getLastUsername();
|
||||
|
||||
return $this->render('security/login.html.twig', [
|
||||
'last_username' => $lastUsername,
|
||||
'error' => $error,
|
||||
'error' => $error
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route(path: '/logout', name: 'app_logout', methods: ['POST'])]
|
||||
#[Route(path: '/logout', name: 'app_logout')]
|
||||
public function logout(): void
|
||||
{
|
||||
throw new LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
|
||||
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,14 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Form\ChangePasswordType;
|
||||
use App\Service\KazApiService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
|
||||
@@ -14,9 +19,6 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
|
||||
class UserController extends AbstractController
|
||||
{
|
||||
|
||||
// TODO : UserPasswordHasherInterface
|
||||
// voir : https://symfony.com/doc/current/security/passwords.html#hashing-the-password
|
||||
/**
|
||||
* Permet de vérifier si un utilisateur existe dans le ldap.
|
||||
*
|
||||
@@ -30,6 +32,7 @@ class UserController extends AbstractController
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
|
||||
#[Route('/user/{email}', name: 'app_user', methods: ['GET'])]
|
||||
public function index(string $email, KazApiService $apiClient): Response
|
||||
{
|
||||
@@ -39,4 +42,45 @@ class UserController extends AbstractController
|
||||
'exist' => $exist,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/user/mot-de-passe', name: 'app_user_edit_password', methods: ['GET', 'POST'])]
|
||||
public function editPassword(Request $request, UserPasswordHasherInterface $hasher, EntityManagerInterface $entityManager): Response
|
||||
{
|
||||
# Récupération de l'adhérent actuellement connecté
|
||||
$user = $this->getUser();
|
||||
|
||||
# Création du formulaire
|
||||
$form = $this->createForm(ChangePasswordType::class);
|
||||
|
||||
# Liaison du formulaire à la requête HTTP
|
||||
$form->handleRequest($request);
|
||||
|
||||
# Vérification du formulaire, s'il est bien soumis et valide
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
# Récupération des données du formulaire
|
||||
$user = $this->getUser();
|
||||
$plainOldPassword = $form->get('oldPassword')->getData();
|
||||
$newPassword = $form->get('newPassword')->getData();
|
||||
|
||||
# Vérification de l'ancien mot de passe
|
||||
if (!$hasher->isPasswordValid($user, $plainOldPassword)) {
|
||||
$form->get('oldPassword')->addError(new FormError('L\'ancien mot de passe est incorrect.'));
|
||||
} else {
|
||||
# Si tout est OK : Hachage du mot de passe
|
||||
$hashedPassword = $hasher->hashPassword($user, $newPassword);
|
||||
$user->setPassword($hashedPassword);
|
||||
|
||||
# Sauvegarde en BDD
|
||||
$entityManager->flush();
|
||||
|
||||
# Message de succès pour l'adhérent
|
||||
$this->addFlash('success', 'Votre mot de passe a bien été mis à jour !');
|
||||
|
||||
return $this->redirectToRoute('app_user_edit_password');
|
||||
}
|
||||
}
|
||||
return $this->render('user/edit_password.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user