From 2fe537045d92e8fb25688f6861a5273582c617a9 Mon Sep 17 00:00:00 2001 From: dthev Date: Sun, 6 Oct 2024 23:26:14 +0200 Subject: [PATCH] =?UTF-8?q?Chap9=20=C3=A0=20terminer,=20contr=C3=B4le=20au?= =?UTF-8?q?thentification=20=C3=A0=20finir?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/java/bdd/SalarieMySQL.java | 83 +++++++++++++++++++ src/java/gestionClient/NouveauServlet.java | 18 ++++- src/java/javabeans/User.java | 93 ++++++++++++++++------ src/java/testForms/AuthentifForm.java | 24 +++++- web/WEB-INF/nouveauClientVue.jsp | 6 ++ 5 files changed, 194 insertions(+), 30 deletions(-) create mode 100644 src/java/bdd/SalarieMySQL.java diff --git a/src/java/bdd/SalarieMySQL.java b/src/java/bdd/SalarieMySQL.java new file mode 100644 index 0000000..d31ee17 --- /dev/null +++ b/src/java/bdd/SalarieMySQL.java @@ -0,0 +1,83 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package bdd; + +import java.sql.Statement; +import java.util.ArrayList; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import javabeans.User; +/** + * + * @author sio + */ +public class SalarieMySQL { + /*Connexion MV serveurBD lycée + private final Connection laConnection = Connexion.getConnect("10.121.38.165", "bdclient", "adminBDClient", "mdpBDClient");*/ + + /*Connexion wampserver + private final Connection laConnection = Connexion.getConnect("localhost", "bdclient", "adminBDClient", "mdpBDClient");*/ + + //connexion MV serveurBD maison + private final Connection laConnection = Connexion.getConnect("192.168.1.21", "bdclient", "adminBDClient", "mdpBDClient"); + + + + public ArrayList readAll(){ + User leSalarie=new User("lo","log"); + ArrayList lesSalaries = new ArrayList<>(); + try{ + Statement stmt = laConnection.createStatement(); + ResultSet resultQ = null; + resultQ = stmt.executeQuery("SELECT * FROM salarie"); + while(resultQ.next()){ + leSalarie.setLogin(resultQ.getString("login")); + leSalarie.setId(resultQ.getInt("idSal")); + leSalarie.setMdp(resultQ.getString("mdp")); + leSalarie.setRole(resultQ.getBoolean("role")); + lesSalaries.add(leSalarie); + } + resultQ.close(); + stmt.close(); + }catch (SQLException ex){ + System.out.println("SQLException : " + ex.getMessage()); + System.out.println("SQLState : " + ex.getSQLState()); + System.out.println("Code erreur : " + ex.getErrorCode()); + } + return lesSalaries; + } + + public User lireSalarie(String log, String m){ + User leSalarie=new User(); + ResultSet resultQ = null; + try{ + + PreparedStatement prepStmt = null; + + String sql = "SELECT * FROM Salarie WHERE login = ? AND mdp=?"; + prepStmt = laConnection.prepareStatement(sql); // pré-compilation + + prepStmt.setString(1, log); //le param. 1 est remplacé par "Toto" + prepStmt.setString(2,m); + resultQ = prepStmt.executeQuery(); + if (resultQ.first()){ + leSalarie.setLogin(resultQ.getString("login")); + leSalarie.setId(resultQ.getInt("idSal")); + leSalarie.setMdp(resultQ.getString("mdp")); + leSalarie.setRole(resultQ.getBoolean("role")); + } + resultQ.close(); + prepStmt.close(); + }catch (SQLException ex){ + System.out.println("SQLException : " + ex.getMessage()); + System.out.println("SQLState : " + ex.getSQLState()); + System.out.println("Code erreur : " + ex.getErrorCode()); + } + return leSalarie; + } +} diff --git a/src/java/gestionClient/NouveauServlet.java b/src/java/gestionClient/NouveauServlet.java index 19547de..5d6c96e 100644 --- a/src/java/gestionClient/NouveauServlet.java +++ b/src/java/gestionClient/NouveauServlet.java @@ -10,6 +10,8 @@ import jakarta.servlet.ServletException; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; +import java.util.ArrayList; import testForms.AuthentifForm; import testForms.NouveauClientForm; @@ -79,7 +81,21 @@ public class NouveauServlet extends HttpServlet { //création de 2 attributs de requête (isAdmin et leControle) request.setAttribute("controlForm", leControle); request.setAttribute("isNumAjoute", isNum); - //Affichage JSP + //gestion de la liste des clients créés + if (isNum != -1) { + // Ajout du no du client créé, dans une collection d’entiers stockée en session + // 1, Récupération de la session + HttpSession maSession = request.getSession(); + // 2. Récupération de la collection des clients si elle existe, création sinon + ArrayList lesNouveauxClients = (ArrayList) maSession.getAttribute("lesNvxClients"); + if (lesNouveauxClients == null) { + lesNouveauxClients = new ArrayList<>(); + } + // 3. Ajout du no du client créé dans la collection + lesNouveauxClients.add(isNum); + // 4. Mise à jour de la session + maSession.setAttribute("lesNvxClients", lesNouveauxClients); } + //Affichage JSP, redirection couche vue getServletContext().getRequestDispatcher("/WEB-INF/nouveauClientVue.jsp").forward(request, response); } diff --git a/src/java/javabeans/User.java b/src/java/javabeans/User.java index b6ff353..aa61dbf 100644 --- a/src/java/javabeans/User.java +++ b/src/java/javabeans/User.java @@ -11,37 +11,70 @@ import java.util.Objects; * @author famille Thevenot */ public class User { - private String pseudo; - private String motDePasse; + //variables membres + private int id; + private String login; + private String mdp; + private boolean role; - public User(String pseudo, String motDePasse) { - this.pseudo = pseudo; - this.motDePasse = motDePasse; + public User(int id, String login, String mdp, boolean role) { + this.id = id; + this.login = login; + this.mdp = mdp; + this.role = role; } - public User(String header) { + public User(String parameter, String parameter0) { + this.login = parameter; + this.mdp = parameter0; + } + + public User() { + this.id = -1; + } + + + + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getLogin() { + return login; + } + + public void setLogin(String login) { + this.login = login; + } + + public String getMdp() { + return mdp; + } + + public void setMdp(String mdp) { + this.mdp = mdp; + } + + public boolean getRole() { + return role; + } + + public void setRole(boolean role) { + this.role = role; + } + + public User(String header) { throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody } - public String getPseudo() { - return pseudo; - } - - public void setPseudo(String pseudo) { - this.pseudo = pseudo; - } - - public String getMotDePasse() { - return motDePasse; - } - - public void setMotDePasse(String motDePasse) { - this.motDePasse = motDePasse; - } - @Override public int hashCode() { - int hash = 5; + int hash = 7; return hash; } @@ -57,10 +90,18 @@ public class User { return false; } final User other = (User) obj; - if (!Objects.equals(this.pseudo, other.pseudo)) { + if (this.id != other.id) { return false; } - return Objects.equals(this.motDePasse, other.motDePasse); + if (!Objects.equals(this.login, other.login)) { + return false; + } + if (!Objects.equals(this.mdp, other.mdp)) { + return false; + } + return Objects.equals(this.role, other.role); } - + + + } diff --git a/src/java/testForms/AuthentifForm.java b/src/java/testForms/AuthentifForm.java index 2c1595d..2612ea1 100644 --- a/src/java/testForms/AuthentifForm.java +++ b/src/java/testForms/AuthentifForm.java @@ -4,6 +4,7 @@ */ package testForms; +import bdd.SalarieMySQL; import jakarta.servlet.http.HttpServletRequest; import javabeans.User; @@ -22,12 +23,29 @@ public class AuthentifForm { this.resultat = resultat; } + public boolean existeUser(HttpServletRequest request) + { + SalarieMySQL userSaisi = new SalarieMySQL(); + //test du compte admin + User leSalarie=new User(); + leSalarie=userSaisi.lireSalarie(request.getParameter("ztPseudo"),request.getParameter("ztMDP")); + if (leSalarie.getId()>-1){ + return true; + } + else { + return false; + } + } + + public boolean controlerAdmin(HttpServletRequest request) { - User admin = new User("admin", "minda"); - User userSaisi = new User(request.getParameter("ztPseudo"),request.getParameter("ztMDP")); + SalarieMySQL userSaisi = new SalarieMySQL(); //test du compte admin - boolean isAdmin = userSaisi.equals(admin); + User leSalarie=new User();; + leSalarie=userSaisi.lireSalarie(request.getParameter("ztPseudo"),request.getParameter("ztMDP")); + + boolean isAdmin = leSalarie.getRole(); //mise à jour de l'attribut resultat setResultat(isAdmin?"Vous êtes administrateur":"Vous n'êtes pas administrateur"); diff --git a/web/WEB-INF/nouveauClientVue.jsp b/web/WEB-INF/nouveauClientVue.jsp index 970e672..9efe449 100644 --- a/web/WEB-INF/nouveauClientVue.jsp +++ b/web/WEB-INF/nouveauClientVue.jsp @@ -20,6 +20,12 @@

Client ${isNumAjoute} ajouté

+
+

Liste des numéros des clients récemment créés :

+ ${unClient} - + +
+