LuminaWebsite/admin/users.php
Maaxxs 9032118b6a Classes come from Discord: all class roles are taken over automatically, members pick their main class, the main class is listed first everywhere
- Discord sync (login and live check) stores every class role of a member in users.classes (role names match the classes, German and English names are recognised); users.class_name is the main class: the one chosen in the profile (users.main_class) while the member still has it, otherwise the highest class role; no class role means no class
- The guild role list is fetched once per request and kept when the bot is briefly unreachable (it used to be emptied); nothing is changed while the role list is unknown
- Profile: the manual class select is gone; new "Klassen" card with the detected classes, main class choice for members with several classes and a "Jetzt mit Discord abgleichen" button (at most every 30 s and 10 per hour, new security bucket discord_sync with a log entry)
- Main class first on the profile page, in the member list (the class filter also finds secondary classes), in the raid signup and premade class pickers (own classes first, main preselected for new raid signups)
- Berechtigungsgruppen shows which Discord roles count as classes
- Schema version 36 (users.classes filled from class_name until the first sync, users.main_class), patch notes, README

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 17:34:37 +02:00

218 lines
13 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');
$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,
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
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 if ($canCreate): ?>
<p><a class="button" href="/admin/user_form.php">Neuen Benutzer anlegen</a></p>
<?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>
<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'] ?? ''))) ?>"
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><?= e($u['role_names'] ?: '-') ?></td>
<td>
<div class="badge-group">
<span class="badge <?= e($statusClass) ?>"><?= e($statusLabel) ?></span>
<?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 ($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'; ?>