update userProfil & logout
This commit is contained in:
@@ -122,7 +122,7 @@ public class UserProfil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String getConfirmPassword() {
|
public String getConfirmPassword() {
|
||||||
return password;
|
return confirmPassword;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setConfirmPassword(String confirmPassword) {
|
public void setConfirmPassword(String confirmPassword) {
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ public class AccueilController {
|
|||||||
|
|
||||||
@PostMapping("/accueil")
|
@PostMapping("/accueil")
|
||||||
public String handleSearch(@RequestParam("searchTitle") String searchTitle, @RequestParam(value = "searchCategory", required = false) Integer searchCategory, Model model) {
|
public String handleSearch(@RequestParam("searchTitle") String searchTitle, @RequestParam(value = "searchCategory", required = false) Integer searchCategory, Model model) {
|
||||||
System.out.println("test");
|
|
||||||
return viewAccueil(searchTitle, searchCategory, model);
|
return viewAccueil(searchTitle, searchCategory, model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,32 +32,7 @@ public class InscriptionController {
|
|||||||
|
|
||||||
@PostMapping("/newUser")
|
@PostMapping("/newUser")
|
||||||
public String setUser(@ModelAttribute("userProfile") UserProfil userProfile, BindingResult result) {
|
public String setUser(@ModelAttribute("userProfile") UserProfil userProfile, BindingResult result) {
|
||||||
// Obtenez l'authentification actuelle
|
userService.setUtilisateur(userProfile);
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
return "redirect:/login";
|
||||||
// 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.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.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(currentUserProfile);
|
|
||||||
return "redirect:/profil"; // Rediriger vers la page de profil après la modification réussie
|
|
||||||
} else {
|
|
||||||
return "accueil";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,6 @@ public class LoginController {
|
|||||||
|
|
||||||
@GetMapping("/logout")
|
@GetMapping("/logout")
|
||||||
public String logout(Model modele) {
|
public String logout(Model modele) {
|
||||||
return "security/logout";
|
return "security/login";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
package fr.eni.enchere.controllers;
|
package fr.eni.enchere.controllers;
|
||||||
|
|
||||||
import fr.eni.enchere.bo.UserProfil;
|
import fr.eni.enchere.bo.UserProfil;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import fr.eni.enchere.bll.UserService;
|
import fr.eni.enchere.bll.UserService;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
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.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
@@ -15,9 +18,12 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
|||||||
public class ProfilController {
|
public class ProfilController {
|
||||||
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
public ProfilController(UserService userService) {
|
|
||||||
|
public ProfilController(UserService userService, PasswordEncoder passwordEncoder) {
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
|
this.passwordEncoder = passwordEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
@@ -76,10 +82,39 @@ public class ProfilController {
|
|||||||
String username = authentication.getName();
|
String username = authentication.getName();
|
||||||
// Utilisez le service approprié pour récupérer les informations de l'utilisateur à partir du nom d'utilisateur
|
// Utilisez le service approprié pour récupérer les informations de l'utilisateur à partir du nom d'utilisateur
|
||||||
UserProfil userProfile = userService.utilisateurByName(username);
|
UserProfil userProfile = userService.utilisateurByName(username);
|
||||||
System.out.println(userProfile.getId());
|
|
||||||
//Supprimer le compte
|
//Supprimer le compte
|
||||||
userService.deleteUtilisateur(userProfile.getId());
|
userService.deleteUtilisateur(userProfile.getId());
|
||||||
//ATTENTION AJOUTER LA DECONNEXION
|
//ATTENTION AJOUTER LA DECONNEXION
|
||||||
return "redirect:/accueil";
|
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";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ public class CategorieRepositoryImpl implements CategorieRepository {
|
|||||||
public List<Categorie> findAllCategories() {
|
public List<Categorie> findAllCategories() {
|
||||||
String sql = "SELECT * FROM CATEGORIES";
|
String sql = "SELECT * FROM CATEGORIES";
|
||||||
List<Categorie> categories = jdbcTemplate.query(sql, new CategorieRowMapper());
|
List<Categorie> categories = jdbcTemplate.query(sql, new CategorieRowMapper());
|
||||||
System.out.println(categories);
|
|
||||||
return categories;
|
return categories;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -96,11 +96,11 @@ public class UserRepositoryImpl implements UserRepository {
|
|||||||
//Mettre à jour
|
//Mettre à jour
|
||||||
String sql = "UPDATE UTILISATEURS SET pseudo = :pseudo, nom = :nom, prenom = :prenom, email = :email, telephone = :telephone, rue = :rue, code_postal = :code_postal, ville = :ville, mot_de_passe = :mot_de_passe WHERE no_utilisateur = :id";
|
String sql = "UPDATE UTILISATEURS SET pseudo = :pseudo, nom = :nom, prenom = :prenom, email = :email, telephone = :telephone, rue = :rue, code_postal = :code_postal, ville = :ville, mot_de_passe = :mot_de_passe WHERE no_utilisateur = :id";
|
||||||
MapSqlParameterSource parameters = new MapSqlParameterSource();
|
MapSqlParameterSource parameters = new MapSqlParameterSource();
|
||||||
if(utilisateur.getPassword().isEmpty()){
|
if(utilisateur.getNewPassword().isEmpty()){
|
||||||
sql = "UPDATE UTILISATEURS SET pseudo = :pseudo, nom = :nom, prenom = :prenom, email = :email, telephone = :telephone, rue = :rue, code_postal = :code_postal, ville = :ville WHERE no_utilisateur = :id";
|
sql = "UPDATE UTILISATEURS SET pseudo = :pseudo, nom = :nom, prenom = :prenom, email = :email, telephone = :telephone, rue = :rue, code_postal = :code_postal, ville = :ville WHERE no_utilisateur = :id";
|
||||||
}else{
|
}else{
|
||||||
sql = "UPDATE UTILISATEURS SET pseudo = :pseudo, nom = :nom, prenom = :prenom, email = :email, telephone = :telephone, rue = :rue, code_postal = :code_postal, ville = :ville, mot_de_passe = :mot_de_passe WHERE no_utilisateur = :id";
|
sql = "UPDATE UTILISATEURS SET pseudo = :pseudo, nom = :nom, prenom = :prenom, email = :email, telephone = :telephone, rue = :rue, code_postal = :code_postal, ville = :ville, mot_de_passe = :mot_de_passe WHERE no_utilisateur = :id";
|
||||||
parameters.addValue("mot_de_passe", passwordEncoder.encode(utilisateur.getPassword()));
|
parameters.addValue("mot_de_passe", passwordEncoder.encode(utilisateur.getNewPassword()));
|
||||||
}
|
}
|
||||||
parameters.addValue("pseudo", utilisateur.getPseudo());
|
parameters.addValue("pseudo", utilisateur.getPseudo());
|
||||||
parameters.addValue("nom", utilisateur.getNom());
|
parameters.addValue("nom", utilisateur.getNom());
|
||||||
@@ -110,7 +110,7 @@ public class UserRepositoryImpl implements UserRepository {
|
|||||||
parameters.addValue("rue", utilisateur.getRue());
|
parameters.addValue("rue", utilisateur.getRue());
|
||||||
parameters.addValue("code_postal", utilisateur.getCode_postal());
|
parameters.addValue("code_postal", utilisateur.getCode_postal());
|
||||||
parameters.addValue("ville", utilisateur.getVille());
|
parameters.addValue("ville", utilisateur.getVille());
|
||||||
parameters.addValue("mot_de_passe", passwordEncoder.encode(utilisateur.getPassword())); // Assurez-vous de hasher le nouveau mot de passe si nécessaire
|
parameters.addValue("mot_de_passe", passwordEncoder.encode(utilisateur.getNewPassword())); // Assurez-vous de hasher le nouveau mot de passe si nécessaire
|
||||||
parameters.addValue("id", utilisateur.getId());
|
parameters.addValue("id", utilisateur.getId());
|
||||||
namedParameterJdbcTemplate.update(sql, parameters);
|
namedParameterJdbcTemplate.update(sql, parameters);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public class WebSecurityConfig{
|
|||||||
.anyRequest().authenticated())
|
.anyRequest().authenticated())
|
||||||
.formLogin((form) -> form.loginPage("/login").defaultSuccessUrl("/", true))
|
.formLogin((form) -> form.loginPage("/login").defaultSuccessUrl("/", true))
|
||||||
.logout((logout) -> logout.clearAuthentication(true).invalidateHttpSession(true)
|
.logout((logout) -> logout.clearAuthentication(true).invalidateHttpSession(true)
|
||||||
.deleteCookies("JSESSIONID").logoutSuccessUrl("/logout")
|
.deleteCookies("JSESSIONID").logoutSuccessUrl("/login")
|
||||||
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll());
|
.logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll());
|
||||||
|
|
||||||
return http.build();
|
return http.build();
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
<body>
|
<body>
|
||||||
<div id="container-main">
|
<div id="container-main">
|
||||||
<h1>Mon profil</h1>
|
<h1>Mon profil</h1>
|
||||||
<form th:action="@{/inscription/newUser}" method="post" th:object="${userProfile}">
|
<form th:action="@{/profil/updateUser}" method="post" th:object="${userProfile}">
|
||||||
<!--<div class="erreur-saisie" th:if="${#fields.hasErrors('*')}" >
|
<!--<div class="erreur-saisie" th:if="${#fields.hasErrors('*')}" >
|
||||||
<p th:text="#{index.erreurs}">Message d'erreur</p>
|
<p th:text="#{index.erreurs}">Message d'erreur</p>
|
||||||
</div>-->
|
</div>-->
|
||||||
@@ -136,7 +136,7 @@
|
|||||||
<div class="champ-saisie">
|
<div class="champ-saisie">
|
||||||
<label for="confirmPassword">Confirmation du nouveau mot de passe: </label>
|
<label for="confirmPassword">Confirmation du nouveau mot de passe: </label>
|
||||||
<div>
|
<div>
|
||||||
<input type="password" th:field="*{confirmPassword}" id="confirmPassword" />
|
<input type="password" th:field="*{confirmPassword}" name="confirmPassword" id="confirmPassword" />
|
||||||
</div>
|
</div>
|
||||||
<span style="color: red;" th:if="${#fields.hasErrors('confirmPassword')}">
|
<span style="color: red;" th:if="${#fields.hasErrors('confirmPassword')}">
|
||||||
<ul>
|
<ul>
|
||||||
|
|||||||
@@ -28,7 +28,7 @@
|
|||||||
<a class="nav-link" href="/profil">Mon profile</a>
|
<a class="nav-link" href="/profil">Mon profile</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item" th:if="${#authorization.expression('hasRole(''MEMBRE'')')}">
|
<li class="nav-item" th:if="${#authorization.expression('hasRole(''MEMBRE'')')}">
|
||||||
<a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true" >Déconnection</a>
|
<a class="nav-link disabled" href="/logout" tabindex="-1" aria-disabled="true" >Déconnection</a>
|
||||||
</li>
|
</li>
|
||||||
<li class="nav-item" th:if="${#authentication.principal == 'anonymousUser'}">
|
<li class="nav-item" th:if="${#authentication.principal == 'anonymousUser'}">
|
||||||
<a class="btn btn-primary" href="/login" role="button" style="background-color: #1B4463;">S'inscrire / Se connecter</a>
|
<a class="btn btn-primary" href="/login" role="button" style="background-color: #1B4463;">S'inscrire / Se connecter</a>
|
||||||
|
|||||||
Reference in New Issue
Block a user