From 6b4ee686924815f421262fcd452e78a3f32e2fb6 Mon Sep 17 00:00:00 2001 From: SovereignX Date: Mon, 12 Oct 2026 12:20:31 +0200 Subject: [PATCH 1/6] Mise en place du validateur pour le formulaire de Connexion --- src/main/java/forms/ConnexionForm.java | 85 +++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 1 deletion(-) diff --git a/src/main/java/forms/ConnexionForm.java b/src/main/java/forms/ConnexionForm.java index 1aaa296..742d604 100644 --- a/src/main/java/forms/ConnexionForm.java +++ b/src/main/java/forms/ConnexionForm.java @@ -1,9 +1,92 @@ package forms; +import beans.Web_user; +import jakarta.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + /** * * @author Axel L. */ public class ConnexionForm { - + + private static final String EMAIL = "email"; + private static final String PASSWORD = "password"; + + private String result; + private final Map errors = new HashMap<>(); + + public String getResult() { + return result; + } + + public Map getErrors() { + return errors; + } + + public Web_user connectUser(HttpServletRequest req) { + String email = getParamValue(req, EMAIL); + String password = getParamValue(req, PASSWORD); + + Web_user user = new Web_user(); + + try { + validateEmail(email); + } catch (Exception ex) { + setError(EMAIL, ex.getMessage()); + } + user.setEmail(email); + + try { + validatePassword(password); + } catch (Exception ex) { + setError(PASSWORD, ex.getMessage()); + } + user.setPassword(password); + + if (errors.isEmpty()) { + // Appel DAO + result = "Succès de la connexion"; + } else { + result = "Échec de la connexion"; + } + return user; + } + + // Vérifie si l'email est valide + private void validateEmail(String email) throws Exception { + if (email == null) { + throw new Exception("Merci de saisir une adresse email"); + + } else if (!email.matches("([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)")) { + throw new Exception("Merci de saisir une adresse email valide"); + } + } + + // Vérifie le mot de passe + private void validatePassword(String password) throws Exception { + if (password != null) { + if (password.length() < 8) { + throw new Exception("Le mot de passe doit contenir au moins 8 caractères"); + } + } else { + throw new Exception("Merci de saisir votre mot de passe"); + } + } + + // Ajoute un message correspondant au champ spécifié à la map des erreurs + private void setError(String field, String message) { + errors.put(field, message); + } + + // Méthode utilitaire qui retourne null si un champ est .. null + private static String getParamValue(HttpServletRequest req, String paramKey) { + String value = req.getParameter(paramKey); + if (value == null || value.trim().length() == 0) { + return null; + } else { + return value.trim(); + } + } } -- GitLab From 9984e97673ef47bb3ef61a168dab7de8de1dfb8d Mon Sep 17 00:00:00 2001 From: SovereignX Date: Mon, 12 Oct 2026 14:44:27 +0200 Subject: [PATCH 2/6] Ajout du validateur du formulaire d'inscription --- src/main/java/forms/InscriptionForm.java | 110 ++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) diff --git a/src/main/java/forms/InscriptionForm.java b/src/main/java/forms/InscriptionForm.java index 8b935f5..2757c17 100644 --- a/src/main/java/forms/InscriptionForm.java +++ b/src/main/java/forms/InscriptionForm.java @@ -1,9 +1,117 @@ package forms; +import beans.Web_user; +import jakarta.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + /** * * @author Axel L. */ public class InscriptionForm { - + + private static final String EMAIL = "email"; + private static final String PASSWORD = "password"; + private static final String USERNAME = "username"; + + // Variables données + private static final int MIN_PASSWORD = 8; // Variable pour le nombre de caractères minimum pour le mot de passe + private static final int MIN_USERNAME = 3; // Variable pour le nombre de caractères minimum pour le pseudo + private static final int MAX_USERNAME = 50; // Variable pour le nombre de caractères maximum pour le pseudo + + private String result; + private final Map errors = new HashMap<>(); + + public String getResult() { + return result; + } + + public Map getErrors() { + return errors; + } + + public Web_user submitUser(HttpServletRequest req) { + String email = getParamValue(req, EMAIL); + String password = req.getParameter(PASSWORD); + String confirmPassword = req.getParameter("confirmPassword"); + String username = getParamValue(req, USERNAME); + + Web_user user = new Web_user(); + user.setEmail(email); + user.setUsername(username); + + try { + validateEmail(email); + } catch (Exception ex) { + setError(EMAIL, ex.getMessage()); + } + user.setEmail(email); + + try { + validatePassword(password); + } catch (Exception ex) { + setError(PASSWORD, ex.getMessage()); + } + user.setPassword(password); + + try { + validateName(username); + } catch (Exception ex) { + setError(USERNAME, ex.getMessage()); + } + user.setUsername(username); + + if (errors.isEmpty()) { + // Partie Dao + result = "Succès de l'inscription"; + } else { + result = "Échec de l'inscription"; + } + return user; + } + + // Vérifie si l'email est valide + private void validateEmail(String email) throws Exception { + if (email == null) { + throw new Exception("Merci de saisir une adresse email"); + + } else if (!email.matches("([^.@]+)(\\.[^.@]+)*@([^.@]+\\.)+([^.@]+)")) { + throw new Exception("Merci de saisir une adresse email valide"); + } + } + + // Vérifie le mot de passe + private void validatePassword(String password) throws Exception { + if (password != null) { + if (password.length() < MIN_PASSWORD) { + throw new Exception("Le mot de passe doit contenir au moins " + MIN_PASSWORD + " caractères"); + } + } else { + throw new Exception("Merci de saisir votre mot de passe"); + } + } + + // Vérifie le pseudo / nom + private void validateName(String username) throws Exception { + if (username == null) { + throw new Exception("Merci de choisir un nom d'utilisateur"); + } + + if (username.length() < MIN_USERNAME || username.length() > MAX_USERNAME) { + throw new Exception("Le nom d'utilisateur doit contenir entre " + MIN_USERNAME + " et " + MAX_USERNAME + "caractères"); + } + } + + private void setError(String field, String message) { + errors.put(field, message); + } + + private static String getParamValue(HttpServletRequest req, String paramKey) { + String value = req.getParameter(paramKey); + if(value == null || value.trim().isEmpty()) { + return null; + } + return value.trim(); + } } -- GitLab From f774a41fb5d388e8666b65e58300d13a9e7d2cab Mon Sep 17 00:00:00 2001 From: SovereignX Date: Mon, 12 Oct 2026 15:54:13 +0200 Subject: [PATCH 3/6] Ajout du validateur du formulaire de publication d'un post + mini modif sur le validateur connexion --- src/main/java/forms/ConnexionForm.java | 6 +- src/main/java/forms/PostForm.java | 104 ++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 3 deletions(-) diff --git a/src/main/java/forms/ConnexionForm.java b/src/main/java/forms/ConnexionForm.java index 742d604..73163e4 100644 --- a/src/main/java/forms/ConnexionForm.java +++ b/src/main/java/forms/ConnexionForm.java @@ -13,6 +13,8 @@ public class ConnexionForm { private static final String EMAIL = "email"; private static final String PASSWORD = "password"; + + private static final int MIN_PASSWORD = 8; private String result; private final Map errors = new HashMap<>(); @@ -67,8 +69,8 @@ public class ConnexionForm { // Vérifie le mot de passe private void validatePassword(String password) throws Exception { if (password != null) { - if (password.length() < 8) { - throw new Exception("Le mot de passe doit contenir au moins 8 caractères"); + if (password.length() < MIN_PASSWORD) { + throw new Exception("Le mot de passe doit contenir au moins " + MIN_PASSWORD + " caractères"); } } else { throw new Exception("Merci de saisir votre mot de passe"); diff --git a/src/main/java/forms/PostForm.java b/src/main/java/forms/PostForm.java index 1815e80..4a70810 100644 --- a/src/main/java/forms/PostForm.java +++ b/src/main/java/forms/PostForm.java @@ -1,9 +1,111 @@ package forms; +import beans.Post; +import beans.Web_user; +import jakarta.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + /** * * @author Axel L. */ public class PostForm { - + + private static final String TITLE = "title"; + private static final String DESCRIPTION = "description"; + private static final String VIDEO = "videoUrl"; + + private static final int MAX_TITLE = 50; // Longueur Max du titre + private static final int MAX_CONTENT = 500; // Longueur MAX de la description + private static final int MAX_VIDEO = 255; // Longueur du lien de la vidéo + + private String result; + private final Map errors = new HashMap<>(); + + public String getResult() { + return result; + } + + public Map getErrors() { + return errors; + } + + public Post createPost(HttpServletRequest req, Web_user author) { + + String title = getParamValue(req, TITLE); + String description = getParamValue(req, DESCRIPTION); + String videoUrl = getParamValue(req, VIDEO); + + Post post = new Post(); + post.setAuthor(author); + + try { + validateTitle(title); + } catch (Exception ex) { + setError(TITLE, ex.getMessage()); + } + post.setTitle(title); + + try { + validateContent(description); + } catch (Exception ex) { + setError(DESCRIPTION, ex.getMessage()); + } + post.setDescription(description); + + try { + validateVideo(videoUrl); + } catch (Exception ex) { + setError(VIDEO, ex.getMessage()); + } + post.setVideoUrl(videoUrl); + + if (errors.isEmpty()) { + // Partie Dao + result = "Post publié"; + } else { + result = "Échec de la publication"; + } + return post; + } + + private void validateTitle(String title) throws Exception { + if (title == null) { + throw new Exception("Merci de donner un titre à votre publication"); + } + if (title.length() > MAX_TITLE) { + throw new Exception("Le titre ne doit pas dépasser " + MAX_TITLE + "caractères"); + } + } + + private void validateContent(String description) throws Exception { + if (description == null) { + throw new Exception("Merci d'ajouter une description"); + } + if (description.length() > MAX_CONTENT) { + throw new Exception("La description ne doit pas dépasser " + MAX_CONTENT + "caractères"); + } + } + + private void validateVideo(String videoUrl) throws Exception { + if (videoUrl == null) { + throw new Exception("Merci de fournir un lien pour votre vidéo"); + } + if (videoUrl.length() > MAX_VIDEO) { + throw new Exception("Ce lien n'est pas correct"); + } + } + + private void setError(String field, String message) { + errors.put(field, message); + } + + private static String getParamValue(HttpServletRequest req, String paramKey) { + String value = req.getParameter(paramKey); + if (value == null || value.trim().isEmpty()) { + return null; + } + return value.trim(); + } } -- GitLab From 8dfc846201ffd0bf1b9675764a1c4b6cdbd6a678 Mon Sep 17 00:00:00 2001 From: SovereignX Date: Mon, 12 Oct 2026 16:36:17 +0200 Subject: [PATCH 4/6] Ajout du validateur pour le formulaire du commentaire --- src/main/java/forms/CommentForm.java | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/main/java/forms/CommentForm.java diff --git a/src/main/java/forms/CommentForm.java b/src/main/java/forms/CommentForm.java new file mode 100644 index 0000000..3bcc807 --- /dev/null +++ b/src/main/java/forms/CommentForm.java @@ -0,0 +1,61 @@ +package forms; + +import beans.Comment; +import beans.Post; +import beans.Web_user; +import jakarta.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author Axel L. + */ +public class CommentForm { + + private static final int MAX_COMMENT = 255; // Longueur max du commentaire + + private String result; + private final Map errors = new HashMap<>(); + + public String getResult() { + return result; + } + + public Map getErrors() { + return errors; + } + + public Comment createComment(HttpServletRequest req, Web_user author, Post post) { + String content = req.getParameter("content"); + content = (content == null) ? null : content.trim(); + + Comment comment = new Comment(); + comment.setContent(content); + comment.setAuthor(author); + comment.setPost(post); + + try { + validateContent(content); + } catch (Exception ex) { + errors.put("content", ex.getMessage()); + } + + if (errors.isEmpty()) { + // Partie Dao + result = "Commentaire publié"; + } else { + result = "Échec lors de l'ajout du commentaire"; + } + return comment; + } + + private void validateContent(String content) throws Exception { + if (content == null) { + throw new Exception("Votre commentaire est vide"); + } + if (content.length() > MAX_COMMENT) { + throw new Exception("Le commentaire doit faire maximum " + MAX_COMMENT + " caractères"); + } + } +} -- GitLab From 2fe20b64830a617be84a3bc388e50c63b918e1d1 Mon Sep 17 00:00:00 2001 From: SovereignX Date: Mon, 12 Oct 2026 16:53:27 +0200 Subject: [PATCH 5/6] ignore --- .gitignore | 1 + .../MyLatestTricks-1.0-SNAPSHOT/WEB-INF/jspf/footer.jspf | 3 +++ target/MyLatestTricks-1.0-SNAPSHOT/assets/css/style.css | 9 +++++++++ 3 files changed, 13 insertions(+) create mode 100644 .gitignore create mode 100644 target/MyLatestTricks-1.0-SNAPSHOT/WEB-INF/jspf/footer.jspf create mode 100644 target/MyLatestTricks-1.0-SNAPSHOT/assets/css/style.css diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/target/MyLatestTricks-1.0-SNAPSHOT/WEB-INF/jspf/footer.jspf b/target/MyLatestTricks-1.0-SNAPSHOT/WEB-INF/jspf/footer.jspf new file mode 100644 index 0000000..31aa642 --- /dev/null +++ b/target/MyLatestTricks-1.0-SNAPSHOT/WEB-INF/jspf/footer.jspf @@ -0,0 +1,3 @@ +<%-- any content can be specified here e.g.: --%> +<%@ page pageEncoding="UTF-8" %> +

Footer

diff --git a/target/MyLatestTricks-1.0-SNAPSHOT/assets/css/style.css b/target/MyLatestTricks-1.0-SNAPSHOT/assets/css/style.css new file mode 100644 index 0000000..bd5be7a --- /dev/null +++ b/target/MyLatestTricks-1.0-SNAPSHOT/assets/css/style.css @@ -0,0 +1,9 @@ +/* +Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license +Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/CascadeStyleSheet.css to edit this template +*/ +/* + Created on : Oct 6, 2026, 3:04:31 PM + Author : Axel L. +*/ + -- GitLab From 613d7c4e8682b66b80db2018e273a648b73585b1 Mon Sep 17 00:00:00 2001 From: SovereignX Date: Mon, 12 Oct 2026 17:06:34 +0200 Subject: [PATCH 6/6] Ajout du validateur de la modification du mot de passe (WIP) --- src/main/java/forms/ModificationForm.java | 69 +++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/main/java/forms/ModificationForm.java diff --git a/src/main/java/forms/ModificationForm.java b/src/main/java/forms/ModificationForm.java new file mode 100644 index 0000000..fa480a8 --- /dev/null +++ b/src/main/java/forms/ModificationForm.java @@ -0,0 +1,69 @@ +package forms; + +import beans.Web_user; +import jakarta.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author Axel L. + */ +public class ModificationForm { + + private static final String CURRENT = "currentPassword"; + private static final String PASSWORD = "password"; + private static final int MIN_PASSWORD = 8; + + private String result; + private final Map errors = new HashMap<>(); + + public String getResult() { + return result; + } + + public Map getErrors() { + return errors; + } + + public Web_user modifyUser(HttpServletRequest req, Web_user sessionUser) { + String current = req.getParameter(CURRENT); + String password = req.getParameter(PASSWORD); + String confirmPassword = req.getParameter("confirmPassword"); + + //DAO + try { + validatePassword(password, confirmPassword); + } catch (Exception ex) { + setError(PASSWORD, ex.getMessage()); + } + if (errors.isEmpty()) { + // Partie Dao + result = "Mot de passe modifié"; + } else { + result = "Échec de la modification"; + } + // Provisoire + Web_user daoUser = null; + return daoUser; + } + + // Vérifie le mot de passe + private void validatePassword(String password, String confirmPassword) throws Exception { + if (password != null) { + if (password.length() < MIN_PASSWORD) { + throw new Exception("Le mot de passe doit contenir au moins " + MIN_PASSWORD + " caractères"); + } + } else { + throw new Exception("Merci de saisir votre mot de passe"); + } + if (!password.equals(confirmPassword)) { + throw new Exception("Les mots de passe sont différents"); + } + } + + // Ajoute un message correspondant au champ spécifié à la map des erreurs + private void setError(String field, String message) { + errors.put(field, message); + } +} -- GitLab