Merge branch 'refs/heads/main' into marvin

# Conflicts:
#	src/main/java/fr/eni/enchere/security/WebSecurityConfig.java
This commit is contained in:
mepiphana2023
2024-04-24 11:20:24 +02:00
6 changed files with 41 additions and 14 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

@@ -1,8 +1,6 @@
package fr.eni.enchere.dal;
import fr.eni.enchere.bo.Article;
import fr.eni.enchere.bo.UserProfil;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -16,9 +14,7 @@ import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
@Primary
@@ -59,8 +55,8 @@ public class UserRepositoryImpl implements UserRepository {
@Override
public UserProfil findByUsername(String username) {
String sql = "SELECT * FROM UTILISATEURS WHERE pseudo = ? OR email = ? AND isDelete = 0";
UserProfil useruser = jdbcTemplate.queryForObject(sql, new UserRowMapper(), username, username);
return useruser;
UserProfil user = jdbcTemplate.queryForObject(sql, new UserRowMapper(), username, username);
return user;
}
@@ -98,7 +94,7 @@ public class UserRepositoryImpl implements UserRepository {
}
}else {
//Mettre à jour
String sql;
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();
if(utilisateur.getPassword().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";
@@ -114,6 +110,7 @@ public class UserRepositoryImpl implements UserRepository {
parameters.addValue("rue", utilisateur.getRue());
parameters.addValue("code_postal", utilisateur.getCode_postal());
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("id", utilisateur.getId());
namedParameterJdbcTemplate.update(sql, parameters);
}

View File

@@ -18,8 +18,8 @@ public class WebSecurityConfig{
http.authorizeHttpRequests((requests) -> requests
.requestMatchers("/", "/accueil").permitAll()
.requestMatchers("/accueil", "/login", "/inscription/**", "/searchArticle", "/article/**", "/change-language").permitAll()
.requestMatchers("/css/**", "/images/**", "/assets/**", "/img/**", "/js/**").permitAll()
.requestMatchers("/profil/**", "/article/new/**").authenticated()
.requestMatchers("/css/**", "/images/**", "/assets/**", "/img/**", "/js/**", "/assets/**").permitAll()
.requestMatchers("/profil/**", "/article/new/**", "/article/update", "/article/delete").authenticated()
.requestMatchers("/admin").hasRole("ADMIN")
.anyRequest().authenticated())
.formLogin((form) -> form.loginPage("/login").defaultSuccessUrl("/", true))