From 7513587b7659ca4e22b4e8c5c48da4289c58f67b Mon Sep 17 00:00:00 2001 From: jleroy Date: Thu, 25 Apr 2024 14:54:23 +0200 Subject: [PATCH 1/2] Correction validation all formulaire version final --- .../controllers/ArticleController.java | 105 +++++++++++++++++- .../controllers/InscriptionController.java | 33 +++++- .../enchere/controllers/LoginController.java | 7 ++ .../enchere/controllers/ProfilController.java | 10 ++ src/main/resources/templates/newArticle.html | 7 +- 5 files changed, 156 insertions(+), 6 deletions(-) diff --git a/src/main/java/fr/eni/enchere/controllers/ArticleController.java b/src/main/java/fr/eni/enchere/controllers/ArticleController.java index 491a289..9445c8f 100644 --- a/src/main/java/fr/eni/enchere/controllers/ArticleController.java +++ b/src/main/java/fr/eni/enchere/controllers/ArticleController.java @@ -1,5 +1,6 @@ package fr.eni.enchere.controllers; +import com.fasterxml.jackson.databind.JsonNode; import fr.eni.enchere.bll.ArticleService; import fr.eni.enchere.bll.CategorieService; import fr.eni.enchere.bll.RetraitService; @@ -9,21 +10,31 @@ import fr.eni.enchere.bo.Categorie; import fr.eni.enchere.bo.Retrait; import fr.eni.enchere.bo.UserProfil; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; +import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.util.ArrayList; import java.util.Date; +import java.util.List; +import java.util.regex.Pattern; @Controller() @RequestMapping("/article") public class ArticleController { + private final String API_URL = "https://apicarto.ign.fr/api/codes-postaux/communes/"; + @Autowired private final ArticleService articleService; private final UserService userService; @@ -88,7 +99,8 @@ public class ArticleController { @RequestParam("code_postal") String code_postal, @RequestParam("ville") String ville, @RequestParam("dateDebut") String dateDebut, - @RequestParam("dateFin") String dateFin) { + @RequestParam("dateFin") String dateFin, + RedirectAttributes redirectAttributes) { //Récupérer l'utilisateur pour set l'article Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); String username = authentication.getName(); @@ -107,10 +119,99 @@ public class ArticleController { dDateDebut = format.parse(dateDebut); dDateFin = format.parse(dateFin); } catch (ParseException e) { - e.printStackTrace(); + redirectAttributes.addAttribute("erreur", "La date de début n'est pas valide."); + redirectAttributes.addAttribute("erreur", "La date de fin n'est pas valide."); } article.setDateDebutEnch(dDateDebut); article.setDateFinEnch(dDateFin); + //Vérification du formulaire + //Vérification du nom de l'article + String regex = "^[a-zA-Z0-9 ]*$"; + if (article.getNom().length() < 3){ + redirectAttributes.addAttribute("erreur", "Le nom de l'article doit contenir au moin 3 caractères."); + return "redirect:/article/new"; + } + if (!Pattern.matches(regex, article.getNom())){ + redirectAttributes.addAttribute("erreur", "Le nom de l'article ne doit pas contenir de caractère speciaux."); + return "redirect:/article/new"; + } + //Vérification description de l'article + if (article.getDesc().length() < 20){ + redirectAttributes.addAttribute("erreur", "La description de l'article doit contenir au moin 20 caractères."); + return "redirect:/article/new"; + } + //Vérification de la photo + if (article.getPhoto() != null && !article.getPhoto().isEmpty()) { + if (article.getPhoto().getSize() > 5 * 1024 * 1024) { + redirectAttributes.addAttribute("erreur", "La photo ne doit pas faire plus de 5 Mo."); + return "redirect:/article/new"; + } + if (!article.getPhoto().getOriginalFilename().toLowerCase().endsWith(".jpg")) { + redirectAttributes.addAttribute("erreur", "L'image doit avoir une extension .jpg."); + return "redirect:/article/new"; + } + } + //Vérification du prix initial + if (article.getPrixInitial() > 2000000000 && article.getPrixInitial() < 0){ + redirectAttributes.addAttribute("erreur", "Le prix doit être compris entre 0 et 2 000 000 000 crédits."); + return "redirect:/article/new"; + } + //Vérifier les dates + LocalDate dateDebutEnch = LocalDate.parse(dateDebut); + LocalDate dateActuelle = LocalDate.now(); + if (dateDebutEnch.isBefore(dateActuelle)) { + redirectAttributes.addAttribute("erreur", "La date de début d'enchère ne peux pas être infèrieur à la date du jour."); + return "redirect:/article/new"; + } + LocalDate dateFinEnch = LocalDate.parse(dateFin); + LocalDate datePlusUnJour = LocalDate.now().plusDays(1); + if (dateFinEnch.isBefore(datePlusUnJour)) { + redirectAttributes.addAttribute("erreur", "La date de début d'enchère ne peux pas être infàrieur à la date du jour + 1."); + return "redirect:/article/new"; + } + + //Vérification rue + if (!Pattern.matches("^[a-zA-Z0-9 ]+$", retrait.getRue())){ + redirectAttributes.addAttribute("erreur", "Le rue n'est pas valide."); + return "redirect:/article/new"; + } + if (retrait.getRue().isEmpty()){ + redirectAttributes.addAttribute("erreur", "Entrer une rue."); + return "redirect:/article/new"; + } + //Vérifier code postal et ville + if(Pattern.matches("^\\d{5}$", retrait.getCode_postale())){ + //Récupérer les villes en fonction du code postal + RestTemplate restTemplate = new RestTemplate(); + List villeCodePostal = new ArrayList<>(); // Initialisez la liste pour éviter les NullPointerException + String apiUrl = API_URL + retrait.getCode_postale(); + ResponseEntity response = restTemplate.getForEntity(apiUrl, JsonNode.class); // Désérialiser en JsonNode + if (response.getStatusCode().is2xxSuccessful()) { + JsonNode responseBody = response.getBody(); + if (responseBody.isArray()) { // Vérifiez si le corps de la réponse est un tableau JSON + for (JsonNode node : responseBody) { + String cityName = node.get("nomCommune").asText(); + villeCodePostal.add(cityName); + System.out.println(cityName); + } + } else { + redirectAttributes.addAttribute("erreur", "Une erreur est survenue !"); + return "redirect:/article/new"; + } + if (!villeCodePostal.contains(userProfile.getVille())) { + String showCity = String.join(", ", villeCodePostal); + redirectAttributes.addAttribute("erreur", "Essayer : " + showCity); + return "redirect:/article/new"; + } + } else { + redirectAttributes.addAttribute("erreur", "La ville n'est pas valide."); + return "redirect:/article/new"; + } + } else { + redirectAttributes.addAttribute("erreur", "Le code postal n'est pas valide."); + return "redirect:/article/new"; + } + //Validation du formulaire retrait.setNumArticle(articleService.saveArticle(article)); retraitService.setRetrait(retrait); return "redirect:/accueil"; diff --git a/src/main/java/fr/eni/enchere/controllers/InscriptionController.java b/src/main/java/fr/eni/enchere/controllers/InscriptionController.java index f76c63e..8fb719f 100644 --- a/src/main/java/fr/eni/enchere/controllers/InscriptionController.java +++ b/src/main/java/fr/eni/enchere/controllers/InscriptionController.java @@ -9,6 +9,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.apache.commons.validator.routines.EmailValidator; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +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; @@ -17,6 +19,8 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; +import org.springframework.web.servlet.view.RedirectView; +import org.springframework.web.util.UriComponentsBuilder; import java.io.IOException; import java.util.ArrayList; @@ -45,6 +49,11 @@ public class InscriptionController { @GetMapping public String viewInscription(Model model) { + // Vérifier si l'utilisateur est déjà authentifié + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (!authentication.getName().equals("anonymousUser")){ + return "redirect:/accueil"; + } model.addAttribute("userProfile", new UserProfil()); return "inscription"; } @@ -56,6 +65,9 @@ public class InscriptionController { if (allUsernames.contains(userProfile.getPseudo())) { result.rejectValue("pseudo", "error.userProfile", "Ce pseudo est déjà utilisé."); } + if (userProfile.getPseudo().isEmpty()){ + result.rejectValue("pseudo", "error.userProfile", "Entrer un pseudo."); + } // Vérifier si l'e-mail existe déjà List allEmails = userService.listEmail(); if (allEmails.contains(userProfile.getEmail())) { @@ -78,6 +90,9 @@ public class InscriptionController { if (!Pattern.matches("^[a-zA-Z0-9 ]+$", userProfile.getRue())){ result.rejectValue("rue", "error.userProfile", "Le rue n'est pas valide."); } + if (userProfile.getRue().isEmpty()){ + result.rejectValue("rue", "error.userProfile", "Entrer une rue."); + } //Vérifier code postal et ville if(Pattern.matches("^\\d{5}$", userProfile.getCode_postal())){ //Récupérer les villes en fonction du code postal @@ -93,7 +108,7 @@ public class InscriptionController { villeCodePostal.add(cityName); } } else { - result.rejectValue("ville", "error.userProfile", "La réponse de l'API n'est pas un tableau JSON."); + result.rejectValue("ville", "error.userProfile", "Une erreur est survenue !"); } if (!villeCodePostal.contains(userProfile.getVille())) { String showCity = String.join(", ", villeCodePostal); @@ -109,12 +124,24 @@ public class InscriptionController { if (!confirmPassword.equals(userProfile.getPassword())) { result.rejectValue("password", "error.userProfile", "Les mots de passe ne correspond pas."); } + // 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.getPassword()).matches()){ + result.rejectValue("password", "error.userProfile", "Le mot de passe ne correspond pas aux critères de sécurité."); + } // Si des erreurs de validation sont détectées, retourner à la page de création de compte if (result.hasErrors()) { return "inscription"; } // Sinon, enregistrer l'utilisateur et rediriger vers la page de connexion - //userService.setUtilisateur(userProfile); - return "redirect:/login"; + userService.setUtilisateur(userProfile); + // Dans votre méthode setUser après la validation réussie + UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/login"); + builder.queryParam("username", userProfile.getPseudo()); + builder.queryParam("password", userProfile.getPassword()); + // Rediriger vers la méthode POST de /login avec les paramètres + return "redirect:" + builder.toUriString(); } } diff --git a/src/main/java/fr/eni/enchere/controllers/LoginController.java b/src/main/java/fr/eni/enchere/controllers/LoginController.java index dcd0ce7..142ce6f 100644 --- a/src/main/java/fr/eni/enchere/controllers/LoginController.java +++ b/src/main/java/fr/eni/enchere/controllers/LoginController.java @@ -3,6 +3,8 @@ package fr.eni.enchere.controllers; import fr.eni.enchere.bll.UserService; import fr.eni.enchere.bo.UserProfil; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -22,6 +24,11 @@ public class LoginController { @GetMapping("/login") public String login(Model modele) { + // Vérifier si l'utilisateur est déjà authentifié + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (!authentication.getName().equals("anonymousUser")){ + return "redirect:/accueil"; + } return "security/login"; } diff --git a/src/main/java/fr/eni/enchere/controllers/ProfilController.java b/src/main/java/fr/eni/enchere/controllers/ProfilController.java index ee7756a..ea9d2bf 100644 --- a/src/main/java/fr/eni/enchere/controllers/ProfilController.java +++ b/src/main/java/fr/eni/enchere/controllers/ProfilController.java @@ -13,6 +13,8 @@ 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 { @@ -110,6 +112,14 @@ public class ProfilController { 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 diff --git a/src/main/resources/templates/newArticle.html b/src/main/resources/templates/newArticle.html index 575b61f..abe0abf 100644 --- a/src/main/resources/templates/newArticle.html +++ b/src/main/resources/templates/newArticle.html @@ -37,7 +37,7 @@
- +
@@ -71,6 +71,11 @@
+ + +
+

+
From e08371fafe1fc62a0dbb54c68d94836b7b81b7a7 Mon Sep 17 00:00:00 2001 From: jleroy Date: Thu, 25 Apr 2024 15:41:09 +0200 Subject: [PATCH 2/2] =?UTF-8?q?D=C3=A9but=20de=20la=20page=20admin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/fr/eni/enchere/bll/UserService.java | 1 + .../fr/eni/enchere/bll/UserServiceImpl.java | 5 +++++ .../java/fr/eni/enchere/bo/UserProfil.java | 12 +++++++++++- .../enchere/controllers/AdminController.java | 11 ++++++++++- .../fr/eni/enchere/dal/UserRepository.java | 1 + .../fr/eni/enchere/dal/UserRepositoryImpl.java | 18 ++++++++++++++---- .../enchere/security/WebSecurityConfig.java | 1 - 7 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/main/java/fr/eni/enchere/bll/UserService.java b/src/main/java/fr/eni/enchere/bll/UserService.java index 54b3110..ac9a9fb 100644 --- a/src/main/java/fr/eni/enchere/bll/UserService.java +++ b/src/main/java/fr/eni/enchere/bll/UserService.java @@ -12,4 +12,5 @@ public interface UserService { List listEmail(); void setUtilisateur(UserProfil utilisateur); void deleteUtilisateur(int id); + void disableUtilisateur(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 397facc..7bc026c 100644 --- a/src/main/java/fr/eni/enchere/bll/UserServiceImpl.java +++ b/src/main/java/fr/eni/enchere/bll/UserServiceImpl.java @@ -49,4 +49,9 @@ public class UserServiceImpl implements UserService { public void deleteUtilisateur(int id) { userRepository.delete(id); } + + @Override + public void disableUtilisateur(int id) { + userRepository.disable(id); + } } diff --git a/src/main/java/fr/eni/enchere/bo/UserProfil.java b/src/main/java/fr/eni/enchere/bo/UserProfil.java index 030c5a0..d6fd876 100644 --- a/src/main/java/fr/eni/enchere/bo/UserProfil.java +++ b/src/main/java/fr/eni/enchere/bo/UserProfil.java @@ -17,12 +17,13 @@ public class UserProfil { private String currentPassword; private String newPassword; private float credit; + private boolean isDisabled; 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, String newPassword, String currentPassword, 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 isDisabled, boolean isAdmin) { setId(id); setPrenom(prenom); setNom(nom); @@ -35,6 +36,7 @@ public class UserProfil { setPassword(password); setConfirmPassword(confirmPassword); setCredit(credit); + setDisabled(isDisabled); setAdmin(isAdmin); setCurrentPassword(currentPassword); setNewPassword(newPassword); @@ -137,6 +139,14 @@ public class UserProfil { this.credit = credit; } + public boolean isDisabled() { + return isDisabled; + } + + public void setDisabled(boolean disabled) { + isDisabled = disabled; + } + public boolean isAdmin() { return isAdmin; } diff --git a/src/main/java/fr/eni/enchere/controllers/AdminController.java b/src/main/java/fr/eni/enchere/controllers/AdminController.java index 637f695..dd84ee1 100644 --- a/src/main/java/fr/eni/enchere/controllers/AdminController.java +++ b/src/main/java/fr/eni/enchere/controllers/AdminController.java @@ -1,5 +1,7 @@ package fr.eni.enchere.controllers; +import fr.eni.enchere.bll.CategorieService; +import fr.eni.enchere.bll.UserService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -7,11 +9,18 @@ import org.springframework.web.bind.annotation.GetMapping; @Controller public class AdminController { - public AdminController() { + private UserService userService; + private CategorieService categorieService; + + public AdminController(UserService userService, CategorieService categorieService) { + this.userService = userService; + this.categorieService = categorieService; } @GetMapping( "/admin") public String viewAdminPanel(Model model) { + model.addAttribute("categories", categorieService.findAllCategories()); + //model.addAttribute("userProfil", userService.f); return "admin"; } } diff --git a/src/main/java/fr/eni/enchere/dal/UserRepository.java b/src/main/java/fr/eni/enchere/dal/UserRepository.java index f0a0cda..3f67a6a 100644 --- a/src/main/java/fr/eni/enchere/dal/UserRepository.java +++ b/src/main/java/fr/eni/enchere/dal/UserRepository.java @@ -12,4 +12,5 @@ public interface UserRepository { List findAllEmail(); void save(UserProfil utilisateur); void delete(int id); + void disable(int id); } diff --git a/src/main/java/fr/eni/enchere/dal/UserRepositoryImpl.java b/src/main/java/fr/eni/enchere/dal/UserRepositoryImpl.java index 7bfc63e..b230052 100644 --- a/src/main/java/fr/eni/enchere/dal/UserRepositoryImpl.java +++ b/src/main/java/fr/eni/enchere/dal/UserRepositoryImpl.java @@ -89,7 +89,8 @@ 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, isDelete) VALUES (:pseudo, :nom, :prenom, :email, :telephone, :rue, :code_postal, :ville, :mot_de_passe, 0, false, false)"; + String sql = "INSERT INTO UTILISATEURS (pseudo, nom, prenom, email, telephone, rue, code_postal, ville, mot_de_passe, credit, administrateur, isDisabled, isDelete) " + + "VALUES (:pseudo, :nom, :prenom, :email, :telephone, :rue, :code_postal, :ville, :mot_de_passe, 0, false, false, false)"; MapSqlParameterSource parameters = new MapSqlParameterSource(); parameters.addValue("pseudo", utilisateur.getPseudo()); parameters.addValue("nom", utilisateur.getNom()); @@ -107,12 +108,14 @@ 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"; + String sql; MapSqlParameterSource parameters = new MapSqlParameterSource(); 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{ - 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.getNewPassword())); } parameters.addValue("pseudo", utilisateur.getPseudo()); @@ -134,4 +137,11 @@ public class UserRepositoryImpl implements UserRepository { String sql = "UPDATE UTILISATEURS SET isDelete = 1 WHERE no_utilisateur = ?"; jdbcTemplate.update(sql, id); } + + @Override + public void disable(int id) { + String sql = "UPDATE UTILISATEURS SET isDisabled = 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 49acdb4..4ba9131 100644 --- a/src/main/java/fr/eni/enchere/security/WebSecurityConfig.java +++ b/src/main/java/fr/eni/enchere/security/WebSecurityConfig.java @@ -35,7 +35,6 @@ public class WebSecurityConfig{ @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); - } }