2026-06-01 20:49:07 +02:00
|
|
|
const { getDatabase } = require('../config/database');
|
|
|
|
|
|
|
|
|
|
class AuditLog {
|
|
|
|
|
/**
|
|
|
|
|
* Create an audit log entry
|
|
|
|
|
*/
|
|
|
|
|
static create(logData) {
|
|
|
|
|
const db = getDatabase();
|
|
|
|
|
const stmt = db.prepare(`
|
|
|
|
|
INSERT INTO audit_log (
|
|
|
|
|
user_id,
|
|
|
|
|
action,
|
|
|
|
|
entity_type,
|
|
|
|
|
entity_id,
|
|
|
|
|
old_value,
|
|
|
|
|
new_value,
|
|
|
|
|
ip_address
|
|
|
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
|
|
|
`);
|
|
|
|
|
|
|
|
|
|
const result = stmt.run(
|
|
|
|
|
logData.user_id,
|
|
|
|
|
logData.action,
|
|
|
|
|
logData.entity_type,
|
|
|
|
|
logData.entity_id,
|
|
|
|
|
logData.old_value ? JSON.stringify(logData.old_value) : null,
|
|
|
|
|
logData.new_value ? JSON.stringify(logData.new_value) : null,
|
|
|
|
|
logData.ip_address || null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return result.lastInsertRowid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get all audit logs with pagination
|
|
|
|
|
*/
|
|
|
|
|
static getAll(limit = 100, offset = 0) {
|
|
|
|
|
const db = getDatabase();
|
|
|
|
|
const stmt = db.prepare(`
|
|
|
|
|
SELECT
|
|
|
|
|
al.*,
|
|
|
|
|
u.username,
|
|
|
|
|
u.email
|
|
|
|
|
FROM audit_log al
|
|
|
|
|
LEFT JOIN users u ON al.user_id = u.id
|
|
|
|
|
ORDER BY al.created_at DESC
|
|
|
|
|
LIMIT ? OFFSET ?
|
|
|
|
|
`);
|
|
|
|
|
return stmt.all(limit, offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get audit logs for a specific user
|
|
|
|
|
*/
|
|
|
|
|
static getByUserId(userId, limit = 100, offset = 0) {
|
|
|
|
|
const db = getDatabase();
|
|
|
|
|
const stmt = db.prepare(`
|
|
|
|
|
SELECT
|
|
|
|
|
al.*,
|
|
|
|
|
u.username,
|
|
|
|
|
u.email
|
|
|
|
|
FROM audit_log al
|
|
|
|
|
LEFT JOIN users u ON al.user_id = u.id
|
|
|
|
|
WHERE al.user_id = ?
|
|
|
|
|
ORDER BY al.created_at DESC
|
|
|
|
|
LIMIT ? OFFSET ?
|
|
|
|
|
`);
|
|
|
|
|
return stmt.all(userId, limit, offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get audit logs for a specific entity
|
|
|
|
|
*/
|
|
|
|
|
static getByEntity(entityType, entityId, limit = 100, offset = 0) {
|
|
|
|
|
const db = getDatabase();
|
|
|
|
|
const stmt = db.prepare(`
|
|
|
|
|
SELECT
|
|
|
|
|
al.*,
|
|
|
|
|
u.username,
|
|
|
|
|
u.email
|
|
|
|
|
FROM audit_log al
|
|
|
|
|
LEFT JOIN users u ON al.user_id = u.id
|
|
|
|
|
WHERE al.entity_type = ? AND al.entity_id = ?
|
|
|
|
|
ORDER BY al.created_at DESC
|
|
|
|
|
LIMIT ? OFFSET ?
|
|
|
|
|
`);
|
|
|
|
|
return stmt.all(entityType, entityId, limit, offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get recent audit logs
|
|
|
|
|
*/
|
|
|
|
|
static getRecent(limit = 10) {
|
|
|
|
|
const db = getDatabase();
|
|
|
|
|
const stmt = db.prepare(`
|
|
|
|
|
SELECT
|
|
|
|
|
al.*,
|
|
|
|
|
u.username,
|
|
|
|
|
u.email
|
|
|
|
|
FROM audit_log al
|
|
|
|
|
LEFT JOIN users u ON al.user_id = u.id
|
|
|
|
|
ORDER BY al.created_at DESC
|
|
|
|
|
LIMIT ?
|
|
|
|
|
`);
|
|
|
|
|
return stmt.all(limit);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-26 13:27:17 +02:00
|
|
|
/**
|
|
|
|
|
* Löscht Audit-Log-Einträge älter als retentionDays (DSGVO Art. 5 Abs. 1 lit. e - Speicherbegrenzung)
|
|
|
|
|
*/
|
|
|
|
|
static cleanupOld(retentionDays = 180) {
|
|
|
|
|
const db = getDatabase();
|
|
|
|
|
const stmt = db.prepare(`DELETE FROM audit_log WHERE created_at < datetime('now', '-' || ? || ' days')`);
|
|
|
|
|
const result = stmt.run(retentionDays);
|
|
|
|
|
return result.changes;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-01 20:49:07 +02:00
|
|
|
/**
|
|
|
|
|
* Helper method to log user actions
|
|
|
|
|
*/
|
|
|
|
|
static logUserAction(userId, action, entityType, entityId, oldValue = null, newValue = null, ipAddress = null) {
|
|
|
|
|
return this.create({
|
|
|
|
|
user_id: userId,
|
|
|
|
|
action,
|
|
|
|
|
entity_type: entityType,
|
|
|
|
|
entity_id: entityId,
|
|
|
|
|
old_value: oldValue,
|
|
|
|
|
new_value: newValue,
|
|
|
|
|
ip_address: ipAddress
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = AuditLog;
|