LuminaWebsite/admin/users.php
Maaxxs 592269d300 Permissions per person: grant single permissions to users in addition to their groups
In the user form (admin/user_form.php) a new section "Einzelne Berechtigungen" lists all
permissions, with the group-assign rights (roles.assign.<ID>) shown by group name as in the role
form. It opens by itself when the user already has some. A shield marks what the user already
gets through one of the ticked groups; the tooltip says who granted a permission and when.

- New table user_permissions (user, permission, granted_by, created_at; schema 52, also in
  sql/schema.sql). user_has_permission() now resolves through user_permission_keys() (groups UNION
  direct permissions, falls back to groups only while the table is missing), and
  discord_ids_with_permission() and the raids.manage recipients in bot_api.php include direct
  permissions too.
- New core permission users.permissions: only with it can someone grant or revoke direct
  permissions, and only ones they have themselves (current_user_permission_ids()); others are
  greyed out and ignored on the server even if submitted. Your own direct permissions can only be
  changed by someone else (except developers). Without the right the section is a read-only list.
  On update, groups with roles.manage get users.permissions.
- Saving applies only the changes, so the remaining entries keep who granted them and when.
  Grants and revokes go into the audit log ("Einzelrechte erteilt/entzogen/jetzt").
- User list: "+N Einzelrechte" badge next to the groups (title lists them), searchable; the live
  fingerprint of the page includes user_permissions.
- Data export gets a section "einzelrechte", account deletion removes them, and the privacy
  policy mentions both. Patch notes, README and the roles page intro updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 02:20:58 +02:00

242 lines
15 KiB
PHP

