LuminaWebsite/admin/discord_sync_action.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

56 lines
2.4 KiB
PHP

<?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');