update userProfil & logout
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package fr.eni.enchere.controllers;
|
||||
|
||||
import fr.eni.enchere.bo.UserProfil;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.ui.Model;
|
||||
import fr.eni.enchere.bll.UserService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.security.core.Authentication;
|
||||
@@ -15,9 +18,12 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
public class ProfilController {
|
||||
|
||||
private final UserService userService;
|
||||
private PasswordEncoder passwordEncoder;
|
||||
|
||||
public ProfilController(UserService userService) {
|
||||
|
||||
public ProfilController(UserService userService, PasswordEncoder passwordEncoder) {
|
||||
this.userService = userService;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@@ -76,10 +82,39 @@ public class ProfilController {
|
||||
String username = authentication.getName();
|
||||
// Utilisez le service approprié pour récupérer les informations de l'utilisateur à partir du nom d'utilisateur
|
||||
UserProfil userProfile = userService.utilisateurByName(username);
|
||||
System.out.println(userProfile.getId());
|
||||
//Supprimer le compte
|
||||
userService.deleteUtilisateur(userProfile.getId());
|
||||
//ATTENTION AJOUTER LA DECONNEXION
|
||||
return "redirect:/accueil";
|
||||
}
|
||||
|
||||
@PostMapping("/updateUser")
|
||||
public String setUser(@ModelAttribute("userProfile") UserProfil userProfile, BindingResult result) {
|
||||
// Obtenez l'authentification actuelle
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
// Vérifiez si l'utilisateur est authentifié
|
||||
if (!authentication.getName().equals("anonymousUser")) {
|
||||
// Obtenez les détails de l'utilisateur authentifié
|
||||
String username = authentication.getName();
|
||||
// Utilisez le service approprié pour récupérer les informations de l'utilisateur à partir du nom d'utilisateur
|
||||
UserProfil currentUserProfile = userService.utilisateurByName(username);
|
||||
// Vérifiez si le mot de passe actuel correspond à celui stocké dans la base de données
|
||||
if (!passwordEncoder.matches(userProfile.getCurrentPassword(), currentUserProfile.getPassword())) {
|
||||
// Mot de passe actuel incorrect, renvoyer une erreur
|
||||
result.rejectValue("currentPassword", "invalid", "Mot de passe actuel incorrect");
|
||||
return "editProfil"; // Rediriger vers la page de modification du profil avec une erreur
|
||||
}
|
||||
// Vérifiez si le nouveau mot de passe et sa confirmation correspondent
|
||||
if (!userProfile.getNewPassword().equals(userProfile.getConfirmPassword())) {
|
||||
// Mauvaise correspondance entre le nouveau mot de passe et sa confirmation, renvoyer une erreur
|
||||
result.rejectValue("confirmPassword", "invalid", "La confirmation du mot de passe ne correspond pas au nouveau mot de passe");
|
||||
return "editProfil"; // Rediriger vers la page de modification du profil avec une erreur
|
||||
}
|
||||
// Mettez à jour le mot de passe de l'utilisateur avec le nouveau mot de passe
|
||||
userService.setUtilisateur(userProfile);
|
||||
return "redirect:/profil"; // Rediriger vers la page de profil après la modification réussie
|
||||
} else {
|
||||
return "accueil";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user