<?php
require_once __DIR__ . '/../includes/bootstrap.php';
require_permission('users.view');
$canCreate = user_has_permission('users.create');
$canEdit = user_has_permission('users.edit');
$canDelete = user_has_permission('users.delete');
$canManageAdmins = user_has_permission('users.admin');
$canExport = user_has_permission('users.export');
$currentUser = current_user();
$users = db()->query(
"SELECT u.id, u.username, u.display_name, u.status, u.last_login, u.created_at, u.must_change_password,
u.password_login, u.is_admin, u.discord_id, u.discord_username, u.deletion_scheduled_at, u.deleted_at,
MIN(r.roster_order) AS best_rank,
GROUP_CONCAT(COALESCE(NULLIF(r.display_name, ''), r.name) ORDER BY COALESCE(r.roster_order, 999), r.name SEPARATOR ', ') AS role_names,
(SELECT GROUP_CONCAT(p.`key` ORDER BY p.`key` SEPARATOR ', ') FROM user_permissions up
JOIN permissions p ON p.id = up.permission_id WHERE up.user_id = u.id) AS direct_permissions
FROM users u
LEFT JOIN user_roles ur ON ur.user_id = u.id
LEFT JOIN roles r ON r.id = ur.role_id
GROUP BY u.id"
)->fetchAll();
// Reihenfolge: wer auf die Freischaltung wartet (die Liste, die man abarbeitet), dann
// Administratoren, Entwickler und danach nach der hoechsten Gruppe - gemessen an der
// Reihenfolge der Mitgliederliste (kleinere Zahl = hoeher, Gruppen ohne Zahl zuletzt).
usort($users, static function (array $a, array $b): int {
$key = static fn (array $u): array => [
$u['status'] === 'pending' ? 0 : 1,
!empty($u['is_admin']) ? 0 : 1,
is_developer($u) ? 0 : 1,
$u['best_rank'] !== null ? (int) $u['best_rank'] : PHP_INT_MAX,
mb_strtolower($u['display_name'] !== '' ? $u['display_name'] : $u['username']),
];
return $key($a) <=> $key($b);
});
$statusLabels = [
'active' => ['Aktiv', 'badge-active'],
'disabled' => ['Deaktiviert', 'badge-disabled'],
'pending' => ['Wartet auf Freischaltung', 'badge-pending'],
];
$pageTitle = 'Benutzerverwaltung';
// Die Tabelle hat viele Spalten - volle Breite statt der schmalen Inhaltsspalte.
$containerWide = true;
include __DIR__ . '/../includes/header.php';
?>
<div class="panel">
<h1>Benutzerverwaltung</h1>
<p class="text-muted">
Wer sich zum ersten Mal über Discord anmeldet, bekommt automatisch ein Konto, das erst
freigeschaltet werden muss. Berechtigungsgruppen mit verknüpfter Discord-Rolle werden
automatisch vergeben und entzogen.
</p>
<?php // Beide Knoepfe gleich breit untereinander, die Erklaerung zum Abgleich daneben ?>
<div class="user-admin-actions">
<?php if ($canCreate): ?>
<a class="button" href="/admin/user_form.php">Neuen Benutzer anlegen</a>
<?php endif; ?>
<?php if ($canEdit): ?>
<form class="discord-sync-form" method="post" action="/admin/discord_sync_action.php">
<?= csrf_field() ?>
<button type="submit" class="button discord-connect-button"><?= discord_logo(16) ?> Jetzt mit Discord abgleichen</button>
<span class="text-muted">Gleicht Server-Mitgliedschaft, Rollen und Klassen aller Konten sofort ab. Automatisch passiert das bei jeder Anmeldung und danach alle 5 Minuten, solange jemand eingeloggt ist.</span>
</form>
<?php endif; ?>
</div>
<?php if ($canEdit): ?>
<form class="settings-toggle" method="post" action="/admin/settings_action.php">
<?= csrf_field() ?>
<input type="hidden" name="setting" value="discord_unlink_allowed">
<label>
<input type="checkbox" name="value" value="1" <?= discord_unlink_allowed() ? 'checked' : '' ?> onchange="this.form.submit()">
Mitglieder dürfen ihre Discord-Verknüpfung im Profil lösen
</label>
<noscript><button type="submit" class="button-secondary button-small">Speichern</button></noscript>
</form>
<?php endif; ?>
<?php
$statusCounts = array_count_values(array_column($users, 'status'));
$adminCount = count(array_filter($users, static fn ($u) => !empty($u['is_admin']) || is_developer($u)));
?>
<div class="list-toolbar" id="users-toolbar">
<label class="search-field">
<?= icon('search') ?>
<input type="search" id="users-search" placeholder="Benutzername, Anzeigename, Discord oder Gruppe suchen …" aria-label="Benutzer durchsuchen">
</label>
<div class="list-toolbar-row">
<div class="members-chips" data-filter="status" role="group" aria-label="Nach Status filtern">
<button type="button" class="members-chip active" data-value="">Alle <span class="chip-count"><?= count($users) ?></span></button>
<?php if (!empty($statusCounts['pending'])): ?>
<button type="button" class="members-chip" data-value="pending">Wartet auf Freischaltung <span class="chip-count"><?= $statusCounts['pending'] ?></span></button>
<?php endif; ?>
<button type="button" class="members-chip" data-value="active">Aktiv <span class="chip-count"><?= $statusCounts['active'] ?? 0 ?></span></button>
<?php if (!empty($statusCounts['disabled'])): ?>
<button type="button" class="members-chip" data-value="disabled">Deaktiviert <span class="chip-count"><?= $statusCounts['disabled'] ?></span></button>
<?php endif; ?>
<?php if ($adminCount > 0): ?>
<button type="button" class="members-chip" data-value="admin">Administratoren <span class="chip-count"><?= $adminCount ?></span></button>
<?php endif; ?>
</div>
<div class="list-toolbar-meta">
<span class="members-filter-label"><?= icon('sort') ?> Sortieren</span>
<div class="members-segment" data-filter="sort" role="group" aria-label="Sortieren">
<button type="button" data-value="rank" class="active">Rang</button>
<button type="button" data-value="name">Name</button>
<button type="button" data-value="login">Letzter Login</button>
<button type="button" data-value="created">Neueste</button>
</div>
<span class="list-search-count text-muted" id="users-count"></span>
</div>
</div>
</div>
<div class="table-scroll">
<table class="table-actions" id="users-table">
<thead>
<tr>
<th>Benutzername</th>
<th>Anzeigename</th>
<th>Discord</th>
<th>Berechtigungsgruppen</th>
<th>Status</th>
<th>Letzter Login</th>
<?php if ($canEdit || $canDelete): ?><th>Aktionen</th><?php endif; ?>
</tr>
</thead>
<tbody>
<?php $rowIndex = 0; ?>
<?php foreach ($users as $u): ?>
<?php
[$statusLabel, $statusClass] = $statusLabels[$u['status']] ?? [$u['status'], ''];
// Administratoren bearbeiten/loeschen nur mit users.admin.
$protectedAdmin = !empty($u['is_admin']) && !$canManageAdmins;
// Entwickler-Konten bearbeiten/loeschen nur andere Entwickler.
$isDevRow = is_developer($u);
if ($isDevRow && !is_developer($currentUser)) {
$protectedAdmin = true;
}
?>
<tr<?= $u['status'] === 'pending' ? ' class="row-pending"' : '' ?>
data-rank="<?= (int) $rowIndex++ ?>"
data-name="<?= e(mb_strtolower($u['display_name'] !== '' ? $u['display_name'] : $u['username'])) ?>"
data-search="<?= e(mb_strtolower($u['username'] . ' ' . $u['display_name'] . ' ' . ($u['discord_username'] ?? '') . ' ' . ($u['role_names'] ?? '') . ' ' . ($u['direct_permissions'] ?? ''))) ?>"
data-status="<?= e($u['status']) ?>"
data-admin="<?= !empty($u['is_admin']) || $isDevRow ? '1' : '0' ?>"
data-login="<?= e((string) ($u['last_login'] ?? '')) ?>"
data-created="<?= e((string) $u['created_at']) ?>">
<td><?= e($u['username']) ?></td>
<td><?= e($u['display_name']) ?></td>
<td>
<?php if (!empty($u['discord_id'])): ?>
<span class="discord-inline"><?= discord_logo(14) ?> <?= e((string) $u['discord_username']) ?></span>
<?php else: ?>
<span class="text-muted">-</span>
<?php endif; ?>
</td>
<td>
<?php $directCount = $u['direct_permissions'] ? count(explode(', ', $u['direct_permissions'])) : 0; ?>
<?= $u['role_names'] || !$directCount ? e($u['role_names'] ?: '-') : '' ?>
<?php if ($directCount): ?>
<span class="badge badge-direct-permissions" title="Einzeln vergeben: <?= e($u['direct_permissions']) ?>">+<?= $directCount ?> <?= $directCount === 1 ? 'Einzelrecht' : 'Einzelrechte' ?></span>
<?php endif; ?>
</td>
<td>
<div class="badge-group">
<?php if (!empty($u['deleted_at'])): ?>
<span class="badge badge-disabled" title="Am <?= e(date('d.m.Y', (int) strtotime((string) $u['deleted_at']))) ?> gelöscht - ohne Namen und Discord-ID">Gelöscht (unkenntlich)</span>
<?php else: ?>
<span class="badge <?= e($statusClass) ?>"><?= e($statusLabel) ?></span>
<?php endif; ?>
<?php if (!empty($u['deletion_scheduled_at']) && empty($u['deleted_at'])): ?>
<span class="badge badge-disabled" title="Vom Mitglied beantragt - Anmelden bricht die Löschung ab">Löschung am <?= e(date('d.m.', (int) strtotime((string) $u['deletion_scheduled_at']))) ?></span>
<?php endif; ?>
<?php if ($isDevRow): ?>
<span class="badge badge-developer" title="Entwickler der Website - hat immer alle Berechtigungen"><?= icon('code') ?> Entwickler</span>
<?php endif; ?>
<?php if (!empty($u['is_admin'])): ?>
<span class="badge badge-admin" title="Hat alle Berechtigungen"><?= icon('crown') ?> Administrator</span>
<?php endif; ?>
<?php if (empty($u['password_login'])): ?>
<span class="badge" title="Anmeldung nur über Discord möglich">Nur Discord</span>
<?php elseif ($u['must_change_password']): ?>
<span class="badge" title="Benutzer muss beim nächsten Login das Passwort ändern">Passwortänderung ausstehend</span>
<?php endif; ?>
</div>
</td>
<td><?= e($u['last_login'] ?? 'nie') ?></td>
<?php if ($canEdit || $canDelete): ?>
<td>
<div class="actions">
<?php if ($canEdit && $u['status'] === 'pending'): ?>
<form class="inline-form" method="post" action="/admin/user_approve.php">
<?= csrf_field() ?>
<input type="hidden" name="id" value="<?= (int) $u['id'] ?>">
<button type="submit" class="button-success button-small">Freischalten</button>
</form>
<?php endif; ?>
<?php if ($canEdit && !$protectedAdmin): ?>
<a class="icon-button" href="/admin/user_form.php?id=<?= (int) $u['id'] ?>" title="Bearbeiten" aria-label="Bearbeiten"><?= icon('edit') ?></a>
<?php endif; ?>
<?php if ($canExport && !$protectedAdmin): ?>
<a class="icon-button" href="/admin/user_export.php?id=<?= (int) $u['id'] ?>" title="Daten &amp; Löschung (Auskunft, Download-Link, Konto löschen)" aria-label="Daten und Löschung"><?= icon('archive') ?></a>
<?php endif; ?>
<?php if ($canEdit && !$protectedAdmin && $u['status'] === 'active' && (int) $u['id'] !== (int) $currentUser['id']): ?>
<?php foreach (['logout' => ['log-out', 'Abmelden erzwingen (alle Sitzungen beenden)', 'Alle Sitzungen dieses Kontos beenden?'], 'lock' => ['lock', 'Sperren und überall abmelden', 'Dieses Konto sperren und überall abmelden?']] as $secAction => [$secIcon, $secTitle, $secQuestion]): ?>
<form class="inline-form" method="post" action="/admin/user_security.php"
data-confirm="<?= e($secQuestion) ?>" data-confirm-title="<?= e($secTitle) ?>" data-confirm-input="reason"
data-confirm-placeholder="Grund (optional, erscheint im Audit-Log)"<?= $secAction === 'lock' ? ' data-confirm-danger data-confirm-ok="Sperren"' : ' data-confirm-ok="Abmelden"' ?>>
<?= csrf_field() ?>
<input type="hidden" name="id" value="<?= (int) $u['id'] ?>">
<input type="hidden" name="action" value="<?= $secAction ?>">
<input type="hidden" name="reason" value="">
<button type="submit" class="icon-button<?= $secAction === 'lock' ? ' icon-button-danger' : '' ?>" title="<?= e($secTitle) ?>" aria-label="<?= e($secTitle) ?>"><?= icon($secIcon) ?></button>
</form>
<?php endforeach; ?>
<?php endif; ?>
<?php if ($canDelete && !$protectedAdmin && (int) $u['id'] !== (int) $currentUser['id']): ?>
<form class="inline-form" method="post" action="/admin/user_delete.php" data-confirm="Diesen Benutzer wirklich löschen?" data-confirm-title="Benutzer löschen" data-confirm-danger data-confirm-ok="Löschen">
<?= csrf_field() ?>
<input type="hidden" name="id" value="<?= (int) $u['id'] ?>">
<button type="submit" class="icon-button icon-button-danger" title="Löschen" aria-label="Löschen"><?= icon('trash') ?></button>
</form>
<?php endif; ?>
</div>
</td>
<?php endif; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<p class="text-muted" id="users-empty" hidden>Kein Benutzer passt zu Suche und Filter.</p>
</div>
<script src="<?= e(asset_url('/assets/js/admin-users.js')) ?>"></script>
<?php include __DIR__ . '/../includes/footer.php'; ?>