The login page leads with "Mit Discord einloggen"; username/password stays available below and can be allowed or refused per user. The Discord login asks for identify + guilds.members.read, so the site itself can check that the account is on the legion's server and read its roles - no bot token on the webspace. Unknown accounts get a new user in the new status "pending", Discord-only, who sees nothing but a "Freischaltung ausstehend" page and has no permissions until approved from the user list. Permission groups can be linked to a Discord role. Linked groups are granted and revoked from the member's roles at every Discord login and then every five minutes during a session, using the stored (refreshed) user token; leaving the server removes them and ends a Discord session. Manual groups are untouched, and several linked roles simply add up their groups. New columns are added automatically on first request (includes/schema.php), since the login page itself needs them; existing accounts keep password login. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
75 lines
2.8 KiB
PHP
75 lines
2.8 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');
|
|
}
|
|
|
|
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' : '/dashboard.php');
|