From 802fcdd8ead39534f965ed9b731b432144f9a15d Mon Sep 17 00:00:00 2001 From: jleroy Date: Wed, 24 Apr 2024 09:23:55 +0200 Subject: [PATCH 1/3] patch session utilisateur v2 --- .../java/fr/eni/enchere/bo/UserProfil.java | 14 ++++++- .../controllers/InscriptionController.java | 38 ++++++++++++++--- src/main/resources/templates/editProfil.html | 41 +++++++++++-------- 3 files changed, 69 insertions(+), 24 deletions(-) diff --git a/src/main/java/fr/eni/enchere/bo/UserProfil.java b/src/main/java/fr/eni/enchere/bo/UserProfil.java index 4c17b05..9677c64 100644 --- a/src/main/java/fr/eni/enchere/bo/UserProfil.java +++ b/src/main/java/fr/eni/enchere/bo/UserProfil.java @@ -12,14 +12,15 @@ public class UserProfil { private String rue; private String code_postal; private String ville; - private String password; //Voir la sécurité du mot de passe + private String password; + private String confirmPassword; 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, 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, int credit, boolean isAdmin) { setId(id); setPrenom(prenom); setNom(nom); @@ -30,6 +31,7 @@ public class UserProfil { setCode_postal(code_postal); setVille(ville); setPassword(password); + setConfirmPassword(confirmPassword); setCredit(credit); setAdmin(isAdmin); } @@ -115,6 +117,14 @@ public class UserProfil { this.password = password; } + public String getConfirmPassword() { + return password; + } + + public void setConfirmPassword(String confirmPassword) { + this.confirmPassword = confirmPassword; + } + public float getCredit() { return credit; } diff --git a/src/main/java/fr/eni/enchere/controllers/InscriptionController.java b/src/main/java/fr/eni/enchere/controllers/InscriptionController.java index fadce9f..2e9c9a6 100644 --- a/src/main/java/fr/eni/enchere/controllers/InscriptionController.java +++ b/src/main/java/fr/eni/enchere/controllers/InscriptionController.java @@ -1,6 +1,9 @@ package fr.eni.enchere.controllers; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.validation.BindingResult; import fr.eni.enchere.bll.UserService; import fr.eni.enchere.bo.UserProfil; @@ -14,9 +17,11 @@ public class InscriptionController { @Autowired private final UserService userService; + private PasswordEncoder passwordEncoder; - public InscriptionController(UserService userService) { + public InscriptionController(UserService userService, PasswordEncoder passwordEncoder) { this.userService = userService; + this.passwordEncoder = passwordEncoder; } @GetMapping @@ -26,10 +31,33 @@ public class InscriptionController { } @PostMapping("/newUser") - public String setUser(@ModelAttribute UserProfil user) { - //Ajouter vérification du formulaire -> @RequestParam("confirmPassword") String confirmPassword - userService.setUtilisateur(user); - return "redirect:/accueil"; + 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.getPassword(), 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())) { + // 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"; + } } } diff --git a/src/main/resources/templates/editProfil.html b/src/main/resources/templates/editProfil.html index ca6c711..cf3aa04 100644 --- a/src/main/resources/templates/editProfil.html +++ b/src/main/resources/templates/editProfil.html @@ -106,34 +106,41 @@ - +
- +
- +
- -
    -
  • -
-
+ +
    +
  • +
+
+
- +
- +
- -
    -
  • -
-
+ +
    +
  • +
+
+
- +
- +
+ +
    +
  • +
+
Crédits:
From a9061f4d9b3a7820b9b13f46570259a262623f0d Mon Sep 17 00:00:00 2001 From: jleroy Date: Wed, 24 Apr 2024 09:37:33 +0200 Subject: [PATCH 2/3] patch web security --- src/main/java/fr/eni/enchere/security/WebSecurityConfig.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/fr/eni/enchere/security/WebSecurityConfig.java b/src/main/java/fr/eni/enchere/security/WebSecurityConfig.java index 076a4fd..d08bf58 100644 --- a/src/main/java/fr/eni/enchere/security/WebSecurityConfig.java +++ b/src/main/java/fr/eni/enchere/security/WebSecurityConfig.java @@ -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/**", "/article/new/**", "/article/update", "/article/delete").authenticated() .requestMatchers("/admin").hasRole("ADMIN") .anyRequest().authenticated()) .formLogin((form) -> form.loginPage("/login").defaultSuccessUrl("/", true)) From 9281a2984f3f1d9204e58a2d886d14d7a1462c13 Mon Sep 17 00:00:00 2001 From: jleroy Date: Wed, 24 Apr 2024 10:08:13 +0200 Subject: [PATCH 3/3] Ajout article v1 --- .../controllers/ArticleController.java | 14 ++-- src/main/resources/templates/newArticle.html | 70 +++++++++++++++++++ 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/src/main/java/fr/eni/enchere/controllers/ArticleController.java b/src/main/java/fr/eni/enchere/controllers/ArticleController.java index 6b9ef13..0e2b5d3 100644 --- a/src/main/java/fr/eni/enchere/controllers/ArticleController.java +++ b/src/main/java/fr/eni/enchere/controllers/ArticleController.java @@ -1,6 +1,7 @@ package fr.eni.enchere.controllers; import fr.eni.enchere.bll.ArticleService; +import fr.eni.enchere.bll.CategorieService; import fr.eni.enchere.bll.UserService; import fr.eni.enchere.bo.Article; import org.springframework.stereotype.Controller; @@ -13,10 +14,12 @@ public class ArticleController { private final ArticleService articleService; private final UserService userService; + private CategorieService categorieService; - public ArticleController(ArticleService articleService, UserService userService) { + public ArticleController(ArticleService articleService, UserService userService, CategorieService categorieService) { this.articleService = articleService; this.userService = userService; + this.categorieService = categorieService; } @GetMapping @@ -24,7 +27,8 @@ public class ArticleController { return "accueil"; } - @GetMapping("/article") + //Affichage d'un article + @GetMapping("/show") public String showArticle(@RequestParam(name = "slug")int id, Model model) { Article article = articleService.findArticleById(id); return "article"; @@ -39,14 +43,14 @@ public class ArticleController { @GetMapping("/new") public String test(@PathVariable(name = "slug")int id, Model model) { - - return "article"; + model.addAttribute("categories", categorieService.findAllCategories()); + return "newArticle"; } @PostMapping("/new/add") public String newArticle(@ModelAttribute("article") Article article) { articleService.saveArticle(article); - return "redirect:/article"; + return "redirect:/accueil"; } @PostMapping("/update") diff --git a/src/main/resources/templates/newArticle.html b/src/main/resources/templates/newArticle.html index 3b51fbd..50c3ea8 100644 --- a/src/main/resources/templates/newArticle.html +++ b/src/main/resources/templates/newArticle.html @@ -5,7 +5,77 @@
+

Nouvelle vente

+
+ +
+ + +
+ +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +
+ + +

Retrait

+
+ + +
+
+ + +
+
+ + +
+ + +
+ +
+
+
+ +