309 lines
20 KiB
PHP
309 lines
20 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');
|
||
$canMessage = user_has_permission('users.message');
|
||
$currentUser = current_user();
|
||
// Gruppennamen fuer die Vergabe-Rechte (roles.assign.<ID>) in den Tooltips der Einzelrechte
|
||
$roleLabelById = array_map('strval', db()->query("SELECT id, COALESCE(NULLIF(display_name, ''), name) FROM roles")->fetchAll(PDO::FETCH_KEY_PAIR));
|
||
|
||
$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, u.left_server_at,
|
||
MIN(r.roster_order) AS best_rank, MIN(r.rank_order) AS best_tier,
|
||
GROUP_CONCAT(COALESCE(NULLIF(r.display_name, ''), r.name) ORDER BY " . ROLE_TAG_ORDER_SQL . " 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,
|
||
" . sql_is_consilium_only('u') . " AS is_ally_perm, " . sql_is_legion_member('u') . " AS is_legion_perm,
|
||
u.consilium_legion_id, MAX(cl.name) AS ally_legion,
|
||
(SELECT COUNT(*) FROM user_notices n WHERE n.user_id = u.id AND n.read_at IS NULL) AS notices_unread,
|
||
(SELECT MAX(" . user_notice_level_sql('n.level') . ") FROM user_notices n WHERE n.user_id = u.id AND n.read_at IS NULL) AS notice_level
|
||
FROM users u
|
||
LEFT JOIN user_roles ur ON ur.user_id = u.id
|
||
LEFT JOIN roles r ON r.id = ur.role_id
|
||
LEFT JOIN consilium_legions cl ON cl.id = u.consilium_legion_id
|
||
GROUP BY u.id"
|
||
)->fetchAll();
|
||
|
||
// Zwei Reiter: Legion und Elyos Allianz. Zur Allianz gehoert, wer nur deren Rechte hat - oder (etwa nach dem Verlassen
|
||
// des Servers ohne Gruppen) keine Legionsrechte, aber eine verbuendete Legion. Mitglieder der Legion, die auch auf dem
|
||
// Server der Elyos Allianz sind, stehen bei der Legion.
|
||
$isAlly = static fn (array $u): bool => !empty($u['is_ally_perm'])
|
||
|| (empty($u['is_legion_perm']) && !empty($u['consilium_legion_id']));
|
||
$showAreas = consilium_enabled();
|
||
// Der Reiter bleibt gemerkt - Aktionen (Freischalten, Loeschen ...) leiten ohne ihn hierher zurueck
|
||
$requestedArea = (string) ($_GET['bereich'] ?? ($_SESSION['users_area'] ?? ''));
|
||
$area = $showAreas && $requestedArea === 'allianz' ? 'allianz' : 'legion';
|
||
$_SESSION['users_area'] = $area;
|
||
$areaCounts = ['legion' => [0, 0], 'allianz' => [0, 0]]; // [alle, wartend]
|
||
foreach ($users as $u) {
|
||
$key = $isAlly($u) ? 'allianz' : 'legion';
|
||
$areaCounts[$key][0]++;
|
||
if ($u['status'] === 'pending') {
|
||
$areaCounts[$key][1]++;
|
||
}
|
||
}
|
||
if ($showAreas) {
|
||
$users = array_values(array_filter($users, static fn (array $u): bool => ($isAlly($u) ? 'allianz' : 'legion') === $area));
|
||
}
|
||
|
||
// 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) - und
|
||
// darin nach dem hoechsten Rang (Rangordnung), wie in der Mitgliederliste.
|
||
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,
|
||
$u['best_tier'] !== null ? (int) $u['best_tier'] : 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" id="discord-sync-all" 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 if ($showAreas): ?>
|
||
<nav class="members-segment page-tabs user-area-tabs" aria-label="Bereiche">
|
||
<?php foreach (['legion' => ['Legion', '?bereich=legion', ''], 'allianz' => [CONSILIUM_NAME, '?bereich=allianz', icon('shield-check') . ' ']] as $key => [$label, $query, $iconHtml]): ?>
|
||
<a href="/admin/users.php<?= $query ?>" class="<?= $area === $key ? 'active' : '' ?>"><?= $iconHtml ?><?= e($label) ?>
|
||
<span class="chip-count"><?= $areaCounts[$key][0] ?></span>
|
||
<?php if ($areaCounts[$key][1] > 0): ?><span class="badge badge-pending" title="Warten auf Freischaltung"><?= $areaCounts[$key][1] ?> neu</span><?php endif; ?>
|
||
</a>
|
||
<?php endforeach; ?>
|
||
</nav>
|
||
<?php if ($area === 'allianz'): ?>
|
||
<p class="text-muted">Konten der verbündeten Legionen – sie sehen nur den Bereich <?= e(CONSILIUM_NAME) ?>. Zu welcher Legion jemand gehört, steht unter <a href="/admin/consilium.php"><?= e(CONSILIUM_NAME) ?></a>.</p>
|
||
<?php endif; ?>
|
||
<?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>
|
||
<?php if ($area === 'allianz'): ?><th>Legion</th><?php endif; ?>
|
||
<th>Berechtigungsgruppen</th>
|
||
<th>Status</th>
|
||
<th>Letzter Login</th>
|
||
<?php if ($canEdit || $canDelete || $canMessage): ?><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'] ?? '') . ' ' . ($u['ally_legion'] ?? ''))) ?>"
|
||
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>
|
||
<?php if ($area === 'allianz'): ?>
|
||
<td><?= $u['ally_legion'] !== null ? e((string) $u['ally_legion']) : '<span class="text-muted">-</span>' ?></td>
|
||
<?php endif; ?>
|
||
<td>
|
||
<?php
|
||
// Einzelrechte mit ausgeschriebenem Namen im Tooltip ("Musik steuern" statt music.control)
|
||
$directKeys = $u['direct_permissions'] ? explode(', ', $u['direct_permissions']) : [];
|
||
$directCount = count($directKeys);
|
||
$directNames = array_map(static fn (string $key): string => permission_label($key, $roleLabelById), $directKeys);
|
||
?>
|
||
<?= $u['role_names'] || !$directCount ? e($u['role_names'] ?: '-') : '' ?>
|
||
<?php if ($directCount): ?>
|
||
<span class="badge badge-direct-permissions" title="Einzeln vergeben: <?= e(implode(', ', $directNames)) ?>">+<?= $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['left_server_at']) && $u['status'] === 'disabled' && empty($u['deleted_at'])): ?>
|
||
<span class="badge badge-disabled" title="Automatisch deaktiviert - kommt die Person zurück auf den Discord-Server, wartet das Konto wieder auf Freischaltung">Discord verlassen am <?= e(date('d.m.', (int) strtotime((string) $u['left_server_at']))) ?></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 ($canMessage && (int) $u['notices_unread'] > 0): ?>
|
||
<?php $noticeLevel = array_keys(USER_NOTICE_LEVELS)[max(0, (int) $u['notice_level'] - 1)] ?? 'info'; ?>
|
||
<a class="badge user-notice-badge user-notice-<?= e($noticeLevel) ?>" href="/admin/user_notice.php?id=<?= (int) $u['id'] ?>"
|
||
title="Persönliche Nachricht noch nicht gelesen"><?= icon('message') ?> <?= (int) $u['notices_unread'] === 1 ? 'Nachricht' : (int) $u['notices_unread'] . ' Nachrichten' ?> ungelesen</a>
|
||
<?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 || $canMessage): ?>
|
||
<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 ($canMessage && empty($u['deleted_at'])): ?>
|
||
<a class="icon-button" href="/admin/user_notice.php?id=<?= (int) $u['id'] ?>" title="Persönliche Nachricht (erscheint als Popup)" aria-label="Persönliche Nachricht"><?= icon('message') ?></a>
|
||
<?php endif; ?>
|
||
<?php if ($canExport && !$protectedAdmin): ?>
|
||
<a class="icon-button" href="/admin/user_export.php?id=<?= (int) $u['id'] ?>" title="Daten & 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 if ($canEdit): ?><script src="<?= e(asset_url('/assets/js/discord-sync.js')) ?>"></script><?php endif; ?>
|
||
<?php include __DIR__ . '/../includes/footer.php'; ?>
|