LuminaWebsite/discord_callback.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

79 lines
3.1 KiB
PHP

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/discord_auth.php';
$state = (string) ($_GET['state'] ?? '');
$code = (string) ($_GET['code'] ?? '');
$expectedState = (string) ($_SESSION['discord_oauth_state'] ?? '');
$mode = (string) ($_SESSION['discord_oauth_mode'] ?? 'login');
unset($_SESSION['discord_oauth_state'], $_SESSION['discord_oauth_mode']);
$currentUser = current_user();
if ($mode === 'link' && !$currentUser) {
$mode = 'login';
}
$backTo = $mode === 'link' ? '/profile.php' : '/login.php';
if (!empty($_GET['error'])) {
set_flash('error', $mode === 'link' ? 'Discord-Verknüpfung wurde abgebrochen.' : 'Discord-Anmeldung wurde abgebrochen.');
redirect($backTo);
}
if ($code === '' || $state === '' || $expectedState === '' || !hash_equals($expectedState, $state)) {
set_flash('error', 'Ungültige Antwort von Discord. Bitte erneut versuchen.');
redirect($backTo);
}
try {
$tokens = discord_exchange_code($code);
$discordUser = discord_fetch_user($tokens['access_token']);
$member = discord_fetch_member($tokens['access_token']);
} catch (Throwable $e) {
set_flash('error', 'Discord-Anmeldung fehlgeschlagen: ' . $e->getMessage());
redirect($backTo);
}
// Nur wer auf dem Discord-Server der Legion ist, kommt hinein - auch beim Verknuepfen.
if ($member === null) {
set_flash('error', 'Der Discord-Account "' . $discordUser['username'] . '" ist nicht auf dem Discord-Server der Legion.');
redirect($backTo);
}
// ---- Verknuepfen: angemeldetes Konto bekommt den Discord-Account ----
if ($mode === 'link') {
try {
db()->prepare('UPDATE users SET discord_id = ?, discord_username = ? WHERE id = ?')
->execute([$discordUser['id'], $discordUser['username'], $currentUser['id']]);
} catch (PDOException $e) {
set_flash('error', 'Dieser Discord-Account ist bereits mit einem anderen Benutzer verknüpft.');
redirect('/profile.php');
}
discord_store_tokens((int) $currentUser['id'], $tokens);
discord_apply_member((int) $currentUser['id'], $member);
set_flash('success', 'Discord-Account "' . $discordUser['username'] . '" wurde verknüpft.');
redirect('/profile.php');
}
// ---- Anmelden: bestehendes Konto oder neues (noch nicht freigeschaltetes) ----
try {
$account = discord_find_or_create_user($discordUser, $member);
} catch (Throwable $e) {
set_flash('error', 'Das Konto konnte nicht angelegt werden: ' . $e->getMessage());
redirect('/login.php');
}
// Zurueck auf dem Server, nachdem das Konto beim Verlassen deaktiviert wurde: wartet wieder auf Freischaltung
if ($account['status'] === 'disabled' && discord_member_returned($account['id'], 'Discord-Login')) {
$account['status'] = 'pending';
}
if ($account['status'] === 'disabled') {
set_flash('error', 'Dieses Konto ist gesperrt.');
redirect('/login.php');
}
discord_store_tokens($account['id'], $tokens);
discord_apply_member($account['id'], $member);
auth_login_session($account['id'], 'discord');
redirect($account['status'] === 'pending' ? '/pending.php' : auth_login_return_target());