auditoria.php
2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
require __DIR__ . '/../src/bootstrap.php';
Auth::requireAdmin();
$perPage = 50;
$page = max(1, (int) ($_GET['p'] ?? 1));
$offset = ($page - 1) * $perPage;
$total = (int) Database::pdo()->query('SELECT COUNT(*) FROM audit_log')->fetchColumn();
$stmt = Database::pdo()->prepare(
'SELECT * FROM audit_log ORDER BY criado_em DESC, id DESC LIMIT :limit OFFSET :offset'
);
$stmt->bindValue('limit', $perPage, PDO::PARAM_INT);
$stmt->bindValue('offset', $offset, PDO::PARAM_INT);
$stmt->execute();
$logs = $stmt->fetchAll();
$totalPages = max(1, (int) ceil($total / $perPage));
$acaoLabels = [
'login' => 'Login',
'login_negado' => 'Login negado (sem permissão)',
'login_falhou' => 'Login falhou (usuário/senha)',
'logout' => 'Logoff',
'habilitou_aluno' => 'Habilitou aluno',
'desabilitou_aluno' => 'Desabilitou aluno',
'alterou_senha_aluno' => 'Alterou senha de aluno',
'adicionou_admin' => 'Adicionou administrador',
'desativou_admin' => 'Desativou administrador',
'reativou_admin' => 'Reativou administrador',
'alterou_papel_admin' => 'Alterou papel de administrador',
'atualizou_settings' => 'Atualizou configurações',
];
View::header('Auditoria');
?>
<div class="card">
<h1>Auditoria</h1>
<p class="hint">
Histórico de logins (inclusive tentativas com senha incorreta), alterações em
alunos, administradores e configurações. Mostrando <?= count($logs) ?> de <?= $total ?> registros.
</p>
<table>
<tr>
<th>Data/hora</th>
<th>Usuário</th>
<th>Ação</th>
<th>Alvo</th>
<th>Detalhe</th>
<th>IP</th>
</tr>
<?php foreach ($logs as $log): ?>
<tr>
<td data-label="Data/hora"><?= View::escape($log['criado_em']) ?></td>
<td data-label="Usuário"><?= View::escape($log['usuario']) ?></td>
<td data-label="Ação"><?= View::escape($acaoLabels[$log['acao']] ?? $log['acao']) ?></td>
<td data-label="Alvo"><?= View::escape($log['alvo'] ?? '') ?></td>
<td data-label="Detalhe"><?= View::escape($log['detalhe'] ?? '') ?></td>
<td data-label="IP"><?= View::escape($log['ip'] ?? '') ?></td>
</tr>
<?php endforeach; ?>
<?php if (!$logs): ?>
<tr><td colspan="6">Nenhum registro ainda.</td></tr>
<?php endif; ?>
</table>
<?php if ($totalPages > 1): ?>
<p style="margin-top:12px">
<?php for ($p = 1; $p <= $totalPages; $p++): ?>
<?php if ($p === $page): ?>
<strong><?= $p ?></strong>
<?php else: ?>
<a href="auditoria?p=<?= $p ?>"><?= $p ?></a>
<?php endif; ?>
<?php if ($p < $totalPages): ?> | <?php endif; ?>
<?php endfor; ?>
</p>
<?php endif; ?>
</div>
<?php
View::footer();