- admin/groups.php : liste/création/suppression des groupes Keycloak avec comptage des membres et services associés par groupe - admin/services.php : remplace requires_adherent par sélection multi-groupes - inc/services.php : modèle groups[], migration auto depuis requires_adherent, helper service_accessible() pour l'accès contextuel - inc/keycloak.php : kc_list_groups, kc_create_group, kc_delete_group, kc_group_members - profile.php : badge Admin visible dans la tuile Mon compte - index.php : utilise service_accessible() avec les groupes de l'utilisateur Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
127 lines
4 KiB
PHP
127 lines
4 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();
|
|
|
|
$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'])) ?> » ?')">
|
|
<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">
|
|
<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'; ?>
|