This commit is contained in:
Parpaillax
2024-04-24 10:07:02 +02:00
parent 9dd6febff1
commit 07b47b3f41
5 changed files with 37 additions and 7 deletions

View File

@@ -14,13 +14,15 @@ public class UserProfil {
private String ville;
private String password;
private String confirmPassword;
private String currentPassword;
private String newPassword;
private float credit;
private boolean isAdmin;
//Constructeur
public UserProfil(){}
public UserProfil(int id, String pseudo, String nom, String prenom, String email, String telephone, String rue, String code_postal, String ville, String password, String confirmPassword, int credit, boolean isAdmin) {
public UserProfil(int id, String pseudo, String nom, String prenom, String email, String telephone, String rue, String code_postal, String ville, String password, String confirmPassword, String newPassword, String currentPassword, int credit, boolean isAdmin) {
setId(id);
setPrenom(prenom);
setNom(nom);
@@ -34,6 +36,8 @@ public class UserProfil {
setConfirmPassword(confirmPassword);
setCredit(credit);
setAdmin(isAdmin);
setCurrentPassword(currentPassword);
setNewPassword(newPassword);
}
//Méthode getter et setter
@@ -141,4 +145,19 @@ public class UserProfil {
isAdmin = admin;
}
public String getCurrentPassword() {
return currentPassword;
}
public void setCurrentPassword(String currentPassword) {
this.currentPassword = currentPassword;
}
public String getNewPassword() {
return newPassword;
}
public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
}
}

View File

@@ -41,13 +41,13 @@ public class InscriptionController {
// 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.getPassword(), currentUserProfile.getPassword())) {
if (!passwordEncoder.matches(userProfile.getNewPassword(), 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.getPassword().equals(userProfile.getConfirmPassword())) {
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

View File

@@ -39,6 +39,17 @@ public class ProfilController {
}
}
@GetMapping("/edit")
public String edit(Model model) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (!authentication.getName().equals("anonymousUser")) {
String username = authentication.getName();
UserProfil userProfile = userService.utilisateurByName(username);
model.addAttribute("userProfile", userProfile);
}
return "editProfil";
}
@PostMapping("/edit")
public String editProfile(Model model) {
// Obtenez l'authentification actuelle

View File

@@ -17,9 +17,9 @@ public class WebSecurityConfig{
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/", "/accueil").permitAll()
.requestMatchers("/accueil", "/login", "/inscription/**", "/searchArticle", "/article/**", "/change-language", "/profile/**").permitAll()
.requestMatchers("/accueil", "/login", "/inscription/**", "/searchArticle", "/article/**", "/change-language").permitAll()
.requestMatchers("/css/**", "/images/**", "/assets/**", "/img/**", "/js/**").permitAll()
.requestMatchers("/profile/**").authenticated()
.requestMatchers("/profil/**").authenticated()
.requestMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated())
.formLogin((form) -> form.loginPage("/login").defaultSuccessUrl("/", true))

View File

@@ -110,7 +110,7 @@
<div class="champ-saisie">
<label for="currentPassword">Mot de passe actuel: </label>
<div>
<input type="password" th:field="*{currentPassword}" id="currentPassword" />
<input type="password" th:field="*{currentPassword}" name="currentPassword" id="currentPassword" />
</div>
<span style="color: red;" th:if="${#fields.hasErrors('currentPassword')}">
<ul>
@@ -122,7 +122,7 @@
<div class="champ-saisie">
<label for="newPassword">Nouveau mot de passe: </label>
<div>
<input type="password" th:field="*{newPassword}" id="newPassword" />
<input type="password" th:field="*{newPassword}" name="newPassword" id="newPassword" />
</div>
<span style="color: red;" th:if="${#fields.hasErrors('newPassword')}">
<ul>