From 3ca27ce33d050b652a8258cddf2023cbc12a8965 Mon Sep 17 00:00:00 2001 From: jleroy Date: Tue, 23 Apr 2024 14:44:43 +0200 Subject: [PATCH] =?UTF-8?q?Update=20permission=20acc=C3=A8s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/fr/eni/enchere/bll/UserService.java | 3 +- .../fr/eni/enchere/bll/UserServiceImpl.java | 7 +- .../controllers/InscriptionController.java | 9 +- .../controllers/ProfileController.java | 36 ++++- .../eni/enchere/dal/UserRepositoryImpl.java | 20 ++- .../enchere/security/WebSecurityConfig.java | 8 +- src/main/resources/templates/inscription.html | 13 +- src/main/resources/templates/profile.html | 144 +++++++++++++++--- 8 files changed, 202 insertions(+), 38 deletions(-) diff --git a/src/main/java/fr/eni/enchere/bll/UserService.java b/src/main/java/fr/eni/enchere/bll/UserService.java index b3b55ec..1a258d1 100644 --- a/src/main/java/fr/eni/enchere/bll/UserService.java +++ b/src/main/java/fr/eni/enchere/bll/UserService.java @@ -6,7 +6,8 @@ import java.util.List; public interface UserService { List listeUtilisateurs(); - UserProfil utilisateur(int id); + UserProfil utilisateurById(int id); + UserProfil utilisateurByName(String username); void setUtilisateur(UserProfil utilisateur); void deleteUtilisateur(int id); } diff --git a/src/main/java/fr/eni/enchere/bll/UserServiceImpl.java b/src/main/java/fr/eni/enchere/bll/UserServiceImpl.java index dec1e94..9938c0e 100644 --- a/src/main/java/fr/eni/enchere/bll/UserServiceImpl.java +++ b/src/main/java/fr/eni/enchere/bll/UserServiceImpl.java @@ -21,10 +21,15 @@ public class UserServiceImpl implements UserService { } @Override - public UserProfil utilisateur(int id) { + public UserProfil utilisateurById(int id) { return userRepository.findById(id); } + @Override + public UserProfil utilisateurByName(String username) { + return userRepository.findByUsername(username); + } + @Override public void setUtilisateur(UserProfil utilisateur) { userRepository.save(utilisateur); diff --git a/src/main/java/fr/eni/enchere/controllers/InscriptionController.java b/src/main/java/fr/eni/enchere/controllers/InscriptionController.java index c761c2f..fadce9f 100644 --- a/src/main/java/fr/eni/enchere/controllers/InscriptionController.java +++ b/src/main/java/fr/eni/enchere/controllers/InscriptionController.java @@ -1,18 +1,18 @@ package fr.eni.enchere.controllers; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.BindingResult; import fr.eni.enchere.bll.UserService; import fr.eni.enchere.bo.UserProfil; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -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.web.bind.annotation.*; @Controller @RequestMapping("/inscription") public class InscriptionController { + @Autowired private final UserService userService; public InscriptionController(UserService userService) { @@ -27,6 +27,7 @@ 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"; } diff --git a/src/main/java/fr/eni/enchere/controllers/ProfileController.java b/src/main/java/fr/eni/enchere/controllers/ProfileController.java index db3876c..392eccd 100644 --- a/src/main/java/fr/eni/enchere/controllers/ProfileController.java +++ b/src/main/java/fr/eni/enchere/controllers/ProfileController.java @@ -1,11 +1,16 @@ package fr.eni.enchere.controllers; import fr.eni.enchere.bo.UserProfil; +import fr.eni.enchere.dal.UserRepository; import org.springframework.ui.Model; import fr.eni.enchere.bll.UserService; import org.springframework.stereotype.Controller; 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; +import org.springframework.security.core.context.SecurityContextHolder; @Controller() @RequestMapping("/profile") @@ -19,7 +24,34 @@ public class ProfileController { @GetMapping public String viewProfile(Model model) { - model.addAttribute("user", new UserProfil()); - return "profile"; + // Obtenez l'authentification actuelle + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + // Vérifiez si l'utilisateur est authentifié + if (!authentication.getName().equals("anonymousUser") || true) { //Retirer le true pour le bon fonctionnement + // 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 userProfile = userService.utilisateurByName("Jojo"); + // Ajoutez les informations du profil à l'objet Model pour les afficher dans la page HTML + model.addAttribute("user", new UserProfil()); + model.addAttribute("userProfile", userProfile); + return "profile"; + }else { + return "accueil"; + } + } + + @PostMapping("/delete") + public String setUser() { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + // 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 userProfile = userService.utilisateurByName("Jojo"); + System.out.println(userProfile.getId()); + //Supprimer le compte + userService.deleteUtilisateur(userProfile.getId()); + //ATTENTION AJOUTER LA DECONNEXION + return "redirect:/accueil"; } } diff --git a/src/main/java/fr/eni/enchere/dal/UserRepositoryImpl.java b/src/main/java/fr/eni/enchere/dal/UserRepositoryImpl.java index b0fcda9..e99582a 100644 --- a/src/main/java/fr/eni/enchere/dal/UserRepositoryImpl.java +++ b/src/main/java/fr/eni/enchere/dal/UserRepositoryImpl.java @@ -43,7 +43,7 @@ public class UserRepositoryImpl implements UserRepository { @Override public UserProfil findByUsername(String username) { - String sql = "SELECT * FROM UTILISATEURS WHERE pseudo = :username OR email = :username"; + String sql = "SELECT * FROM UTILISATEURS WHERE pseudo = :username OR email = :username AND isDelete = 0"; Map params = new HashMap<>(); params.put("username", username); UserProfil user = namedParameterJdbcTemplate.queryForObject(sql, params, (rs, rowNum) -> { @@ -69,7 +69,7 @@ public class UserRepositoryImpl implements UserRepository { public void save(UserProfil utilisateur) { if (utilisateur.getId() == 0) { //Création utilisateur - String sql = "INSERT INTO UTILISATEURS (pseudo, nom, prenom, email, telephone, rue, code_postal, ville, mot_de_passe, credit, administrateur) VALUES (:pseudo, :nom, :prenom, :email, :telephone, :rue, :code_postal, :ville, :mot_de_passe, 0, false)"; + String sql = "INSERT INTO UTILISATEURS (pseudo, nom, prenom, email, telephone, rue, code_postal, ville, mot_de_passe, credit, administrateur, isDelete) VALUES (:pseudo, :nom, :prenom, :email, :telephone, :rue, :code_postal, :ville, :mot_de_passe, 0, false, false)"; MapSqlParameterSource parameters = new MapSqlParameterSource(); parameters.addValue("pseudo", utilisateur.getPseudo()); parameters.addValue("nom", utilisateur.getNom()); @@ -87,11 +87,25 @@ public class UserRepositoryImpl implements UserRepository { } }else { //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"; + MapSqlParameterSource parameters = new MapSqlParameterSource(); + parameters.addValue("pseudo", utilisateur.getPseudo()); + parameters.addValue("nom", utilisateur.getNom()); + parameters.addValue("prenom", utilisateur.getPrenom()); + parameters.addValue("email", utilisateur.getEmail()); + parameters.addValue("telephone", utilisateur.getTelephone()); + 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); } } @Override public void delete(int id) { - + String sql = "UPDATE UTILISATEURS SET isDelete = 1 WHERE no_utilisateur = ?"; + jdbcTemplate.update(sql, id); } } diff --git a/src/main/java/fr/eni/enchere/security/WebSecurityConfig.java b/src/main/java/fr/eni/enchere/security/WebSecurityConfig.java index 8a2bbbd..c8a3ee5 100644 --- a/src/main/java/fr/eni/enchere/security/WebSecurityConfig.java +++ b/src/main/java/fr/eni/enchere/security/WebSecurityConfig.java @@ -13,15 +13,15 @@ public class WebSecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { - http.authorizeHttpRequests((requests) -> requests.requestMatchers("/", "/accueil").permitAll() - .requestMatchers("/accueil", "/login", "/inscription", "/searchArticle").permitAll() + http.authorizeHttpRequests((requests) -> requests + .requestMatchers("/", "/accueil").permitAll() + .requestMatchers("/accueil", "/login", "/inscription/**", "/searchArticle", "/profile/**").permitAll() .requestMatchers("/css/**", "/images/**", "/assets/**", "/img/**", "/js/**").permitAll() - .requestMatchers("/profile").hasAnyRole("MEMBRE", "ADMIN") .requestMatchers("/admin").hasRole("ADMIN") .anyRequest().authenticated()) .formLogin((form) -> form.loginPage("/login").defaultSuccessUrl("/", true)) .logout((logout) -> logout.clearAuthentication(true).invalidateHttpSession(true) - .deleteCookies("JSESSIONID").logoutSuccessUrl("/filmLogout") + .deleteCookies("JSESSIONID").logoutSuccessUrl("/logout") .logoutRequestMatcher(new AntPathRequestMatcher("/logout")).permitAll()); return http.build(); diff --git a/src/main/resources/templates/inscription.html b/src/main/resources/templates/inscription.html index 9c8bbb3..33b34c7 100644 --- a/src/main/resources/templates/inscription.html +++ b/src/main/resources/templates/inscription.html @@ -14,7 +14,7 @@
- +
    @@ -106,7 +106,6 @@
-
@@ -119,14 +118,16 @@
- +
- +
- + + +
+
- Annuler
diff --git a/src/main/resources/templates/profile.html b/src/main/resources/templates/profile.html index 4793090..7ba1a51 100644 --- a/src/main/resources/templates/profile.html +++ b/src/main/resources/templates/profile.html @@ -5,23 +5,133 @@ Mon profile -
-
-

Mon profile

-
-
-
-
-
+
+

Mon profil

+ + + +
+ +
+
- -
-
-
-
-
+ +
    +
  • +
+
+
+ +
+ +
+
- -
+ +
    +
  • +
+
+
+ +
+ +
+ +
+ +
    +
  • +
+
+
+ +
+ +
+ +
+ +
    +
  • +
+
+
+ +
+ +
+ +
+ +
    +
  • +
+
+
+ +
+ +
+ +
+ +
    +
  • +
+
+
+ +
+ +
+ +
+ +
    +
  • +
+
+
+ +
+ +
+ +
+ +
    +
  • +
+
+
+ +
+ +
+ +
+ +
    +
  • +
+
+
+
+ +
+ +
+
+
Crédits:
+ + + +
+ +
+
- \ No newline at end of file +