alpinux-portail/web/admin/services.php
Alpinux 4921a0691c Admin : gestion groupes, services par groupes, badge admin
- 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>
2026-05-04 00:59:25 +02:00

119 lines
4.3 KiB
PHP

<?php
require_once __DIR__ . '/../inc/config.php';
require_once __DIR__ . '/../inc/auth.php';
require_once __DIR__ . '/../inc/services.php';
require_once __DIR__ . '/../inc/keycloak.php';
session_start_safe();
require_admin();
$kc_groups = kc_list_groups();
$group_names = array_column($kc_groups, 'name');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$updated = [];
foreach ($_POST as $key => $val) {
if (!str_starts_with($key, 'name_')) continue;
$idx = substr($key, 5);
$updated[] = [
'name' => trim($val),
'url' => trim($_POST["url_$idx"] ?? ''),
'description' => trim($_POST["description_$idx"] ?? ''),
'groups' => (array)($_POST["groups_$idx"] ?? []),
'visible' => isset($_POST["visible_$idx"]),
];
}
$new_name = trim($_POST['new_name'] ?? '');
if ($new_name) {
$updated[] = [
'name' => $new_name,
'url' => trim($_POST['new_url'] ?? ''),
'description' => trim($_POST['new_description'] ?? ''),
'groups' => (array)($_POST['new_groups'] ?? []),
'visible' => isset($_POST['new_visible']),
];
}
services_save($updated);
set_flash('success', 'Services sauvegardés.');
header('Location: /admin/services.php');
exit;
}
$services = services_list();
$title = 'Paramétrage des services';
require __DIR__ . '/../views/layout.php';
?>
<div class="admin-page">
<div class="page-header">
<h1>Paramétrage des services</h1>
<div class="admin-nav">
<a href="/admin/members.php">Membres</a>
<a href="/admin/groups.php">Groupes</a>
<a href="/admin/services.php" class="active">Services</a>
</div>
</div>
<div class="card">
<form method="post">
<table class="services-table">
<thead>
<tr>
<th>Nom</th>
<th>URL</th>
<th>Description</th>
<th>Groupes requis</th>
<th>Visible</th>
</tr>
</thead>
<tbody>
<?php foreach ($services as $i => $s): ?>
<tr>
<td><input type="text" name="name_<?= $i ?>" value="<?= htmlspecialchars($s['name']) ?>" required></td>
<td><input type="url" name="url_<?= $i ?>" value="<?= htmlspecialchars($s['url']) ?>"></td>
<td><input type="text" name="description_<?= $i ?>" value="<?= htmlspecialchars($s['description']) ?>"></td>
<td>
<div class="group-checks">
<?php foreach ($group_names as $gn): ?>
<label class="group-check-label">
<input type="checkbox" name="groups_<?= $i ?>[]" value="<?= htmlspecialchars($gn) ?>"
<?= in_array($gn, $s['groups'], true) ? 'checked' : '' ?>>
<?= htmlspecialchars($gn) ?>
</label>
<?php endforeach; ?>
<?php if (!$group_names): ?>
<span class="text-muted small">Aucun groupe</span>
<?php endif; ?>
</div>
</td>
<td class="center">
<input type="checkbox" name="visible_<?= $i ?>" <?= $s['visible'] ? 'checked' : '' ?>>
</td>
</tr>
<?php endforeach; ?>
<tr class="new-row">
<td><input type="text" name="new_name" placeholder="Nouveau service"></td>
<td><input type="url" name="new_url" placeholder="https://..."></td>
<td><input type="text" name="new_description" placeholder="Description"></td>
<td>
<div class="group-checks">
<?php foreach ($group_names as $gn): ?>
<label class="group-check-label">
<input type="checkbox" name="new_groups[]" value="<?= htmlspecialchars($gn) ?>">
<?= htmlspecialchars($gn) ?>
</label>
<?php endforeach; ?>
</div>
</td>
<td class="center"><input type="checkbox" name="new_visible" checked></td>
</tr>
</tbody>
</table>
<div style="margin-top:1rem">
<button type="submit" class="btn-primary">Enregistrer</button>
</div>
</form>
</div>
</div>
<?php require __DIR__ . '/../views/layout_end.php'; ?>