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>
This commit is contained in:
parent
f5c7a6f776
commit
ba8cf2a2ef
6 changed files with 96 additions and 7 deletions
|
|
@ -162,7 +162,10 @@ des Nutzers, das bei Bedarf erneuert wird. Ist der Account nicht mehr auf dem Se
|
|||
alle Discord-Gruppen entzogen, und wer über Discord angemeldet ist, wird abgemeldet. Ist
|
||||
Discord nicht erreichbar, bleibt alles, wie es ist. Nach dem Ändern der Discord-Rolle einer
|
||||
Gruppe werden alle Konten beim nächsten Seitenaufruf neu abgeglichen. Wer nicht angemeldet
|
||||
ist, wird erst bei der nächsten Anmeldung abgeglichen.
|
||||
ist, wird erst bei der nächsten Anmeldung abgeglichen - oder sofort über den Knopf
|
||||
**Jetzt mit Discord abgleichen** in der Benutzerverwaltung (alle Konten) bzw. **Jetzt abgleichen**
|
||||
im Benutzerformular (ein Konto), beides mit `users.edit`. Das geht bei jedem Konto, das sich
|
||||
schon einmal über Discord angemeldet hat, egal ob es gerade eingeloggt ist.
|
||||
|
||||
**Mehrere Rollen** (z.B. *Sedes Lumines* als Leitung, die zugleich *Lumina Ascendens* hat):
|
||||
Jede verknüpfte Rolle bringt ihre Gruppe mit, die Rechte addieren sich. Am besten bekommt die
|
||||
|
|
|
|||
56
admin/discord_sync_action.php
Normal file
56
admin/discord_sync_action.php
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
require_once __DIR__ . '/../includes/bootstrap.php';
|
||||
require_permission('users.edit');
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||
redirect('/admin/users.php');
|
||||
}
|
||||
verify_csrf();
|
||||
|
||||
// Manueller Abgleich mit Discord - fuer ein Konto (aus dem Benutzerformular) oder fuer alle.
|
||||
$userId = (int) ($_POST['user_id'] ?? 0);
|
||||
|
||||
if ($userId > 0) {
|
||||
$result = discord_live_check($userId, true);
|
||||
$messages = [
|
||||
'ok' => ['success', 'Mit Discord abgeglichen - die Berechtigungsgruppen sind aktuell.'],
|
||||
'left' => ['error', 'Der Account ist nicht mehr auf dem Discord-Server - die Discord-Gruppen wurden entzogen.'],
|
||||
'revoked' => ['error', 'Die Discord-Anmeldung dieses Kontos ist abgelaufen. Abgeglichen wird wieder, sobald sich der Benutzer über Discord anmeldet.'],
|
||||
'no_token' => ['error', 'Dieses Konto hat sich noch nie über Discord angemeldet - abgleichen lässt es sich erst nach der ersten Discord-Anmeldung.'],
|
||||
'error' => ['error', 'Discord war gerade nicht erreichbar. Bitte später erneut versuchen.'],
|
||||
];
|
||||
[$type, $message] = $messages[$result] ?? ['error', 'Abgleich fehlgeschlagen.'];
|
||||
set_flash($type, $message);
|
||||
redirect('/admin/user_form.php?id=' . $userId);
|
||||
}
|
||||
|
||||
// Alle Konten mit Discord-Verknuepfung. Pro Konto zwei kurze Anfragen an Discord - bei
|
||||
// einer Legion mit ein paar Dutzend Mitgliedern dauert das einige Sekunden.
|
||||
if (function_exists('set_time_limit')) {
|
||||
@set_time_limit(300);
|
||||
}
|
||||
|
||||
$ids = db()->query('SELECT id FROM users WHERE discord_id IS NOT NULL ORDER BY id')->fetchAll(PDO::FETCH_COLUMN);
|
||||
$counts = ['ok' => 0, 'left' => 0, 'revoked' => 0, 'no_token' => 0, 'error' => 0];
|
||||
foreach ($ids as $id) {
|
||||
$result = discord_live_check((int) $id, true);
|
||||
$counts[$result] = ($counts[$result] ?? 0) + 1;
|
||||
}
|
||||
|
||||
$parts = [$counts['ok'] . ' abgeglichen'];
|
||||
if ($counts['left'] > 0) {
|
||||
$parts[] = $counts['left'] . ' nicht mehr auf dem Server (Discord-Gruppen entzogen)';
|
||||
}
|
||||
if ($counts['revoked'] > 0) {
|
||||
$parts[] = $counts['revoked'] . ' mit abgelaufener Discord-Anmeldung';
|
||||
}
|
||||
if ($counts['no_token'] > 0) {
|
||||
$parts[] = $counts['no_token'] . ' noch nie über Discord angemeldet';
|
||||
}
|
||||
if ($counts['error'] > 0) {
|
||||
$parts[] = $counts['error'] . ' fehlgeschlagen (Discord nicht erreichbar)';
|
||||
}
|
||||
|
||||
$onlyOk = $counts['left'] + $counts['revoked'] + $counts['no_token'] + $counts['error'] === 0;
|
||||
set_flash($onlyOk ? 'success' : 'info', 'Discord-Abgleich: ' . implode(', ', $parts) . '.');
|
||||
redirect('/admin/users.php');
|
||||
|
|
@ -151,6 +151,7 @@ include __DIR__ . '/../includes/header.php';
|
|||
<?= discord_logo(16) ?>
|
||||
<?php if ($hasDiscord): ?>
|
||||
Verknüpft mit <strong><?= e((string) $user['discord_username']) ?></strong>
|
||||
<button type="submit" form="discord-sync-user" class="button-secondary button-small">Jetzt abgleichen</button>
|
||||
<?php else: ?>
|
||||
<span class="text-muted">Kein Discord-Account verknüpft</span>
|
||||
<?php endif; ?>
|
||||
|
|
@ -225,4 +226,10 @@ include __DIR__ . '/../includes/header.php';
|
|||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<?php if ($isEdit && $hasDiscord): ?>
|
||||
<form id="discord-sync-user" method="post" action="/admin/discord_sync_action.php" hidden>
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="user_id" value="<?= (int) $editId ?>">
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
<?php include __DIR__ . '/../includes/footer.php'; ?>
|
||||
|
|
|
|||
|
|
@ -43,6 +43,12 @@ include __DIR__ . '/../includes/header.php';
|
|||
<?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">
|
||||
|
|
|
|||
|
|
@ -2029,3 +2029,17 @@ a.icon-button:hover { text-decoration: none; }
|
|||
|
||||
/* Aktionen-Spalte der Verwaltung: Icons und Freischalten-Knopf auf einer Linie. */
|
||||
.table-actions td:last-child .actions { align-items: center; flex-wrap: nowrap; }
|
||||
|
||||
.discord-sync-form {
|
||||
display: flex;
|
||||
/* Formulare sind global als Spalte gesetzt - hier Knopf und Erklaerung nebeneinander. */
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem 1rem;
|
||||
margin: 0 0 1rem;
|
||||
}
|
||||
|
||||
.discord-sync-form span { flex: 1; min-width: 240px; font-size: 0.85rem; }
|
||||
|
||||
.flash-info { background: rgba(201, 162, 39, 0.12); border: 1px solid var(--gold); color: var(--gold); }
|
||||
|
|
|
|||
|
|
@ -122,12 +122,14 @@ function discord_find_or_create_user(array $discordUser, array $member): array
|
|||
}
|
||||
|
||||
/**
|
||||
* Prueft waehrend einer Sitzung erneut bei Discord, ob der Nutzer noch auf dem Server ist
|
||||
* und welche Rollen er hat - hoechstens alle DISCORD_SYNC_INTERVAL Sekunden.
|
||||
* Prueft erneut bei Discord, ob der Nutzer noch auf dem Server ist und welche Rollen er hat.
|
||||
* Automatisch waehrend einer Sitzung hoechstens alle DISCORD_SYNC_INTERVAL Sekunden, mit
|
||||
* $force (manueller Abgleich in der Benutzerverwaltung) sofort.
|
||||
*
|
||||
* @return string ok | skipped | left (nicht mehr auf dem Server) | revoked (Token ungueltig) | error
|
||||
* @return string ok | skipped (Intervall noch nicht um) | no_token (nie per Discord angemeldet)
|
||||
* | left (nicht mehr auf dem Server) | revoked (Token ungueltig) | error
|
||||
*/
|
||||
function discord_live_check(int $userId): string
|
||||
function discord_live_check(int $userId, bool $force = false): string
|
||||
{
|
||||
$stmt = db()->prepare(
|
||||
'SELECT discord_access_token, discord_refresh_token, discord_token_expires, discord_synced_at
|
||||
|
|
@ -137,9 +139,10 @@ function discord_live_check(int $userId): string
|
|||
$row = $stmt->fetch();
|
||||
|
||||
if (!$row || empty($row['discord_refresh_token'])) {
|
||||
return 'skipped';
|
||||
return 'no_token';
|
||||
}
|
||||
if ($row['discord_synced_at'] !== null && strtotime((string) $row['discord_synced_at']) > time() - DISCORD_SYNC_INTERVAL) {
|
||||
if (!$force && $row['discord_synced_at'] !== null
|
||||
&& strtotime((string) $row['discord_synced_at']) > time() - DISCORD_SYNC_INTERVAL) {
|
||||
return 'skipped';
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue