diff --git a/build.gradle b/build.gradle index 7d769d9..607d65f 100644 --- a/build.gradle +++ b/build.gradle @@ -27,6 +27,10 @@ dependencies { //Securité implementation 'org.springframework.boot:spring-boot-starter-security' implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6' + //Vérification de formulaire + implementation 'commons-validator:commons-validator:1.7' + implementation 'com.googlecode.libphonenumber:libphonenumber:5.5' + //test testImplementation 'org.springframework.security:spring-security-test' testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/src/main/java/fr/eni/enchere/bo/UserProfil.java b/src/main/java/fr/eni/enchere/bo/UserProfil.java index c96e22b..030c5a0 100644 --- a/src/main/java/fr/eni/enchere/bo/UserProfil.java +++ b/src/main/java/fr/eni/enchere/bo/UserProfil.java @@ -160,4 +160,5 @@ public class UserProfil { public void setNewPassword(String newPassword) { this.newPassword = newPassword; } + } diff --git a/src/main/java/fr/eni/enchere/config/WebConfig.java b/src/main/java/fr/eni/enchere/config/WebConfig.java index 623bde6..638e62d 100644 --- a/src/main/java/fr/eni/enchere/config/WebConfig.java +++ b/src/main/java/fr/eni/enchere/config/WebConfig.java @@ -1,5 +1,7 @@ package fr.eni.enchere.config; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import org.apache.commons.validator.routines.EmailValidator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; @@ -28,4 +30,15 @@ public class WebConfig { source.setUseCodeAsDefaultMessage(true); return source; } + + @Bean + public EmailValidator emailValidator() { + return EmailValidator.getInstance(); + } + + @Bean + public PhoneNumberUtil phoneValidator() { + return PhoneNumberUtil.getInstance(); + } + } \ No newline at end of file diff --git a/src/main/java/fr/eni/enchere/controllers/InscriptionController.java b/src/main/java/fr/eni/enchere/controllers/InscriptionController.java index d50902a..f76c63e 100644 --- a/src/main/java/fr/eni/enchere/controllers/InscriptionController.java +++ b/src/main/java/fr/eni/enchere/controllers/InscriptionController.java @@ -1,8 +1,14 @@ package fr.eni.enchere.controllers; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.i18n.phonenumbers.NumberParseException; +import com.google.i18n.phonenumbers.PhoneNumberUtil; +import com.google.i18n.phonenumbers.Phonenumber; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; +import org.apache.commons.validator.routines.EmailValidator; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.validation.BindingResult; import fr.eni.enchere.bll.UserService; @@ -10,30 +16,41 @@ import fr.eni.enchere.bo.UserProfil; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; +import org.springframework.web.client.RestTemplate; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.regex.Pattern; @Controller @RequestMapping("/inscription") public class InscriptionController { + private final String API_URL = "https://apicarto.ign.fr/api/codes-postaux/communes/"; + @Autowired private final UserService userService; private PasswordEncoder passwordEncoder; + private EmailValidator emailValidator; + private PhoneNumberUtil phoneValidator; - public InscriptionController(UserService userService, PasswordEncoder passwordEncoder) { + public InscriptionController(UserService userService, PasswordEncoder passwordEncoder, EmailValidator emailValidator, PhoneNumberUtil phoneValidator) { this.userService = userService; this.passwordEncoder = passwordEncoder; + this.emailValidator = emailValidator; + this.phoneValidator = phoneValidator; } @GetMapping public String viewInscription(Model model) { - model.addAttribute("user", new UserProfil()); + model.addAttribute("userProfile", new UserProfil()); return "inscription"; } @PostMapping("/newUser") - public String setUser(@ModelAttribute("userProfile") UserProfil userProfile, BindingResult result) { + public String setUser(@ModelAttribute("userProfile") UserProfil userProfile, @RequestParam("confirmPassword") String confirmPassword, BindingResult result) { // Vérifier si le pseudo existe déjà List allUsernames = userService.listPseudo(); if (allUsernames.contains(userProfile.getPseudo())) { @@ -44,12 +61,60 @@ public class InscriptionController { if (allEmails.contains(userProfile.getEmail())) { result.rejectValue("email", "error.userProfile", "Cet e-mail est déjà utilisé."); } + // vérifier si l'e-mail est valide + if (!emailValidator.isValid(userProfile.getEmail())) { + result.rejectValue("email", "error.userProfile", "L'adresse e-mail n'est pas valide."); + } + // vérifier si le numéro de téléphone est valide + PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); + try { + Phonenumber.PhoneNumber number = phoneNumberUtil.parse(userProfile.getTelephone(), "FR"); // Indiquez le code pays, ici FR pour la France + phoneNumberUtil.isValidNumber(number); + } catch (NumberParseException e) { + result.rejectValue("telephone", "error.userProfile", "Le numéro de téléphone n'est pas valide."); + } + // vérification retrait + //Vérification rue + if (!Pattern.matches("^[a-zA-Z0-9 ]+$", userProfile.getRue())){ + result.rejectValue("rue", "error.userProfile", "Le rue n'est pas valide."); + } + //Vérifier code postal et ville + if(Pattern.matches("^\\d{5}$", userProfile.getCode_postal())){ + //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 + userProfile.getCode_postal(); + 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); + } + } else { + result.rejectValue("ville", "error.userProfile", "La réponse de l'API n'est pas un tableau JSON."); + } + if (!villeCodePostal.contains(userProfile.getVille())) { + String showCity = String.join(", ", villeCodePostal); + result.rejectValue("ville", "error.userProfile", "Essayer : " + showCity); + } + } else { + result.rejectValue("ville", "error.userProfile", "La ville n'est pas valide."); + } + } else { + result.rejectValue("code_postal", "error.userProfile", "Le code postal n'est pas valide."); + } + // vérifier si les mot de passes sont identique + if (!confirmPassword.equals(userProfile.getPassword())) { + result.rejectValue("password", "error.userProfile", "Les mots de passe ne correspond pas."); + } // 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); + //userService.setUtilisateur(userProfile); return "redirect:/login"; } } diff --git a/src/main/resources/templates/inscription.html b/src/main/resources/templates/inscription.html index af30f07..7a509be 100644 --- a/src/main/resources/templates/inscription.html +++ b/src/main/resources/templates/inscription.html @@ -6,7 +6,7 @@
-
+ @@ -14,7 +14,7 @@
- +
    @@ -26,7 +26,7 @@
    - +
      @@ -38,7 +38,7 @@
      - +
        @@ -50,7 +50,7 @@
        - +
          @@ -74,7 +74,7 @@
          - +
            @@ -86,7 +86,7 @@
            - +
              @@ -98,7 +98,7 @@
              - +
                @@ -109,7 +109,7 @@
                - +
                  @@ -120,7 +120,7 @@
                  - +