2026-03-15 21:07:22 +00:00
|
|
|
// middleware/auditLogger.js
|
2026-03-16 00:07:16 +00:00
|
|
|
const db = require('../db');
|
2026-03-15 21:07:22 +00:00
|
|
|
|
2026-03-16 00:07:16 +00:00
|
|
|
/**
|
|
|
|
|
* Middleware zur Protokollierung sensibler Aktionen
|
|
|
|
|
* @param {string} action - Name der Aktion (z.B. 'USER_SUSPEND')
|
|
|
|
|
* @param {string} targetType - Typ des Zielobjekts (z.B. 'user')
|
|
|
|
|
* @returns {function} Express-Middleware-Funktion
|
|
|
|
|
*/
|
|
|
|
|
function auditLogger(action, targetType) {
|
|
|
|
|
return async (req, res, next) => {
|
|
|
|
|
try {
|
|
|
|
|
const timestamp = new Date().toISOString();
|
|
|
|
|
const actorUserId = req.user?.id || null;
|
|
|
|
|
const targetId = req.params.id || req.body.id || null;
|
|
|
|
|
const reason = req.body.reason || null;
|
2026-03-15 21:07:22 +00:00
|
|
|
|
2026-03-16 00:07:16 +00:00
|
|
|
// Audit-Eintrag in die Datenbank schreiben
|
|
|
|
|
await db.run(
|
|
|
|
|
'INSERT INTO audit_log (timestamp, actor_user_id, action, target_type, target_id, reason) VALUES (?, ?, ?, ?, ?, ?)',
|
|
|
|
|
[timestamp, actorUserId, action, targetType, targetId, reason]
|
|
|
|
|
);
|
2026-03-15 21:07:22 +00:00
|
|
|
|
2026-03-16 00:07:16 +00:00
|
|
|
next();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error('Audit logging failed:', err);
|
|
|
|
|
// Fehler bei Audit-Logging sollte nicht den Request blockieren
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
};
|
2026-03-15 21:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = auditLogger;
|