feat(admin): Registro de transacciones — log de auditoría de todo el sistema
NUEVA INFRAESTRUCTURA * SQL 010: tabla audit_log (id, fecha, user_email, user_name, modulo, accion, entity_type, entity_id, entity_label, detalles JSON, ip_addr) con índices por fecha/user/modulo/accion/entity. * src/AuditLog.php: helper estático con dos métodos: - log() lee actor de $_SESSION (uso normal post-auth) - logAs() recibe email/name explícito (para login/logout y CLI) Falla silenciosamente si la DB no responde — no rompe la operación principal por un fallo de logging. HOOKS EN MUTACIONES * Auth.php: registra 'login' al completar OAuth y 'logout' antes de destruir la sesión. * escrituras/api.php: registra create/update/delete con id+nombre. * tramites/api.php: registra 8 mutaciones — create/update/delete del trámite, set_status (con nuevo estatus en detalles), add_obs, add_hours/update_hours/delete_hours (con abogado y horas en detalles), todos referenciados por codigo (ADM-001, etc.). * admin/api.php: registra create_user/update_user/delete_user, con diff de cambios en update_user. * GETs (list_*) no se loguean — serían ruido. LECTOR * admin/api.php nuevo endpoint GET ?action=list_log que devuelve entradas con filtros from/to/user/modulo/accion/search (LIKE) + catálogos para dropdowns. Límite 500 por query. UI EN PANEL DE CONTROL * admin.html ahora tiene tabs: "Usuarios" (existente) y "Registro de transacciones" (nuevo). * Tab Registro: toolbar de filtros (rango fechas, usuario, módulo, acción, búsqueda libre, atajos Hoy/Semana/Mes/Todo) + tabla con Fecha·Hora · Usuario · Módulo (pill coloreado) · Acción · Entidad con label y tipo · Detalles JSON resumido.
This commit is contained in:
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
require_once __DIR__ . '/../../src/Auth.php';
|
||||
require_once __DIR__ . '/../../src/Db.php';
|
||||
require_once __DIR__ . '/../../src/AuditLog.php';
|
||||
|
||||
if (($_GET['action'] ?? '') === 'logout') {
|
||||
header('Location: /auth/logout.php');
|
||||
@@ -73,7 +74,9 @@ try {
|
||||
|
||||
$row = $pdo->prepare('SELECT * FROM escrituras_tramites WHERE id = :id');
|
||||
$row->execute([':id' => $id]);
|
||||
ok($row->fetch());
|
||||
$r = $row->fetch();
|
||||
AuditLog::log('escrituras', 'create', 'escritura', (string)$id, $r['tramite'] ?? '');
|
||||
ok($r);
|
||||
}
|
||||
|
||||
if ($method === 'PUT' && $action === 'update') {
|
||||
@@ -148,13 +151,20 @@ try {
|
||||
|
||||
$row = $pdo->prepare('SELECT * FROM escrituras_tramites WHERE id = :id');
|
||||
$row->execute([':id' => $id]);
|
||||
ok($row->fetch());
|
||||
$r = $row->fetch();
|
||||
AuditLog::log('escrituras', 'update', 'escritura', (string)$id, $r['tramite'] ?? '');
|
||||
ok($r);
|
||||
}
|
||||
|
||||
if ($method === 'DELETE' && $action === 'delete') {
|
||||
$id = (int) ($_GET['id'] ?? 0);
|
||||
if (!$id) err('ID requerido');
|
||||
// Fetch name before delete for the log
|
||||
$nameStmt = $pdo->prepare('SELECT tramite FROM escrituras_tramites WHERE id = :id');
|
||||
$nameStmt->execute([':id' => $id]);
|
||||
$tramiteName = (string)($nameStmt->fetchColumn() ?: '');
|
||||
$pdo->prepare('DELETE FROM escrituras_tramites WHERE id = :id')->execute([':id' => $id]);
|
||||
AuditLog::log('escrituras', 'delete', 'escritura', (string)$id, $tramiteName);
|
||||
ok(['deleted' => $id]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user