LuminaWebsite/includes/schema.php
Maaxxs 01719dd958 Login: Discord first, pending approval, groups synced from Discord roles
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>
2026-09-21 07:49:47 +02:00

91 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* Bringt eine bestehende Datenbank auf den Stand, den der Code erwartet. Frueher standen
* dafuer ALTER-Befehle in der README, die von Hand in phpMyAdmin auszufuehren waren - fuer
* den Discord-Login geht das nicht mehr, weil schon die Login-Seite die neuen Spalten
* braucht (ohne sie koennte sich niemand mehr anmelden, auch kein Administrator).
*
* Geprueft wird einmal pro Sitzung; ist alles da, kostet das eine einzige Abfrage.
*/
// Hochzaehlen, sobald unten eine Aenderung dazukommt - dann pruefen alle Sitzungen neu.
const SCHEMA_VERSION = 1;
/**
* Spalten, die nachtraeglich dazugekommen sind: Tabelle => [Spalte => ALTER-Befehl].
* Die Befehle muessen zu sql/schema.sql passen (dort stehen sie fuer Neuinstallationen).
*/
function schema_column_upgrades(): array
{
return [
'users' => [
'password_login' => "ALTER TABLE users ADD COLUMN password_login TINYINT(1) NOT NULL DEFAULT 1 AFTER must_change_password",
'discord_access_token' => "ALTER TABLE users ADD COLUMN discord_access_token VARCHAR(255) NULL AFTER discord_username",
'discord_refresh_token' => "ALTER TABLE users ADD COLUMN discord_refresh_token VARCHAR(255) NULL AFTER discord_access_token",
'discord_token_expires' => "ALTER TABLE users ADD COLUMN discord_token_expires DATETIME NULL AFTER discord_refresh_token",
'discord_synced_at' => "ALTER TABLE users ADD COLUMN discord_synced_at DATETIME NULL AFTER discord_token_expires",
],
'roles' => [
'discord_role_id' => "ALTER TABLE roles ADD COLUMN discord_role_id BIGINT UNSIGNED NULL AFTER roster_order",
],
];
}
function ensure_schema(): void
{
if (($_SESSION['schema_version'] ?? 0) === SCHEMA_VERSION) {
return;
}
$pdo = db();
$upgrades = schema_column_upgrades();
$stmt = $pdo->query(
"SELECT LOWER(table_name) AS t, LOWER(column_name) AS c FROM information_schema.columns
WHERE table_schema = DATABASE() AND table_name IN ('users', 'roles')"
);
$existing = [];
foreach ($stmt->fetchAll() as $row) {
$existing[$row['t']][$row['c']] = true;
}
// Noch gar keine Tabellen: frische Installation, schema.sql ist noch nicht importiert.
if (empty($existing['users'])) {
return;
}
$statements = [];
foreach ($upgrades as $table => $columns) {
foreach ($columns as $column => $sql) {
if (empty($existing[$table][$column])) {
$statements[] = $sql;
}
}
}
// Der Status "pending" (Freischaltung ausstehend) kam mit dem Discord-Login dazu.
$enum = $pdo->query(
"SELECT column_type FROM information_schema.columns
WHERE table_schema = DATABASE() AND table_name = 'users' AND column_name = 'status'"
)->fetchColumn();
if (is_string($enum) && strpos($enum, "'pending'") === false) {
$statements[] = "ALTER TABLE users MODIFY status ENUM('active', 'disabled', 'pending') NOT NULL DEFAULT 'active'";
}
try {
foreach ($statements as $sql) {
$pdo->exec($sql);
}
} catch (Throwable $e) {
http_response_code(500);
header('Content-Type: text/plain; charset=UTF-8');
echo "Die Datenbank konnte nicht automatisch aktualisiert werden:\n" . $e->getMessage()
. "\n\nBitte diese Befehle einmal in phpMyAdmin ausfuehren und die Seite neu laden:\n\n"
. implode(";\n", $statements) . ";\n";
exit;
}
$_SESSION['schema_version'] = SCHEMA_VERSION;
}