LuminaWebsite/discord_callback.php
Maaxxs b96bbd1780 Consilium: own Discord server - its roles decide who sees the Consilium; rights for Consilium raids and entries
Asked for: the alliances move to their own Discord server with the roles Force Commander, Legion Lead, Legion
Officer and Legionär. Decided: everyone with a role there sees the Consilium (our members too), no role per legion
(assignment stays on the website), new accounts are approved by our admins, rights per role as planned, a
permission for Consilium calendar entries that is assigned later. Server ID 1553882046793777283.

- Two Discord servers: settings.consilium_guild_id (seeded, Verwaltung -> Discord-Bot with a check whether the bot
  is there); a group's role lies on one of them (roles.discord_guild_id, NULL = legion, schema 71). Login and the
  periodic sync read both memberships (discord_fetch_memberships/discord_apply_memberships) and sync groups per
  server; name, avatar, joined date and classes come from the legion server, for allies from the Consilium server.
- Leaving: whoever leaves a server loses its groups; an account is deactivated only when it is on neither
  (discord_member_left_guild; bot_api.php takes in_guilds from the bot, the sync checks all servers, older bot
  versions cannot deactivate Consilium members). Logout text no longer names a server.
- Group form lists the roles of both servers, the group list marks Consilium roles; Consilium-Raids channel is
  chosen from the Consilium server's channels.
- Rights per area: consilium.raids / consilium.raids.manage for Consilium raids (create, manage, restore, delete),
  raids.create / raids.manage only for legion raids (raid_area_can_create, raid_area_can_manage_all,
  raid_can_manage asks the raid's area). consilium.calendar: only with it Consilium calendar entries (allies without
  it cannot add entries; legion members with it get "Für wen?" with legion/Consilium).
- Consilium member list: everyone with a role on the Consilium server - our legion as its own section, allied legions,
  without legion - with the Consilium ranks as tags; legion list and profiles leave the Consilium groups out for
  those who do not see the Consilium. Allies pick their class in the profile.
- Consilium admin page: setup checklist for the new structure with the groups of the Consilium server and their
  rights (setup only for roles.manage). Privacy page, README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 02:55:59 +02:00

80 lines
3.3 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']);
// Server der Legion und - fuer die verbuendeten Legionen - der des Consilium Elyseum (includes/discord_auth.php)
[$member, $consiliumMember] = discord_fetch_memberships($tokens['access_token']);
} catch (Throwable $e) {
set_flash('error', 'Discord-Anmeldung fehlgeschlagen: ' . $e->getMessage());
redirect($backTo);
}
// Nur wer auf einem unserer Discord-Server ist, kommt hinein - auch beim Verknuepfen.
if ($member === null && $consiliumMember === 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_memberships((int) $currentUser['id'], $member, $consiliumMember);
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 ?? $consiliumMember);
} 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_memberships($account['id'], $member, $consiliumMember);
auth_login_session($account['id'], 'discord');
redirect($account['status'] === 'pending' ? '/pending.php' : auth_login_return_target());