Ajout historique_bloc

This commit is contained in:
Lensors 2025-05-03 08:28:53 +02:00
parent 0993140ffd
commit 9277154e0c
5 changed files with 66 additions and 12 deletions

View File

@ -2,6 +2,7 @@ CREATE DATABASE IF NOT EXISTS projet;
USE projet; USE projet;
DROP TABLE IF EXISTS bloc; DROP TABLE IF EXISTS bloc;
DROP TABLE IF EXISTS historique_bloc;
DROP TABLE IF EXISTS partage; DROP TABLE IF EXISTS partage;
DROP TABLE IF EXISTS page; DROP TABLE IF EXISTS page;
DROP TABLE IF EXISTS message; DROP TABLE IF EXISTS message;
@ -40,6 +41,20 @@ CREATE TABLE bloc (
FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) ON DELETE CASCADE FOREIGN KEY (auteur_id) REFERENCES utilisateur(id) ON DELETE CASCADE
); );
CREATE TABLE historique_bloc (
id INT PRIMARY KEY AUTO_INCREMENT,
bloc_id INT NOT NULL,
version INT NOT NULL,
contenu TEXT,
date_modification DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
modifie_par INT,
metadata JSON NOT NULL,
ordre INT NOT NULL,
type ENUM('TEXTE', 'LISTE', 'TITRE', 'CODE', 'PAGE', 'SEPARATEUR', 'CITATION') NOT NULL,
FOREIGN KEY (bloc_id) REFERENCES bloc(id) ON DELETE CASCADE,
FOREIGN KEY (modifie_par) REFERENCES utilisateur(id) ON DELETE SET NULL
);
CREATE TABLE message ( CREATE TABLE message (
id INT AUTO_INCREMENT PRIMARY KEY, id INT AUTO_INCREMENT PRIMARY KEY,
login VARCHAR(100) NOT NULL, login VARCHAR(100) NOT NULL,
@ -58,6 +73,8 @@ CREATE TABLE partage (
UNIQUE(page_id, utilisateur_id) -- Empêche les doublons de partage UNIQUE(page_id, utilisateur_id) -- Empêche les doublons de partage
); );
INSERT INTO utilisateur (login, mot_de_passe, privilege)
VALUES ('root', SHA2('123', 256), 'ADMIN');
CREATE INDEX idx_page_auteur ON page(auteur_id); CREATE INDEX idx_page_auteur ON page(auteur_id);
CREATE INDEX idx_bloc_page ON bloc(page_id); CREATE INDEX idx_bloc_page ON bloc(page_id);

View File

@ -159,22 +159,35 @@ public class Bloc extends ParamBD {
} }
public static void updateBloc(int idBloc, String nouveauContenu, String type, String metadata) { public static void updateBloc(int idBloc, String nouveauContenu, String type, String metadata, int idU) {
String sql;
PreparedStatement pst;
try { try {
Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword); Connection connexion = DriverManager.getConnection(bdURL, bdLogin, bdPassword);
String sql = " UPDATE bloc"
+ " SET contenu = ?" // 1. Insérer dans l'historique
+ ", type = ?" sql =
+ ", metadata = ?" "INSERT INTO historique_bloc (bloc_id, version, contenu, date_modification, modifie_par, metadata, ordre, type) " +
+ ", date_modification = ?" "SELECT b.id, COALESCE(MAX(h.version), 0) + 1, b.contenu, NOW(), ?, b.metadata, b.ordre, b.type " +
"FROM bloc b " +
"LEFT JOIN historique_bloc h ON h.bloc_id = b.id " +
"WHERE b.id = ? " +
"GROUP BY b.id, b.contenu, b.metadata, b.ordre, b.type";
pst = connexion.prepareStatement(sql);
pst.setInt(1, idU);
pst.setInt(2, idBloc);
pst.executeUpdate();
// 2. Mise à jour du bloc
sql = " UPDATE bloc"
+ " SET contenu = ?, type = ?, metadata = ?, date_modification = NOW()"
+ " WHERE id = ?" + " WHERE id = ?"
+ ";"; + ";";
PreparedStatement pst = connexion.prepareStatement(sql); pst = connexion.prepareStatement(sql);
pst.setString(1, nouveauContenu); pst.setString(1, nouveauContenu);
pst.setString(2, type); pst.setString(2, type);
pst.setString(3, metadata); pst.setString(3, metadata);
pst.setDate(4, Date.valueOf(LocalDate.now())); pst.setInt(4, idBloc);
pst.setInt(5, idBloc);
pst.executeUpdate(); pst.executeUpdate();
pst.close(); pst.close();

View File

@ -0,0 +1,24 @@
package projet;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Hasher {
public static String hashPassword(String password) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hashedBytes = md.digest(password.getBytes());
StringBuilder sb = new StringBuilder();
for (byte b : hashedBytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Erreur lors du hachage", e);
}
}
}

View File

@ -41,7 +41,7 @@ public class ModifBloc extends HttpServlet {
int blocId = Integer.parseInt(request.getParameter("blocId")); int blocId = Integer.parseInt(request.getParameter("blocId"));
Bloc.updateBloc(blocId, contenu, type, metadata); Bloc.updateBloc(blocId, contenu, type, metadata, u.getId());
response.sendRedirect("AfficherPage"); response.sendRedirect("AfficherPage");
} else { } else {
response.sendRedirect("/Projet/"); response.sendRedirect("/Projet/");

View File

@ -159,7 +159,7 @@ public class Utilisateur extends ParamBD {
+ ";"; + ";";
PreparedStatement pst = connexion.prepareStatement(sql); PreparedStatement pst = connexion.prepareStatement(sql);
pst.setString(1, login); pst.setString(1, login);
pst.setString(2, mdp); pst.setString(2, Hasher.hashPassword(mdp));
ResultSet rs = pst.executeQuery(); ResultSet rs = pst.executeQuery();
while(rs.next()) { while(rs.next()) {
int id = rs.getInt("id"); int id = rs.getInt("id");
@ -205,7 +205,7 @@ public class Utilisateur extends ParamBD {
+ ";"; + ";";
pst = connexion.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); pst = connexion.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
pst.setString(1, login); pst.setString(1, login);
pst.setString(2, mdp); pst.setString(2, Hasher.hashPassword(mdp));
int ri = pst.executeUpdate(); int ri = pst.executeUpdate();
if (ri > 0) { if (ri > 0) {