LuminaWebsite/admin/discord_sync_action.php
Maaxxs 27470d0fd7 Accounts: whoever leaves the Discord server is deactivated; on return the account waits for approval again
Asked for: "wenn ein mitglied die legion verlässt, soll das benutzerprofil deaktiviert werden" - decided: leaving the
Discord server (also kick or ban) counts as leaving the legion; a returning member goes back to "Wartet auf
Freischaltung".

- includes/discord_auth.php: discord_member_left() disables active/pending accounts (status disabled, users.left_server_at,
  session_epoch + 1, Discord groups removed, log entry "Automatisch (...)"); developers stay untouched.
  discord_member_returned() puts such accounts back to pending (left_server_at cleared); accounts disabled by hand
  stay locked. discord_live_check() calls both, so the session check and "Jetzt mit Discord abgleichen" deactivate too.
- bot_api.php for the new bot cog (cogs/legion_members.py): member_left, member_joined, legion_accounts (Discord IDs of
  active/pending accounts) and legion_sync (the ones the bot misses on the server). Other guilds are ignored; a sync
  that would disable more than half of the accounts is refused with a warning in the log (at most once a day).
- discord_callback.php: a Discord login of a returned member puts the account back to pending instead of "gesperrt".
- includes/auth.php: logout message says the account was deactivated.
- Admin: users list shows "Discord verlassen am ...", the user form a hint; setting the status by hand clears the mark.
- Schema version 67 (users.left_server_at); account deletion clears it.
- Privacy page (automatic deactivation, bot sync), patch note, README.

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

58 lines
2.7 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 - das Konto ist deaktiviert, die Discord-Gruppen sind 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.'];
admin_log('🔄 Discord-Abgleich (ein Konto)', ['Benutzer' => admin_log_user($userId), 'Ergebnis' => $message]);
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 (deaktiviert, 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;
admin_log('🔄 Discord-Abgleich (alle Konten)', ['Ergebnis' => implode(', ', $parts)]);
set_flash($onlyOk ? 'success' : 'info', 'Discord-Abgleich: ' . implode(', ', $parts) . '.');
redirect('/admin/users.php');