feat: rôles admin/user, page Utilisateurs, lien Vision dans navbar, filtre courriers par compte

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Jules
2026-05-06 00:01:39 +02:00
parent d11e6aec5e
commit 3f73421fd3
8 changed files with 305 additions and 28 deletions

View File

@@ -15,13 +15,23 @@ function auth_check(): void {
}
}
function admin_check(): void {
auth_check();
if ($_SESSION['admin_role'] !== 'admin') {
header('Location: /');
exit;
}
}
function auth_login(string $username, string $password): bool {
$st = db()->prepare('SELECT id, password FROM admin_users WHERE username = ? LIMIT 1');
$st = db()->prepare('SELECT id, role, account_id, password FROM admin_users WHERE username = ? LIMIT 1');
$st->execute([$username]);
$row = $st->fetch();
if ($row && password_verify($password, $row['password'])) {
$_SESSION['admin_id'] = $row['id'];
$_SESSION['admin_user'] = $username;
$_SESSION['admin_id'] = $row['id'];
$_SESSION['admin_user'] = $username;
$_SESSION['admin_role'] = $row['role'];
$_SESSION['admin_account_id'] = $row['account_id'];
return true;
}
return false;
@@ -34,6 +44,9 @@ function auth_logout(): void {
exit;
}
function current_user(): string {
return $_SESSION['admin_user'] ?? '';
function current_user(): string { return $_SESSION['admin_user'] ?? ''; }
function is_admin(): bool { return ($_SESSION['admin_role'] ?? '') === 'admin'; }
function current_account_id(): ?int {
$id = $_SESSION['admin_account_id'] ?? null;
return $id ? (int)$id : null;
}

View File

@@ -1,5 +1,7 @@
<?php
define('VISION_URL', 'http://192.168.111.212');
function html_head(string $title): void {
$domain = cfg('domain', 'copymail');
echo <<<HTML
@@ -18,13 +20,23 @@ HTML;
}
function html_navbar(string $active = ''): void {
$user = current_user();
$user = current_user();
$isAdmin = is_admin();
$pages = [
'mail' => ['/', 'bi-envelope-fill', 'Courriers'],
'accounts' => ['/admin/accounts.php', 'bi-people-fill', 'Comptes'],
'domain' => ['/admin/domain.php', 'bi-gear-fill', 'Configuration'],
'status' => ['/admin/index.php', 'bi-activity', 'Tableau de bord'],
'mail' => ['/', 'bi-envelope-fill', 'Courriers'],
];
if ($isAdmin) {
$pages['accounts'] = ['/admin/accounts.php', 'bi-people-fill', 'Comptes'];
$pages['domain'] = ['/admin/domain.php', 'bi-gear-fill', 'Configuration'];
$pages['status'] = ['/admin/index.php', 'bi-activity', 'Tableau de bord'];
$pages['users'] = ['/admin/users.php', 'bi-person-lock', 'Utilisateurs'];
}
$roleBadge = $isAdmin
? '<span class="badge bg-warning text-dark me-2">admin</span>'
: '<span class="badge bg-secondary me-2">user</span>';
echo '<nav class="navbar navbar-expand-lg navbar-dark bg-primary mb-4">';
echo '<div class="container-fluid">';
echo '<a class="navbar-brand fw-bold" href="/"><i class="bi bi-envelope-arrow-down-fill me-2"></i>CopyMail</a>';
@@ -35,9 +47,12 @@ function html_navbar(string $active = ''): void {
echo "<li class=\"nav-item\"><a class=\"{$cls}\" href=\"{$href}\"><i class=\"bi {$icon} me-1\"></i>{$label}</a></li>";
}
echo '</ul>';
echo "<span class=\"navbar-text me-3\"><i class=\"bi bi-person-circle me-1\"></i>{$user}</span>";
echo '<div class="d-flex align-items-center gap-2">';
// Lien Vision
echo '<a class="btn btn-outline-light btn-sm" href="' . VISION_URL . '" target="_blank" title="Ouvrir Vision"><i class="bi bi-grid-fill me-1"></i>Vision</a>';
echo "{$roleBadge}<span class=\"navbar-text me-2\"><i class=\"bi bi-person-circle me-1\"></i>{$user}</span>";
echo '<a class="btn btn-outline-light btn-sm" href="/logout.php"><i class="bi bi-box-arrow-right me-1"></i>Déconnexion</a>';
echo '</div></div></nav>';
echo '</div></div></div></nav>';
}
function html_foot(): void {