430 lines
26 KiB
HTML
430 lines
26 KiB
HTML
{% extends "base.html" %}
|
|
|
|
{% block title %}Registro de Auditoría Institucional - Edu-Space Admin{% endblock %}
|
|
|
|
{% block content %}
|
|
<div class="container-fluid pt-5 mt-4">
|
|
<!-- Header Institucional -->
|
|
<div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
|
|
<div>
|
|
<h1 class="h3 mb-1">
|
|
<i class="bi bi-shield-check text-primary me-2"></i>Registro de Auditoría Institucional
|
|
</h1>
|
|
<p class="text-muted small mb-0">Trazabilidad inmutable de inicios de sesión, cambios en espacios, reservas y operaciones del sistema</p>
|
|
</div>
|
|
<div class="d-flex gap-2">
|
|
<a href="/admin/audit_logs" class="btn btn-outline-secondary btn-sm" title="Recargar eventos">
|
|
<i class="bi bi-arrow-clockwise me-1"></i>Actualizar
|
|
</a>
|
|
<a href="/main/dashboard" class="btn btn-outline-primary btn-sm">
|
|
<i class="bi bi-arrow-left me-1"></i>Volver al Panel
|
|
</a>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Filtros y Búsqueda -->
|
|
<div class="card border-0 shadow-sm mb-4">
|
|
<div class="card-body p-3">
|
|
<form method="GET" action="/admin/audit_logs" class="row g-2 align-items-center">
|
|
<div class="col-md-4">
|
|
<div class="input-group input-group-sm">
|
|
<span class="input-group-text bg-transparent"><i class="bi bi-search"></i></span>
|
|
<input type="text" name="search" class="form-control" placeholder="Buscar por usuario, IP, acción o detalle..." value="{{ search }}">
|
|
</div>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<select name="module" class="form-select form-select-sm">
|
|
<option value="">Todos los módulos</option>
|
|
{% for mod in modules %}
|
|
<option value="{{ mod }}" {% if current_module == mod %}selected{% endif %}>
|
|
{% if mod == 'auth' %}Autenticación y Sesiones
|
|
{% elif mod == 'reservations' %}Reservas de Aulas
|
|
{% elif mod == 'classrooms' %}Gestión de Aulas
|
|
{% elif mod == 'buildings' %}Sedes y Edificios
|
|
{% elif mod == 'optimizer' %}Optimizador Genético IA
|
|
{% elif mod == 'users' %}Usuarios y Roles
|
|
{% elif mod == 'academic' %}Ciclos y Carreras
|
|
{% elif mod == 'admin' %}Sistema y Reinicio
|
|
{% else %}{{ mod|capitalize }}
|
|
{% endif %}
|
|
</option>
|
|
{% endfor %}
|
|
</select>
|
|
</div>
|
|
<div class="col-md-3">
|
|
<select name="action" class="form-select form-select-sm">
|
|
<option value="">Todas las acciones</option>
|
|
{% for act in actions %}
|
|
<option value="{{ act }}" {% if current_action == act %}selected{% endif %}>
|
|
{% if act == 'LOGIN' %}Inicio de Sesión (Local)
|
|
{% elif act == 'LOGIN_GOOGLE' %}Inicio de Sesión (Google)
|
|
{% elif act == 'LOGIN_MOODLE' %}Inicio de Sesión (Moodle)
|
|
{% elif act == 'LOGIN_FAILED' %}Inicio de Sesión Fallido
|
|
{% elif act == 'LOGIN_BLOCKED' %}Acceso Bloqueado
|
|
{% elif act == 'LOGOUT' %}Cierre de Sesión
|
|
{% elif act == 'PASSWORD_CHANGE' %}Cambio de Contraseña
|
|
{% elif act == 'IMPERSONATE_START' %}Inicio Impersonación
|
|
{% elif act == 'IMPERSONATE_END' %}Fin Impersonación
|
|
{% elif act == 'CREATE' %}Creación (Alta)
|
|
{% elif act == 'UPDATE' %}Modificación (Edición)
|
|
{% elif act == 'DELETE' %}Eliminación (Baja)
|
|
{% elif act == 'CANCEL' %}Cancelación
|
|
{% elif act == 'CONFIRM' %}Confirmación
|
|
{% elif act == 'TOGGLE' %}Alternar Estado
|
|
{% elif act == 'OPTIMIZE_RUN' %}Ejecución Optimizador IA
|
|
{% elif act == 'OPTIMIZE_APPLY' %}Aplicación Optimizador IA
|
|
{% elif act == 'SYSTEM_RESET' %}Reinicio del Sistema
|
|
{% else %}{{ act }}
|
|
{% endif %}
|
|
</option>
|
|
{% endfor %}
|
|
</select>
|
|
</div>
|
|
<div class="col-md-2 d-flex gap-1">
|
|
<button type="submit" class="btn btn-primary btn-sm flex-grow-1">
|
|
<i class="bi bi-funnel me-1"></i>Filtrar
|
|
</button>
|
|
{% if search or current_module or current_action %}
|
|
<a href="/admin/audit_logs" class="btn btn-outline-secondary btn-sm" title="Limpiar filtros">
|
|
<i class="bi bi-x-lg"></i>
|
|
</a>
|
|
{% endif %}
|
|
</div>
|
|
</form>
|
|
|
|
<!-- Acceso Rápido por Categorías -->
|
|
<div class="d-flex flex-wrap gap-1 mt-3 pt-2 border-top">
|
|
<span class="text-muted small align-self-center me-2"><i class="bi bi-lightning-charge me-1"></i>Filtros rápidos:</span>
|
|
<a href="/admin/audit_logs?module=auth" class="badge rounded-pill text-decoration-none {% if current_module == 'auth' %}bg-primary text-white{% else %}bg-light text-dark border{% endif %}">
|
|
<i class="bi bi-key me-1"></i>Sesiones y Accesos
|
|
</a>
|
|
<a href="/admin/audit_logs?action=LOGIN_FAILED" class="badge rounded-pill text-decoration-none {% if current_action == 'LOGIN_FAILED' %}bg-danger text-white{% else %}bg-light text-danger border{% endif %}">
|
|
<i class="bi bi-shield-x me-1"></i>Fallos de Login
|
|
</a>
|
|
<a href="/admin/audit_logs?module=reservations" class="badge rounded-pill text-decoration-none {% if current_module == 'reservations' %}bg-primary text-white{% else %}bg-light text-dark border{% endif %}">
|
|
<i class="bi bi-calendar-event me-1"></i>Reservas
|
|
</a>
|
|
<a href="/admin/audit_logs?module=classrooms" class="badge rounded-pill text-decoration-none {% if current_module == 'classrooms' %}bg-primary text-white{% else %}bg-light text-dark border{% endif %}">
|
|
<i class="bi bi-door-open me-1"></i>Aulas
|
|
</a>
|
|
<a href="/admin/audit_logs?module=buildings" class="badge rounded-pill text-decoration-none {% if current_module == 'buildings' %}bg-primary text-white{% else %}bg-light text-dark border{% endif %}">
|
|
<i class="bi bi-building me-1"></i>Sedes
|
|
</a>
|
|
<a href="/admin/audit_logs?module=optimizer" class="badge rounded-pill text-decoration-none {% if current_module == 'optimizer' %}bg-primary text-white{% else %}bg-light text-dark border{% endif %}">
|
|
<i class="bi bi-cpu me-1"></i>Optimizador IA
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Tabla de Auditoría -->
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="card-body p-0">
|
|
{% if audit_logs and audit_logs.length > 0 %}
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead class="table-light small text-uppercase">
|
|
<tr>
|
|
<th class="ps-3" style="width: 170px;">Fecha / Hora</th>
|
|
<th style="min-width: 180px;">Usuario</th>
|
|
<th style="width: 160px;">Acción</th>
|
|
<th style="width: 130px;">Módulo</th>
|
|
<th style="width: 90px;">ID</th>
|
|
<th>Detalles de la Operación</th>
|
|
<th class="pe-3 text-end" style="width: 130px;">IP Origen</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{% for log in audit_logs %}
|
|
<tr>
|
|
<td class="ps-3 text-nowrap">
|
|
<span class="small font-monospace text-dark fw-semibold">
|
|
{{ log.created_at or log.created_at_iso or '-' }}
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<div class="d-flex align-items-center gap-2">
|
|
<div class="avatar-sm rounded-circle d-flex align-items-center justify-content-center small {% if 'FAILED' in log.action or 'BLOCKED' in log.action %}bg-danger-subtle text-danger{% elif 'LOGIN' in log.action %}bg-success-subtle text-success{% else %}bg-primary-subtle text-primary{% endif %}" style="width: 28px; height: 28px;">
|
|
{% if 'LOGIN' in log.action %}
|
|
<i class="bi bi-person-check"></i>
|
|
{% elif 'FAILED' in log.action %}
|
|
<i class="bi bi-person-x"></i>
|
|
{% elif 'IMPERSONATE' in log.action %}
|
|
<i class="bi bi-person-badge"></i>
|
|
{% else %}
|
|
<i class="bi bi-person"></i>
|
|
{% endif %}
|
|
</div>
|
|
<div>
|
|
<div class="fw-semibold small text-truncate" style="max-width: 200px;" title="{{ log.user_email or 'Sistema' }}">
|
|
{{ log.user_email or 'Sistema' }}
|
|
</div>
|
|
{% if log.user_id %}
|
|
<span class="text-muted" style="font-size: 0.75rem;">ID Usuario: #{{ log.user_id }}</span>
|
|
{% endif %}
|
|
</div>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
{% if log.action == 'CREATE' %}
|
|
<span class="badge bg-success-subtle text-success border border-success-subtle px-2 py-1">
|
|
<i class="bi bi-plus-circle me-1"></i>ALTA
|
|
</span>
|
|
{% elif log.action == 'UPDATE' %}
|
|
<span class="badge bg-primary-subtle text-primary border border-primary-subtle px-2 py-1">
|
|
<i class="bi bi-pencil-square me-1"></i>EDICIÓN
|
|
</span>
|
|
{% elif log.action == 'DELETE' %}
|
|
<span class="badge bg-danger-subtle text-danger border border-danger-subtle px-2 py-1">
|
|
<i class="bi bi-trash me-1"></i>BAJA
|
|
</span>
|
|
{% elif log.action == 'CANCEL' %}
|
|
<span class="badge bg-warning-subtle text-warning-emphasis border border-warning-subtle px-2 py-1">
|
|
<i class="bi bi-dash-circle me-1"></i>CANCELACIÓN
|
|
</span>
|
|
{% elif log.action == 'CONFIRM' %}
|
|
<span class="badge bg-info-subtle text-info-emphasis border border-info-subtle px-2 py-1">
|
|
<i class="bi bi-check-circle me-1"></i>CONFIRMADA
|
|
</span>
|
|
{% elif log.action == 'TOGGLE' %}
|
|
<span class="badge bg-secondary-subtle text-secondary border px-2 py-1">
|
|
<i class="bi bi-toggle-on me-1"></i>ESTADO
|
|
</span>
|
|
{% elif log.action == 'LOGIN' %}
|
|
<span class="badge bg-success-subtle text-success border border-success-subtle px-2 py-1">
|
|
<i class="bi bi-box-arrow-in-right me-1"></i>LOGIN LOCAL
|
|
</span>
|
|
{% elif log.action == 'LOGIN_GOOGLE' %}
|
|
<span class="badge bg-info-subtle text-primary border border-info-subtle px-2 py-1">
|
|
<i class="bi bi-google me-1"></i>LOGIN GOOGLE
|
|
</span>
|
|
{% elif log.action == 'LOGIN_MOODLE' %}
|
|
<span class="badge bg-warning-subtle text-dark border border-warning-subtle px-2 py-1">
|
|
<i class="bi bi-mortarboard me-1"></i>LOGIN MOODLE
|
|
</span>
|
|
{% elif log.action == 'LOGIN_FAILED' %}
|
|
<span class="badge bg-danger text-white px-2 py-1">
|
|
<i class="bi bi-shield-x me-1"></i>LOGIN FALLIDO
|
|
</span>
|
|
{% elif log.action == 'LOGIN_BLOCKED' %}
|
|
<span class="badge bg-danger-subtle text-danger border border-danger-subtle px-2 py-1">
|
|
<i class="bi bi-slash-circle me-1"></i>BLOQUEADO
|
|
</span>
|
|
{% elif log.action == 'LOGOUT' %}
|
|
<span class="badge bg-secondary-subtle text-secondary border px-2 py-1">
|
|
<i class="bi bi-box-arrow-right me-1"></i>LOGOUT
|
|
</span>
|
|
{% elif log.action == 'PASSWORD_CHANGE' %}
|
|
<span class="badge bg-purple-subtle text-purple border px-2 py-1" style="background-color: #f3e8ff; color: #6b21a8;">
|
|
<i class="bi bi-key me-1"></i>CAMBIO CLAVE
|
|
</span>
|
|
{% elif log.action == 'IMPERSONATE_START' %}
|
|
<span class="badge bg-warning text-dark px-2 py-1">
|
|
<i class="bi bi-person-badge me-1"></i>IMPERSONACIÓN
|
|
</span>
|
|
{% elif log.action == 'IMPERSONATE_END' %}
|
|
<span class="badge bg-light text-dark border px-2 py-1">
|
|
<i class="bi bi-person-check me-1"></i>FIN IMPERSON.
|
|
</span>
|
|
{% elif log.action == 'OPTIMIZE_RUN' %}
|
|
<span class="badge bg-info text-white px-2 py-1">
|
|
<i class="bi bi-cpu me-1"></i>CORRIDA IA
|
|
</span>
|
|
{% elif log.action == 'OPTIMIZE_APPLY' %}
|
|
<span class="badge bg-success text-white px-2 py-1">
|
|
<i class="bi bi-magic me-1"></i>APLICACIÓN IA
|
|
</span>
|
|
{% elif log.action == 'SYSTEM_RESET' %}
|
|
<span class="badge bg-danger text-white px-2 py-1">
|
|
<i class="bi bi-arrow-counterclockwise me-1"></i>REINICIO SISTEMA
|
|
</span>
|
|
{% else %}
|
|
<span class="badge bg-secondary-subtle text-secondary border px-2 py-1">{{ log.action }}</span>
|
|
{% endif %}
|
|
</td>
|
|
<td>
|
|
{% if log.module == 'auth' %}
|
|
<span class="badge bg-primary-subtle text-primary border font-monospace small">auth</span>
|
|
{% elif log.module == 'reservations' %}
|
|
<span class="badge bg-info-subtle text-info border font-monospace small">reservas</span>
|
|
{% elif log.module == 'classrooms' %}
|
|
<span class="badge bg-success-subtle text-success border font-monospace small">aulas</span>
|
|
{% elif log.module == 'buildings' %}
|
|
<span class="badge bg-warning-subtle text-dark border font-monospace small">sedes</span>
|
|
{% elif log.module == 'optimizer' %}
|
|
<span class="badge bg-dark text-white font-monospace small">optimizador</span>
|
|
{% else %}
|
|
<span class="badge bg-body-secondary text-body border font-monospace small">{{ log.module }}</span>
|
|
{% endif %}
|
|
</td>
|
|
<td class="font-monospace small text-muted">
|
|
{{ log.entity_id or '-' }}
|
|
</td>
|
|
<td>
|
|
<div class="d-flex align-items-center justify-content-between gap-2">
|
|
<div class="small text-muted text-truncate" style="max-width: 480px;" title="{{ log.details }}">
|
|
{{ log.details or '-' }}
|
|
</div>
|
|
{% if log.details and log.details.length > 50 %}
|
|
<button type="button" class="btn btn-link btn-sm p-0 text-decoration-none text-primary btn-view-log-detail"
|
|
data-log-id="{{ log.id }}"
|
|
data-log-action="{{ log.action }}"
|
|
data-log-module="{{ log.module }}"
|
|
data-log-user="{% if log.user_email %}{{ log.user_email }}{% else %}Sistema{% endif %}"
|
|
data-log-time="{{ log.created_at }}"
|
|
data-log-ip="{{ log.ip_address or '-' }}"
|
|
data-log-details="{{ log.details | escape }}">
|
|
<i class="bi bi-eye"></i>
|
|
</button>
|
|
{% endif %}
|
|
</div>
|
|
</td>
|
|
<td class="pe-3 text-end font-monospace small text-muted">
|
|
{{ log.ip_address or '-' }}
|
|
</td>
|
|
</tr>
|
|
{% endfor %}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<!-- Paginación Dinámica -->
|
|
{% if pagination.pages > 1 %}
|
|
<div class="p-3 border-top d-flex justify-content-between align-items-center flex-wrap gap-2">
|
|
<small class="text-muted">
|
|
Mostrando página {{ pagination.page }} de {{ pagination.pages }} (Total: <strong>{{ pagination.total }}</strong> eventos registrados)
|
|
</small>
|
|
<nav aria-label="Navegación de auditoría">
|
|
<ul class="pagination pagination-sm mb-0">
|
|
<li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
|
|
<a class="page-link" href="/admin/audit_logs?page={{ pagination.page - 1 }}{% if search %}&search={{ search }}{% endif %}{% if current_module %}&module={{ current_module }}{% endif %}{% if current_action %}&action={{ current_action }}{% endif %}">
|
|
« Anterior
|
|
</a>
|
|
</li>
|
|
{% for p in pagination.pages_list %}
|
|
<li class="page-item {% if p == pagination.page %}active{% endif %}">
|
|
<a class="page-link" href="/admin/audit_logs?page={{ p }}{% if search %}&search={{ search }}{% endif %}{% if current_module %}&module={{ current_module }}{% endif %}{% if current_action %}&action={{ current_action }}{% endif %}">
|
|
{{ p }}
|
|
</a>
|
|
</li>
|
|
{% endfor %}
|
|
<li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
|
|
<a class="page-link" href="/admin/audit_logs?page={{ pagination.page + 1 }}{% if search %}&search={{ search }}{% endif %}{% if current_module %}&module={{ current_module }}{% endif %}{% if current_action %}&action={{ current_action }}{% endif %}">
|
|
Siguiente »
|
|
</a>
|
|
</li>
|
|
</ul>
|
|
</nav>
|
|
</div>
|
|
{% endif %}
|
|
|
|
{% else %}
|
|
<div class="text-center py-5">
|
|
<i class="bi bi-shield-slash text-muted fs-1 d-block mb-2"></i>
|
|
<h6 class="fw-bold">No se encontraron registros de auditoría</h6>
|
|
<p class="text-muted small">Los eventos de inicio de sesión y modificaciones de espacios áulicos se registrarán automáticamente a medida que ocurran.</p>
|
|
{% if search or current_module or current_action %}
|
|
<a href="/admin/audit_logs" class="btn btn-outline-primary btn-sm mt-2">
|
|
<i class="bi bi-x-circle me-1"></i>Limpiar filtros de búsqueda
|
|
</a>
|
|
{% endif %}
|
|
</div>
|
|
{% endif %}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal de Detalle Extendido -->
|
|
<div class="modal fade" id="logDetailModal" tabindex="-1" aria-labelledby="logDetailModalLabel" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered modal-lg">
|
|
<div class="modal-content">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title" id="logDetailModalLabel">
|
|
<i class="bi bi-info-circle text-primary me-2"></i>Detalle del Registro de Auditoría
|
|
</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Cerrar"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<div class="row g-2 mb-3 small">
|
|
<div class="col-sm-6">
|
|
<span class="text-muted">Fecha y Hora:</span>
|
|
<div class="fw-semibold font-monospace" id="modalLogTime">-</div>
|
|
</div>
|
|
<div class="col-sm-6">
|
|
<span class="text-muted">Usuario Actuante:</span>
|
|
<div class="fw-semibold" id="modalLogUser">-</div>
|
|
</div>
|
|
<div class="col-sm-4">
|
|
<span class="text-muted">Acción:</span>
|
|
<div class="fw-semibold" id="modalLogAction">-</div>
|
|
</div>
|
|
<div class="col-sm-4">
|
|
<span class="text-muted">Módulo:</span>
|
|
<div class="fw-semibold" id="modalLogModule">-</div>
|
|
</div>
|
|
<div class="col-sm-4">
|
|
<span class="text-muted">IP de Conexión:</span>
|
|
<div class="fw-semibold font-monospace" id="modalLogIp">-</div>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<label class="form-label small fw-semibold text-muted">Contenido del Evento / Payload:</label>
|
|
<pre class="bg-light p-3 rounded border font-monospace small mb-0" id="modalLogDetails" style="max-height: 300px; overflow-y: auto; white-space: pre-wrap; word-break: break-word;"></pre>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="button" class="btn btn-outline-secondary btn-sm" onclick="copyModalDetails()">
|
|
<i class="bi bi-clipboard me-1"></i>Copiar Detalle
|
|
</button>
|
|
<button type="button" class="btn btn-secondary btn-sm" data-bs-dismiss="modal">Cerrar</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function showLogDetailModal(id, action, module, user, time, ip, details) {
|
|
document.getElementById('modalLogTime').textContent = time || '-';
|
|
document.getElementById('modalLogUser').textContent = user || 'Sistema';
|
|
document.getElementById('modalLogAction').textContent = action || '-';
|
|
document.getElementById('modalLogModule').textContent = module || '-';
|
|
document.getElementById('modalLogIp').textContent = ip || '-';
|
|
|
|
let formatted = details || '';
|
|
try {
|
|
const parsed = JSON.parse(details);
|
|
formatted = JSON.stringify(parsed, null, 2);
|
|
} catch(e) {
|
|
// es texto plano
|
|
}
|
|
document.getElementById('modalLogDetails').textContent = formatted;
|
|
|
|
const modal = new bootstrap.Modal(document.getElementById('logDetailModal'));
|
|
modal.show();
|
|
}
|
|
|
|
function copyModalDetails() {
|
|
const text = document.getElementById('modalLogDetails').textContent;
|
|
navigator.clipboard.writeText(text).then(() => {
|
|
alert('Detalle copiado al portapapeles.');
|
|
});
|
|
}
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
document.querySelectorAll('.btn-view-log-detail').forEach(function(btn) {
|
|
btn.addEventListener('click', function() {
|
|
showLogDetailModal(
|
|
this.dataset.logId,
|
|
this.dataset.logAction,
|
|
this.dataset.logModule,
|
|
this.dataset.logUser,
|
|
this.dataset.logTime,
|
|
this.dataset.logIp,
|
|
this.dataset.logDetails
|
|
);
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
{% endblock %}
|