package fr.eni.enchere.controllers; import fr.eni.enchere.bo.UserProfil; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.ui.Model; import fr.eni.enchere.bll.UserService; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; 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; import java.util.regex.Pattern; @Controller() @RequestMapping("/profil") public class ProfilController { private final UserService userService; private PasswordEncoder passwordEncoder; public ProfilController(UserService userService, PasswordEncoder passwordEncoder) { this.userService = userService; this.passwordEncoder = passwordEncoder; } @GetMapping public String viewProfile(Model model) { // 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 userProfile = userService.utilisateurByName(username); // 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 "profil"; }else { return "accueil"; } } @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 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 userProfile = userService.utilisateurByName(username); // 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 "editProfil"; }else { return "accueil"; } } @PostMapping("/delete") public String deleteUser() { 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(username); //Supprimer le compte userService.deleteUtilisateur(userProfile.getId()); //ATTENTION AJOUTER LA DECONNEXION 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 } // Vérifier si le mot de passe est sécurisé String passwordRegex = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,}$"; Pattern pattern = Pattern.compile(passwordRegex); // Vérifier si le mot de passe correspond à l'expression régulière if (!pattern.matcher(userProfile.getNewPassword()).matches()){ result.rejectValue("confirmPassword", "invalid", "Le mot de passe ne correspond pas aux critères de sécurité."); return "editProfil"; } // 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"; } } }