Chap9 à terminer, contrôle authentification à finir

This commit is contained in:
dthev 2024-10-06 23:26:14 +02:00
parent e9751677f4
commit 2fe537045d
5 changed files with 194 additions and 30 deletions

View File

@ -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<User> readAll(){
User leSalarie=new User("lo","log");
ArrayList <User> 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;
}
}

View File

@ -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 dentiers 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<Integer> lesNouveauxClients = (ArrayList<Integer>) 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);
}

View File

@ -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 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);
}
}

View File

@ -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");

View File

@ -20,6 +20,12 @@
<c:choose>
<c:when test="${isNumAjoute >0}" >
<p>Client ${isNumAjoute} ajouté</p>
<article>
<h3>Liste des numéros des clients récemment créés : </h3>
<c:forEach items="${sessionScope.lesNvxClients}" var="unClient"> ${unClient} -
</c:forEach>
</article>
</c:when>
<c:otherwise>