commit d644c8efef90c86bc1699edb8ed0c600ad6d3d46 Author: Lensors Date: Sat Apr 12 08:23:51 2025 +0200 Premier commit diff --git a/Projet/.classpath b/Projet/.classpath new file mode 100644 index 0000000..be1e92f --- /dev/null +++ b/Projet/.classpath @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/Projet/.gitignore b/Projet/.gitignore new file mode 100644 index 0000000..84c048a --- /dev/null +++ b/Projet/.gitignore @@ -0,0 +1 @@ +/build/ diff --git a/Projet/.project b/Projet/.project new file mode 100644 index 0000000..08e98c5 --- /dev/null +++ b/Projet/.project @@ -0,0 +1,31 @@ + + + Projet + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.jdt.core.javanature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/Projet/.settings/.jsdtscope b/Projet/.settings/.jsdtscope new file mode 100644 index 0000000..76c2d63 --- /dev/null +++ b/Projet/.settings/.jsdtscope @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/Projet/.settings/org.eclipse.core.resources.prefs b/Projet/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000..99f26c0 --- /dev/null +++ b/Projet/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +encoding/=UTF-8 diff --git a/Projet/.settings/org.eclipse.jdt.core.prefs b/Projet/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..5205a10 --- /dev/null +++ b/Projet/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=21 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=21 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=21 diff --git a/Projet/.settings/org.eclipse.wst.common.component b/Projet/.settings/org.eclipse.wst.common.component new file mode 100644 index 0000000..23b48cb --- /dev/null +++ b/Projet/.settings/org.eclipse.wst.common.component @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/Projet/.settings/org.eclipse.wst.common.project.facet.core.xml b/Projet/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 0000000..6d01952 --- /dev/null +++ b/Projet/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/Projet/.settings/org.eclipse.wst.jsdt.ui.superType.container b/Projet/.settings/org.eclipse.wst.jsdt.ui.superType.container new file mode 100644 index 0000000..3bd5d0a --- /dev/null +++ b/Projet/.settings/org.eclipse.wst.jsdt.ui.superType.container @@ -0,0 +1 @@ +org.eclipse.wst.jsdt.launching.baseBrowserLibrary \ No newline at end of file diff --git a/Projet/.settings/org.eclipse.wst.jsdt.ui.superType.name b/Projet/.settings/org.eclipse.wst.jsdt.ui.superType.name new file mode 100644 index 0000000..05bd71b --- /dev/null +++ b/Projet/.settings/org.eclipse.wst.jsdt.ui.superType.name @@ -0,0 +1 @@ +Window \ No newline at end of file diff --git a/Projet/BD.md b/Projet/BD.md new file mode 100644 index 0000000..53d5b03 --- /dev/null +++ b/Projet/BD.md @@ -0,0 +1,42 @@ +``` +DROP TABLE IF EXISTS utilisateur; +DROP TABLE IF EXISTS page; +DROP TABLE IF EXISTS bloc; +``` + +``` +CREATE TABLE utilisateur ( + id INT PRIMARY KEY AUTO_INCREMENT, + login VARCHAR(100) NOT NULL, + date_arrivee DATE NOT NULL, + privileges ENUM('ADMIN', 'USER', 'GUEST') NOT NULL +); +``` + +``` +CREATE TABLE page ( + id INT PRIMARY KEY AUTO_INCREMENT, + titre VARCHAR(200) NOT NULL, + date_creation DATETIME NOT NULL, + date_modification DATETIME NOT NULL, + auteur_id INT NOT NULL, + droits ENUM('LECTURE', 'ECRITURE', 'ADMIN') NOT NULL + FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) +); +``` + +``` +CREATE TABLE bloc ( + id INT PRIMARY KEY AUTO_INCREMENT, + type ENUM('TEXTE', 'LISTE', 'TITRE', 'CODE', 'PAGE') NOT NULL, + contenu TEXT, + date_creation DATETIME NOT NULL, + date_modification DATETIME NOT NULL, + page_id INT NOT NULL, + ordre INT NOT NULL, + auteur_id INT NOT NULL, + metadata JSON, + FOREIGN KEY (page_id) REFERENCES page(id), + FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) +); +``` \ No newline at end of file diff --git a/Projet/projet.sql b/Projet/projet.sql new file mode 100644 index 0000000..dcee547 --- /dev/null +++ b/Projet/projet.sql @@ -0,0 +1,35 @@ +DROP TABLE IF EXISTS bloc; +DROP TABLE IF EXISTS page; +DROP TABLE IF EXISTS utilisateur; + +CREATE TABLE utilisateur ( + id INT PRIMARY KEY AUTO_INCREMENT, + login VARCHAR(100) NOT NULL, + mot_de_passe VARCHAR(255) NOT NULL, + privilege ENUM('ADMIN', 'USER', 'GUEST') NOT NULL +); + +CREATE TABLE page ( + id INT PRIMARY KEY AUTO_INCREMENT, + titre VARCHAR(200) NOT NULL, + date_creation DATETIME NOT NULL, + date_modification DATETIME NOT NULL, + auteur_id INT NOT NULL, + droits ENUM('LECTURE', 'ECRITURE', 'ADMIN') NOT NULL, + FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) +); + +CREATE TABLE bloc ( + id INT PRIMARY KEY AUTO_INCREMENT, + type ENUM('TEXTE', 'LISTE', 'TITRE', 'CODE', 'PAGE') NOT NULL, + contenu TEXT, + date_creation DATETIME NOT NULL, + date_modification DATETIME NOT NULL, + page_id INT NOT NULL, + ordre INT NOT NULL, + auteur_id INT NOT NULL, + metadata JSON, + FOREIGN KEY (page_id) REFERENCES page(id), + FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) +); + diff --git a/Projet/src/main/java/projet/Accueil.java b/Projet/src/main/java/projet/Accueil.java new file mode 100644 index 0000000..45b22a0 --- /dev/null +++ b/Projet/src/main/java/projet/Accueil.java @@ -0,0 +1,63 @@ +package projet; + +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import java.io.IOException; + +@WebServlet("") +public class Accueil extends HttpServlet { + private static final long serialVersionUID = 1L; + + public void init() { + ParamBD.init(getServletContext()); + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + if(u == null) { + request.setAttribute("erreur", 0); + RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Accueil.jsp"); + rd.forward(request, response); + } else { + response.sendRedirect("AfficherPage"); + } + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String login = request.getParameter("login"); + String mdp = request.getParameter("mdp"); + String action = request.getParameter("action"); + + if(action.equals("connexion")) { + Utilisateur u = Utilisateur.authentifier(login, mdp); + if(u != null) { + HttpSession session = request.getSession(); + session.setAttribute("utilisateur", u); + response.sendRedirect("AfficherPage"); + }else { + request.setAttribute("erreur", 1); + RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Accueil.jsp"); + rd.forward(request, response); + } + } else if (action.equals("inscription")){ + Utilisateur u = Utilisateur.inscrire(login, mdp); + if(u != null) { + HttpSession session = request.getSession(); + session.setAttribute("utilisateur", u); + response.sendRedirect("AfficherPage"); + }else { + request.setAttribute("erreur", 2); + RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/Accueil.jsp"); + rd.forward(request, response); + } + } + } +} diff --git a/Projet/src/main/java/projet/AfficherPage.java b/Projet/src/main/java/projet/AfficherPage.java new file mode 100644 index 0000000..a69f6bd --- /dev/null +++ b/Projet/src/main/java/projet/AfficherPage.java @@ -0,0 +1,58 @@ +package projet; + +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import java.io.IOException; +import java.util.ArrayList; + +@WebServlet("/AfficherPage") +public class AfficherPage extends HttpServlet { + private static final long serialVersionUID = 1L; + + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + String idStr = request.getParameter("id"); + + if(u != null) { + u.chargerPages(); + ArrayList listePages = u.getListePages(); + + request.setAttribute("listePages", listePages); + + if (idStr != null ) { + try { + int id = Integer.parseInt(idStr); + Page page = Page.getPageById(u.getId(), id); + if (page != null) { + request.setAttribute("page", page); + request.getRequestDispatcher("/WEB-INF/AfficherPage.jsp").forward(request, response); + return; + } else { + request.getRequestDispatcher("/WEB-INF/AfficherPage.jsp").forward(request, response); + return; + } + } catch (NumberFormatException e) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST, "ID invalide"); + return; + } + } + RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/AfficherPage.jsp"); + rd.forward(request, response); + } else { + response.sendRedirect("/Projet/"); + } + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + } + +} diff --git a/Projet/src/main/java/projet/Bloc.java b/Projet/src/main/java/projet/Bloc.java new file mode 100644 index 0000000..8678b8d --- /dev/null +++ b/Projet/src/main/java/projet/Bloc.java @@ -0,0 +1,167 @@ +package projet; + +import java.sql.Connection; +import java.sql.Date; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.time.LocalDate; +import java.util.HashMap; +import java.util.Map; + +public class Bloc extends ParamBD { + private int id; + private Type type; + private String contenu; + private LocalDate dateCreation; + private LocalDate dateModification; + private int ordre; + private Map metadata; + + public enum Type { + TEXTE, + LISTE, + TITRE, + CODE, + PAGE + } + + public Bloc() { + + } + + public Bloc(int id, Type type, String contenu, int ordre) { + if (contenu == null || contenu.isEmpty()) { + throw new IllegalArgumentException("Le contenu ne peut pas être vide."); + } + this.id = id; + this.type = type; + this.contenu = contenu; + this.ordre = ordre; + this.dateCreation = LocalDate.now(); + this.dateModification = LocalDate.now(); + metadata = new HashMap<>(); + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + public String getContenu() { + return contenu; + } + + public void setContenu(String contenu) { + this.contenu = contenu; + } + + public LocalDate getDateCreation() { + return dateCreation; + } + + public void setDateCreation(LocalDate dateCreation) { + this.dateCreation = dateCreation; + } + + public LocalDate getDateModification() { + return dateModification; + } + + public void setDateModification(LocalDate dateModification) { + this.dateModification = dateModification; + } + + public int getOrdre() { + return ordre; + } + + public void setOrdre(int ordre) { + this.ordre = ordre; + } + + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + + public String toString() { + return "Bloc{id=" + id + ", type=" + type + ", contenu='" + contenu + "', dateCreation=" + dateCreation + ", dateModification=" + dateModification + ", ordre=" + ordre + "}"; + } + + protected static void ajouterBloc(int idU, int idP, String contenu, int ordre, Type type, LocalDate dl, Map metadata) { + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " INSERT INTO bloc(type, contenu, date_creation, date_modification, page_id, ordre, auteur_id, metadata)" + + " VALUES (?, ?, ?, ?, ?, ?, ?, ?)" + + ";"; + PreparedStatement pst = connexion.prepareStatement(sql); + pst.setString(1, type.name()); + pst.setString(2, contenu); + pst.setDate(3, Date.valueOf(dl)); + pst.setDate(4, Date.valueOf(dl)); + pst.setInt(5, idP); + pst.setInt(6, ordre); + pst.setInt(7, idU); + pst.setString(8, metadata.toString()); + pst.executeUpdate(); + + pst.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public static void updateBloc(int idBloc, String nouveauContenu) { + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " UPDATE bloc" + + " SET contenu = ?" + + ", date_modification = ?" + + " WHERE id = ?" + + ";"; + PreparedStatement pst = connexion.prepareStatement(sql); + pst.setString(1, nouveauContenu); + pst.setDate(2, Date.valueOf(LocalDate.now())); + pst.setInt(3, idBloc); + pst.executeUpdate(); + + pst.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public static void supprimerBloc(int idBloc, int idU) { + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " DELETE FROM bloc" + + " WHERE id = ? and auteur_id = ?" + + ";"; + PreparedStatement pst = connexion.prepareStatement(sql); + + pst.setInt(1, idBloc); + pst.setInt(2, idU); + pst.executeUpdate(); + + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/Projet/src/main/java/projet/Deconnexion.java b/Projet/src/main/java/projet/Deconnexion.java new file mode 100644 index 0000000..60d7701 --- /dev/null +++ b/Projet/src/main/java/projet/Deconnexion.java @@ -0,0 +1,26 @@ +package projet; + +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import java.io.IOException; + +@WebServlet("/Deconnexion") +public class Deconnexion extends HttpServlet { + private static final long serialVersionUID = 1L; + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + session.invalidate(); + response.sendRedirect("/Projet/"); + } + + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + } + +} diff --git a/Projet/src/main/java/projet/ModifBloc.java b/Projet/src/main/java/projet/ModifBloc.java new file mode 100644 index 0000000..8b792ce --- /dev/null +++ b/Projet/src/main/java/projet/ModifBloc.java @@ -0,0 +1,47 @@ +package projet; + +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import java.io.IOException; + +@WebServlet("/ModifBloc") +public class ModifBloc extends HttpServlet { + private static final long serialVersionUID = 1L; + + public ModifBloc() { + super(); + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + if(u != null) { + RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp"); + rd.forward(request, response); + }else { + response.sendRedirect("/Projet/"); + } + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + if(u != null) { + String contenu = request.getParameter("contenu"); + int blocId = Integer.parseInt(request.getParameter("blocId")); + + Bloc.updateBloc(blocId, contenu); + response.sendRedirect("AfficherPage"); + } else { + response.sendRedirect("/Projet/"); + } + } +} diff --git a/Projet/src/main/java/projet/NouveauBloc.java b/Projet/src/main/java/projet/NouveauBloc.java new file mode 100644 index 0000000..26eb109 --- /dev/null +++ b/Projet/src/main/java/projet/NouveauBloc.java @@ -0,0 +1,65 @@ +package projet; + +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import java.io.IOException; +import java.time.LocalDate; +import java.util.HashMap; +import java.util.Map; + +@WebServlet("/NouveauBloc") +public class NouveauBloc extends HttpServlet { + private static final long serialVersionUID = 1L; + + public NouveauBloc() { + super(); + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + if(u != null) { + RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp"); + rd.forward(request, response); + }else { + response.sendRedirect("/Projet/"); + } + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + if(u != null) { + request.setCharacterEncoding("UTF-8"); + + String contenu = request.getParameter("contenu"); + String typeStr = request.getParameter("type"); + String ordreStr = request.getParameter("ordre"); + String pageIdStr = request.getParameter("pageId"); + + int ordre = Integer.parseInt(ordreStr); + int pageId = Integer.parseInt(pageIdStr); + Bloc.Type type = Bloc.Type.valueOf(typeStr); + Map metadata = new HashMap<>(); + + if (contenu == null || contenu.isEmpty()) { + response.sendRedirect("AfficherPage"); + } else { + + Bloc.ajouterBloc(u.getId(), pageId, contenu, ordre, type, LocalDate.now(), metadata); + response.sendRedirect("AfficherPage"); + } + } else { + response.sendRedirect("/Projet/"); + } + } + +} diff --git a/Projet/src/main/java/projet/NouvellePage.java b/Projet/src/main/java/projet/NouvellePage.java new file mode 100644 index 0000000..064b6fc --- /dev/null +++ b/Projet/src/main/java/projet/NouvellePage.java @@ -0,0 +1,53 @@ +package projet; + +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import java.io.IOException; +import java.time.LocalDate; + +@WebServlet("/NouvellePage") +public class NouvellePage extends HttpServlet { + private static final long serialVersionUID = 1L; + + public NouvellePage() { + super(); + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + if(u != null) { + RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp"); + rd.forward(request, response); + }else { + response.sendRedirect("/Projet/"); + } + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + if(u != null) { + request.setCharacterEncoding("UTF-8"); + + String titre = request.getParameter("titre"); + + if (titre == null || titre.isEmpty()) { + response.sendRedirect("AfficherPage"); + } else { + Page.ajouterPage(u.getId(), titre, LocalDate.now()); + response.sendRedirect("AfficherPage"); + } + } else { + response.sendRedirect("/Projet/"); + } + } +} diff --git a/Projet/src/main/java/projet/Page.java b/Projet/src/main/java/projet/Page.java new file mode 100644 index 0000000..ff3b54e --- /dev/null +++ b/Projet/src/main/java/projet/Page.java @@ -0,0 +1,185 @@ +package projet; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.time.LocalDate; +import java.sql.Date; +import java.util.ArrayList; +import java.util.List; + +import projet.Bloc.Type; + +public class Page extends ParamBD { + private int id; + private String titre; + private LocalDate dateCreation; + private LocalDate dateModification; + private Droit droits; + private List listeBlocs; + + public enum Droit { + LECTURE, + ECRITURE, + ADMIN + } + + public Page() { + + } + + public Page(int id, String titre, LocalDate dateCreation, LocalDate dateModification, Droit droits) { + if (titre == null || titre.isEmpty()) { + throw new IllegalArgumentException("Le contenu ne peut pas être vide."); + } + this.id = id; + this.titre = titre; + this.dateCreation = dateCreation; + this.dateModification = dateModification; + this.droits = droits; + } + + public Page(int id, String titre) { + this(id, titre, null, null, Droit.ADMIN); // Appelle le constructeur avec droits = null + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitre() { + return titre; + } + + public void setTitre(String titre) { + this.titre = titre; + } + + public LocalDate getDateCreation() { + return dateCreation; + } + + public void setDateCreation(LocalDate dateCreation) { + this.dateCreation = dateCreation; + } + + public LocalDate getDateModification() { + return dateModification; + } + + public void setDateModification(LocalDate dateModification) { + this.dateModification = dateModification; + } + + public Droit getDroits() { + return droits; + } + + public void setDroits(Droit droits) { + this.droits = droits; + } + + public List getListeBlocs() { + return listeBlocs; + } + + public void setListeBlocs(List listeBlocs) { + this.listeBlocs = listeBlocs; + } + + public String toString() { + return "Page{id=" + id + ", titre='" + titre + "', dateCreation=" + dateCreation + ", dateModification=" + dateModification + ", droits=" + droits + "}"; + } + + protected static void ajouterPage(int idU, String t, LocalDate dl) { + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " INSERT INTO page(titre, date_creation, date_modification, auteur_id, droits)" + + " VALUES (?, ?, ?, ?, ?)" + + ";"; + PreparedStatement pst = connexion.prepareStatement(sql); + pst.setString(1, t); + pst.setDate(2, Date.valueOf(dl)); + pst.setDate(3, Date.valueOf(dl)); + pst.setInt(4, idU); + pst.setString(5, "ADMIN"); + pst.executeUpdate(); + + pst.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + protected static Page getPageById(int idU, int id) { + Page page = new Page(); + String titre = null; + String sql; + PreparedStatement pst; + ResultSet rs; + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + sql = " SELECT titre" + + " FROM page" + + " WHERE id=? AND auteur_id = ?" + + ";"; + pst = connexion.prepareStatement(sql); + pst.setInt(1, id); + pst.setInt(2, idU); + rs = pst.executeQuery(); + while(rs.next()) { + titre = rs.getString("titre"); + page.titre = titre; + } + if(titre != null) { + sql = " SELECT id, contenu, type, ordre" + + " FROM bloc" + + " WHERE page_id=?" + + " ORDER BY ordre" + + ";"; + pst = connexion.prepareStatement(sql); + pst.setInt(1, id); + rs = pst.executeQuery(); + page.listeBlocs = new ArrayList<>(); + while(rs.next()) { + int idB = rs.getInt("id"); + String contenu = rs.getString("contenu"); + String type = rs.getString("type"); + Type typeEnum = Type.valueOf(type.toUpperCase()); + int ordre = rs.getInt("ordre"); + + Bloc bloc = new Bloc(idB, typeEnum, contenu, ordre); + page.listeBlocs.add(bloc); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + + return page; + } + + public static void supprimerPage(int idPage, int idU) { + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " DELETE FROM page" + + " WHERE id = ? and auteur_id = ?" + + ";"; + PreparedStatement pst = connexion.prepareStatement(sql); + + pst.setInt(1, idPage); + pst.setInt(2, idU); + pst.executeUpdate(); + + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/Projet/src/main/java/projet/ParamBD.java b/Projet/src/main/java/projet/ParamBD.java new file mode 100644 index 0000000..60c388c --- /dev/null +++ b/Projet/src/main/java/projet/ParamBD.java @@ -0,0 +1,20 @@ +package projet; + +import jakarta.servlet.ServletContext; + +public class ParamBD { + protected static String bdURL; + protected static String bdLogin; + protected static String bdPassword; + + protected static void init(ServletContext context) { + try { + Class.forName(context.getInitParameter("JDBC_DRIVER")); + bdURL = context.getInitParameter("JDBC_URL"); + bdLogin = context.getInitParameter("JDBC_LOGIN"); + bdPassword = context.getInitParameter("JDBC_PASSWORD"); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } +} diff --git a/Projet/src/main/java/projet/SupprimerBloc.java b/Projet/src/main/java/projet/SupprimerBloc.java new file mode 100644 index 0000000..e307f0f --- /dev/null +++ b/Projet/src/main/java/projet/SupprimerBloc.java @@ -0,0 +1,52 @@ +package projet; + +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import java.io.IOException; + +@WebServlet("/SupprimerBloc") +public class SupprimerBloc extends HttpServlet { + private static final long serialVersionUID = 1L; + + public SupprimerBloc() { + super(); + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + if(u != null) { + RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp"); + rd.forward(request, response); + }else { + response.sendRedirect("/Projet/"); + } + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + if(u != null) { + String idBlocStr = request.getParameter("blocId"); + + if(idBlocStr == null || idBlocStr.isEmpty()) { + response.sendRedirect("AfficherPage"); + } else { + int idBloc = Integer.parseInt(idBlocStr); + Bloc.supprimerBloc(idBloc, u.getId()); + response.sendRedirect("AfficherPage"); + } + + } else { + response.sendRedirect("/Projet/"); + } + } +} diff --git a/Projet/src/main/java/projet/SupprimerPage.java b/Projet/src/main/java/projet/SupprimerPage.java new file mode 100644 index 0000000..283e340 --- /dev/null +++ b/Projet/src/main/java/projet/SupprimerPage.java @@ -0,0 +1,49 @@ +package projet; + +import jakarta.servlet.RequestDispatcher; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.servlet.http.HttpSession; + +import java.io.IOException; + +@WebServlet("/SupprimerPage") +public class SupprimerPage extends HttpServlet { + private static final long serialVersionUID = 1L; + + public SupprimerPage() { + super(); + } + + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + if(u != null) { + RequestDispatcher rd = request.getRequestDispatcher("AfficherPage.jsp"); + rd.forward(request, response); + }else { + response.sendRedirect("/Projet/"); + } + } + + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + HttpSession session = request.getSession(); + Utilisateur u = (Utilisateur) session.getAttribute("utilisateur"); + + if(u != null) { + String idPageStr = request.getParameter("pageId"); + + int idPage = Integer.parseInt(idPageStr); + + Page.supprimerPage(idPage, u.getId()); + response.sendRedirect("AfficherPage"); + } else { + response.sendRedirect("/Projet/"); + } + } + +} diff --git a/Projet/src/main/java/projet/Utilisateur.java b/Projet/src/main/java/projet/Utilisateur.java new file mode 100644 index 0000000..5d9be4e --- /dev/null +++ b/Projet/src/main/java/projet/Utilisateur.java @@ -0,0 +1,186 @@ +package projet; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.time.LocalDate; +import java.util.ArrayList; + +import projet.Bloc.Type; +import projet.Page.Droit; + + + +public class Utilisateur extends ParamBD { + private int id; + private String login; + private Privilege privilege; + private ArrayList listePages; + + public enum Privilege { + ADMIN, + USER, + GUEST + } + + public Utilisateur(int id, String login, Privilege privilege) { + if (login == null || login.isEmpty()) { + throw new IllegalArgumentException("Le pseudo ne peut pas être vide."); + } + this.id = id; + this.login = login; + this.privilege = privilege; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public Privilege getprivilege() { + return privilege; + } + + public void setprivilege(Privilege privilege) { + this.privilege = privilege; + } + + public String getLogin() { + return login; + } + + public void setLogin(String login) { + this.login = login; + } + + public Privilege getPrivilege() { + return privilege; + } + + public void setPrivilege(Privilege privilege) { + this.privilege = privilege; + } + + public ArrayList getListePages() { + return listePages; + } + + public void setListePages(ArrayList listePages) { + this.listePages = listePages; + } + + public String toString() { + return "Utilisateur{id=" + id + ", login='" + login + "', privilege=" + privilege + "}"; + } + + protected void chargerPages(){ + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " SELECT id, titre, date_creation, date_modification, droits" + + " FROM page" + + " WHERE auteur_id=" + + this.id + + ";"; + Statement st = connexion.createStatement(); + ResultSet rs = st.executeQuery(sql); + + listePages = new ArrayList(); + while(rs.next()) { + int idPage = rs.getInt("id"); + String titre = rs.getString("titre"); + LocalDate date_creation = rs.getDate("date_creation").toLocalDate(); + LocalDate date_modification = rs.getDate("date_modification").toLocalDate(); + String droits = rs.getString("droits"); + Droit droitsEnum = Droit.valueOf(droits.toUpperCase()); + + listePages.add(new Page(idPage, titre, date_creation, date_modification, droitsEnum)); + } + rs.close(); + st.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + protected static Utilisateur authentifier(String login, String mdp) { + Utilisateur u = null; + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = " SELECT id, privilege" + + " FROM utilisateur" + + " WHERE login=? AND mot_de_passe = ?" + + ";"; + PreparedStatement pst = connexion.prepareStatement(sql); + pst.setString(1, login); + pst.setString(2, mdp); + ResultSet rs = pst.executeQuery(); + while(rs.next()) { + int id = rs.getInt("id"); + String privilegeSQL = rs.getString("privilege"); + Privilege privilege = null; + if (privilegeSQL.equals("ADMIN")) privilege=Privilege.ADMIN; + else if (privilegeSQL.equals("USER")) privilege=Privilege.USER; + else if (privilegeSQL.equals("GUEST")) privilege=Privilege.GUEST; + if(id != 0) { + u = new Utilisateur(id, login, privilege); + } + } + rs.close(); + pst.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return u; + } + + public static Utilisateur inscrire(String login, String mdp) { + Utilisateur u = null; + try { + Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); + String sql = "SELECT id " + + " FROM utilisateur " + + " WHERE login = ?" + + ";"; + PreparedStatement pst = connexion.prepareStatement(sql); + pst.setString(1, login); + ResultSet rs = pst.executeQuery(); + + while(rs.next()) { + rs.close(); + pst.close(); + connexion.close(); + return u; + } + + sql = "INSERT INTO utilisateur (login, mot_de_passe) " + + " VALUES (?, ?)" + + ";"; + pst = connexion.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + pst.setString(1, login); + pst.setString(2, mdp); + + int ri = pst.executeUpdate(); + if (ri > 0) { + rs = pst.getGeneratedKeys(); + while(rs.next()) { + int id = rs.getInt(1); + u = new Utilisateur(id, login, Privilege.USER); + } + } + rs.close(); + pst.close(); + connexion.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return u; + } +} diff --git a/Projet/src/main/webapp/META-INF/MANIFEST.MF b/Projet/src/main/webapp/META-INF/MANIFEST.MF new file mode 100644 index 0000000..5e94951 --- /dev/null +++ b/Projet/src/main/webapp/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Class-Path: + diff --git a/Projet/src/main/webapp/WEB-INF/Accueil.jsp b/Projet/src/main/webapp/WEB-INF/Accueil.jsp new file mode 100644 index 0000000..776bcc1 --- /dev/null +++ b/Projet/src/main/webapp/WEB-INF/Accueil.jsp @@ -0,0 +1,78 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + + + + + + + + + +
+
+
+
+

Erreur

+
+
+

Mauvais login ou mot de passe.

+
+
+
+
+
+ + +
+
+
+
+

Erreur

+
+
+

Login déjà utilisé.

+
+
+
+
+
+ +
+
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ + + +
+
+
+ + + \ No newline at end of file diff --git a/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp b/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp new file mode 100644 index 0000000..e2533d1 --- /dev/null +++ b/Projet/src/main/webapp/WEB-INF/AfficherPage.jsp @@ -0,0 +1,191 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + + + + + + +
+
+ +
+
+ + +

${page.titre}

+
+ +
+
+ +
+
+ +
+
+
+ +
+
+ +
+
+ +
+
+
+
+ +

Pas encore de page choisie.

+
+
+
+
+ +
+
+ + + + diff --git a/Projet/src/main/webapp/WEB-INF/Footer.jsp b/Projet/src/main/webapp/WEB-INF/Footer.jsp new file mode 100644 index 0000000..64cafbc --- /dev/null +++ b/Projet/src/main/webapp/WEB-INF/Footer.jsp @@ -0,0 +1,12 @@ + + + Made with Bulma + + + + + \ No newline at end of file diff --git a/Projet/src/main/webapp/WEB-INF/Header.jsp b/Projet/src/main/webapp/WEB-INF/Header.jsp new file mode 100644 index 0000000..db8da4a --- /dev/null +++ b/Projet/src/main/webapp/WEB-INF/Header.jsp @@ -0,0 +1,45 @@ +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + + + + + + ${param.titre} + + + + + + +
\ No newline at end of file diff --git a/Projet/src/main/webapp/WEB-INF/MenuPages.jsp b/Projet/src/main/webapp/WEB-INF/MenuPages.jsp new file mode 100644 index 0000000..02f8127 --- /dev/null +++ b/Projet/src/main/webapp/WEB-INF/MenuPages.jsp @@ -0,0 +1,27 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + +

Menu des pages

+ \ No newline at end of file diff --git a/Projet/src/main/webapp/WEB-INF/Tchat.jsp b/Projet/src/main/webapp/WEB-INF/Tchat.jsp new file mode 100644 index 0000000..1ee4e7a --- /dev/null +++ b/Projet/src/main/webapp/WEB-INF/Tchat.jsp @@ -0,0 +1,10 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> + +

Tchat

+
+ +
\ No newline at end of file diff --git a/Projet/src/main/webapp/WEB-INF/lib/jakarta.servlet.jsp.jstl-2.0.0.jar b/Projet/src/main/webapp/WEB-INF/lib/jakarta.servlet.jsp.jstl-2.0.0.jar new file mode 100644 index 0000000..92712b0 Binary files /dev/null and b/Projet/src/main/webapp/WEB-INF/lib/jakarta.servlet.jsp.jstl-2.0.0.jar differ diff --git a/Projet/src/main/webapp/WEB-INF/lib/jakarta.servlet.jsp.jstl-api-2.0.0.jar b/Projet/src/main/webapp/WEB-INF/lib/jakarta.servlet.jsp.jstl-api-2.0.0.jar new file mode 100644 index 0000000..81059ec Binary files /dev/null and b/Projet/src/main/webapp/WEB-INF/lib/jakarta.servlet.jsp.jstl-api-2.0.0.jar differ diff --git a/Projet/src/main/webapp/WEB-INF/lib/mysql-connector-j-9.2.0.jar b/Projet/src/main/webapp/WEB-INF/lib/mysql-connector-j-9.2.0.jar new file mode 100644 index 0000000..9794444 Binary files /dev/null and b/Projet/src/main/webapp/WEB-INF/lib/mysql-connector-j-9.2.0.jar differ diff --git a/Projet/src/main/webapp/WEB-INF/web.xml b/Projet/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..94707bc --- /dev/null +++ b/Projet/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,24 @@ + + + Projet + + + + + + JDBC_DRIVER + com.mysql.cj.jdbc.Driver + + + JDBC_URL + jdbc:mysql://localhost:3306/projet + + + JDBC_LOGIN + webuser + + + JDBC_PASSWORD + multipass + + \ No newline at end of file diff --git a/Projet/src/main/webapp/bulma.css b/Projet/src/main/webapp/bulma.css new file mode 100644 index 0000000..7863a7f --- /dev/null +++ b/Projet/src/main/webapp/bulma.css @@ -0,0 +1,2 @@ +@charset "UTF-8"; +@import "https://cdn.jsdelivr.net/npm/bulma@1.0.2/css/bulma.min.css"; \ No newline at end of file