csrf_jeton(), csrf_champ() et csrf_verifier() dans inc/auth.php : un jeton par session, comparé par hash_equals, réponse 400 s'il manque ou ne correspond pas. La vérification est appelée en tête de chaque page qui traite un POST — console d'administration (membres, validation, révocation, groupes, services) et pages des membres (profil, mot de passe, inscription, renouvellement d'adhésion) —, et chaque formulaire porte le champ caché. Ajoute aussi e(), l'échappement de sortie à utiliser dans tout nouveau code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01V2csyno8m71DJFKYM1vgzV
130 lines
4.1 KiB
PHP
130 lines
4.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/../inc/config.php';
|
|
require_once __DIR__ . '/../inc/auth.php';
|
|
require_once __DIR__ . '/../inc/keycloak.php';
|
|
require_once __DIR__ . '/../inc/services.php';
|
|
|
|
session_start_safe();
|
|
require_admin();
|
|
csrf_verifier();
|
|
|
|
$errors = [];
|
|
$success = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$action = $_POST['action'] ?? '';
|
|
|
|
if ($action === 'create') {
|
|
$name = trim($_POST['name'] ?? '');
|
|
if (!$name) {
|
|
$errors[] = 'Le nom du groupe est obligatoire.';
|
|
} else {
|
|
try {
|
|
kc_create_group($name);
|
|
set_flash('success', "Groupe « $name » créé.");
|
|
header('Location: /admin/groups.php'); exit;
|
|
} catch (Exception $e) { $errors[] = $e->getMessage(); }
|
|
}
|
|
}
|
|
|
|
if ($action === 'delete') {
|
|
$group_id = $_POST['group_id'] ?? '';
|
|
if ($group_id) {
|
|
try {
|
|
kc_delete_group($group_id);
|
|
set_flash('success', 'Groupe supprimé.');
|
|
header('Location: /admin/groups.php'); exit;
|
|
} catch (Exception $e) { $errors[] = $e->getMessage(); }
|
|
}
|
|
}
|
|
}
|
|
|
|
$groups = kc_list_groups();
|
|
$services = services_list();
|
|
|
|
// Indexe les services par groupe requis
|
|
$services_by_group = [];
|
|
foreach ($services as $s) {
|
|
foreach ($s['groups'] ?? [] as $g) {
|
|
$services_by_group[$g][] = $s['name'];
|
|
}
|
|
}
|
|
|
|
$title = 'Gestion des groupes';
|
|
require __DIR__ . '/../views/layout.php';
|
|
?>
|
|
|
|
<div class="admin-page">
|
|
<div class="page-header">
|
|
<h1>Gestion des groupes</h1>
|
|
<div class="admin-nav">
|
|
<a href="/admin/members.php">Membres</a>
|
|
<a href="/admin/groups.php" class="active">Groupes</a>
|
|
<a href="/admin/services.php">Services</a>
|
|
</div>
|
|
</div>
|
|
|
|
<?php if ($errors): ?>
|
|
<div class="alert alert-error"><ul><?php foreach ($errors as $e): ?><li><?= htmlspecialchars($e) ?></li><?php endforeach; ?></ul></div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Liste des groupes -->
|
|
<div class="card">
|
|
<h2>Groupes Keycloak (<?= count($groups) ?>)</h2>
|
|
<?php if (!$groups): ?>
|
|
<p class="text-muted">Aucun groupe trouvé.</p>
|
|
<?php else: ?>
|
|
<table>
|
|
<thead>
|
|
<tr><th>Nom</th><th>Membres</th><th>Services associés</th><th>Actions</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($groups as $g):
|
|
$members = kc_group_members($g['id']);
|
|
$linked = $services_by_group[$g['name']] ?? [];
|
|
?>
|
|
<tr>
|
|
<td><strong><?= htmlspecialchars($g['name']) ?></strong></td>
|
|
<td><?= count($members) ?></td>
|
|
<td>
|
|
<?php if ($linked): ?>
|
|
<?php foreach ($linked as $svc): ?>
|
|
<span class="badge"><?= htmlspecialchars($svc) ?></span>
|
|
<?php endforeach; ?>
|
|
<?php else: ?>
|
|
<span class="text-muted small">—</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td>
|
|
<form method="post" style="display:inline"
|
|
onsubmit="return confirm('Supprimer le groupe « <?= htmlspecialchars(addslashes($g['name'])) ?> » ?')">
|
|
<?= csrf_champ() ?>
|
|
<input type="hidden" name="action" value="delete">
|
|
<input type="hidden" name="group_id" value="<?= htmlspecialchars($g['id']) ?>">
|
|
<button type="submit" class="btn-danger btn-sm">Supprimer</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<!-- Créer un groupe -->
|
|
<div class="card">
|
|
<h2>Créer un groupe</h2>
|
|
<form method="post" style="display:flex; gap:.8rem; align-items:flex-end">
|
|
<?= csrf_champ() ?>
|
|
<input type="hidden" name="action" value="create">
|
|
<div class="form-group" style="margin:0; flex:1">
|
|
<label>Nom du groupe</label>
|
|
<input type="text" name="name" required placeholder="ex: bureau, moderateurs…"
|
|
value="<?= htmlspecialchars($_POST['name'] ?? '') ?>">
|
|
</div>
|
|
<button type="submit" class="btn-primary">Créer</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require __DIR__ . '/../views/layout_end.php'; ?>
|