diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/bean/ProfileBean.java b/S2/DevWeb/Projet/src/main/java/uppa/project/bean/ProfileBean.java index dfe2948..bd36e06 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/bean/ProfileBean.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/bean/ProfileBean.java @@ -15,7 +15,6 @@ public class ProfileBean { private String email; private String oldPassword; private String password; - private String oldGender; private String gender; private User user; private HttpResponse error; @@ -34,32 +33,44 @@ public class ProfileBean { public boolean validate() { EntityManager entityManager = EntityManagerProvider.getInstance(); entityManager.getTransaction().begin(); - try{ - DAO userDAO = new Game_JPA_DAO_Factory().getDAOUser(); + DAO userDAO; + try { + userDAO= new Game_JPA_DAO_Factory().getDAOUser(); //Check if the user is valid user = userDAO.findById(Integer.parseInt(id)); if (user == null) { error = new HttpResponse(HttpResponseCode.UNAUTHORIZED, "Utilisateur non trouvé"); + entityManager.getTransaction().rollback(); return false; } //Check if the email is not already taken User[] users = userDAO.findByField("email", email); if (!oldEmail.equals(email) && users.length > 0) { error = new HttpResponse(HttpResponseCode.UNAUTHORIZED, "Cet email est déjà utilisé"); + entityManager.getTransaction().rollback(); return false; } - //Check if the old password is correct - if (!oldPassword.equals("") && user.verifyPassword(oldPassword) == false) { + //Check if the oldPassword is correct + if(!oldPassword.equals("") && !user.verifyPassword(oldPassword)) { error = new HttpResponse(HttpResponseCode.UNAUTHORIZED, "Ancien mot de passe incorrect"); + entityManager.getTransaction().rollback(); return false; } - //Update the user - user.setEmail(email); + } catch (DAOException e) { + error = new HttpResponse(HttpResponseCode.INTERNAL_SERVER_ERROR, "Une erreur est survenue (DB_CONNECTION_ERROR:002)"); + entityManager.getTransaction().rollback(); + return false; + } + //Update the user + user.setEmail(email); + if (!password.equals("")) { user.setPassword(password); - user.setGender(User.Gender.valueOf(gender)); - userDAO.update(user); - entityManager.getTransaction().commit(); - return true; + } + user.setGender(User.Gender.valueOf(gender)); + try { + userDAO.update(user); + entityManager.getTransaction().commit(); + return true; } catch (DAOException e) { error = new HttpResponse(HttpResponseCode.INTERNAL_SERVER_ERROR, "Une erreur est survenue (DB_CONNECTION_ERROR:002)"); entityManager.getTransaction().rollback(); diff --git a/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/ProfileServlet.java b/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/ProfileServlet.java index 469e870..c8e80fb 100644 --- a/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/ProfileServlet.java +++ b/S2/DevWeb/Projet/src/main/java/uppa/project/web/servlet/ProfileServlet.java @@ -44,6 +44,7 @@ public class ProfileServlet extends HttpServlet { .setId(request.getParameter("id")) .setOldEmail(request.getParameter("oldEmail")) .setEmail(request.getParameter("email")) + .setOldPassword(request.getParameter("oldPassword")) .setPassword(request.getParameter("password")) .setGender(request.getParameter("gender")) ; diff --git a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/profile.jsp b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/profile.jsp index 9c87ca0..0b3ef6b 100644 --- a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/profile.jsp +++ b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/pages/profile.jsp @@ -11,22 +11,27 @@ <%@taglib prefix="form" tagdir="/WEB-INF/tags/forms" %> - -
-
- -
- -
+ + + + + +
+
+ +
+ +
-
-
+ +
-
- - - +
+ + + +
-
- + + diff --git a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/profile.tag b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/profile.tag index a26fa33..bf51de8 100644 --- a/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/profile.tag +++ b/S2/DevWeb/Projet/src/main/webapp/WEB-INF/tags/forms/profile.tag @@ -105,19 +105,10 @@ const password = profileForm.querySelector("input[name='password']"); const repassword = profileForm.querySelector("input[name='repeat-password']"); // Check if the password and the confirmation password are the same - if (oldPassword.value !== "") { - if("${User.hashPassword(oldPassword.value)}" !== "${user.password}"){ - onError(new Error("L'ancien mot de passe ne corresponds pas")); - return; - } - if(password.value !== repassword.value) { - onError(new Error("Les mots de passe ne correspondent pas")); - return; - } + if (oldPassword.value !== "" && password.value !== repassword.value) { + onError(new Error("Les mots de passe ne correspondent pas")); + return; } - else { - password.value = "${user.password}"; - }; const {action, method} = profileForm; @@ -144,7 +135,7 @@ * @param error {Error} - Error of the form submission */ function onError(error) { - console.log("Error:", error) + console.log(error) // Input fields in red inputs.forEach(input => input.classList.add("is-danger")); @@ -169,7 +160,7 @@ notification.appendChild(notificationTitle); notification.appendChild(notificationMessage); document.body.appendChild(notification); - + console.log("je suis bien dans la fonction mais la notification ne s'affiche pas") setTimeout(() => notification.remove(), 5010); } @@ -177,7 +168,7 @@ * Handle the success of the form submission */ function onSuccess() { - console.log("Succes:", "Modifications effectuées avec succès") + console.log("Succès:", "Modifications effectuées avec succès") // Notification const notification = document.createElement("div"); @@ -189,7 +180,7 @@ const notificationIcon = document.createElement("span"); notificationIcon.classList.add("icon"); - notificationIcon.innerHTML = ""; + notificationIcon.innerHTML = ""; const notificationMessage = document.createElement("p"); notificationMessage.classList.add("subtitle", "is-6"); @@ -199,6 +190,7 @@ notification.appendChild(notificationTitle); notification.appendChild(notificationMessage); document.body.appendChild(notification); + console.log("je suis bien dans la fonction mais la notification ne s'affiche pas") setTimeout(() => notification.remove(), 5010); }