LuminaWebsite/admin/users.php
Maaxxs ba8cf2a2ef Admin: sync Discord roles on demand
Besides the automatic check every five minutes during a session, the user
list gets "Jetzt mit Discord abgleichen" (all linked accounts, logged in or
not) and the user form "Jetzt abgleichen" (one account), both for users.edit.
They run the same check with the stored token, just without waiting for the
interval, and report how many accounts were synced, left the server, have an
expired Discord login, never logged in via Discord, or could not be reached.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 08:06:51 +02:00

130 lines
6.2 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');
$currentUser = current_user();
// Wer auf die Freischaltung wartet, steht oben - das ist die Liste, die man abarbeitet.
$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.discord_id, u.discord_username,
GROUP_CONCAT(COALESCE(NULLIF(r.display_name, ''), r.name) ORDER BY 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
ORDER BY u.status = 'pending' DESC, u.username"
)->fetchAll();
$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 und Rollen 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; ?>
<div class="table-scroll">
<table class="table-actions">
<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 foreach ($users as $u): ?>
<?php [$statusLabel, $statusClass] = $statusLabels[$u['status']] ?? [$u['status'], '']; ?>
<tr<?= $u['status'] === 'pending' ? ' class="row-pending"' : '' ?>>
<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 (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): ?>
<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 ($canDelete && (int) $u['id'] !== (int) $currentUser['id']): ?>
<form class="inline-form" method="post" action="/admin/user_delete.php" onsubmit="return confirm('Diesen Benutzer wirklich 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>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>