LuminaWebsite/includes/schema.php
Maaxxs 0eb8816ef8 Admin: switch for unlinking Discord, keep table actions in view
The user list gets a switch "Mitglieder dürfen ihre Discord-Verknüpfung im
Profil lösen" (users.edit, default on). Turned off, the profile shows a note
instead of the button and discord_unlink.php refuses the request. The value
lives in a new settings table, created automatically like the other schema
additions (SCHEMA_VERSION 2).

The user and group tables were wider than the 1100px content column, so the
actions column (edit/delete/approve) scrolled out of view. Both pages now
use the wide container and the actions column sticks to the right edge.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-21 08:00:06 +02:00

116 lines
4.2 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 = 2;
/**
* Tabellen, die nachtraeglich dazugekommen sind: Tabelle => CREATE-Befehl (wie in sql/schema.sql).
*/
function schema_table_upgrades(): array
{
return [
'settings' => <<<'SQL'
CREATE TABLE IF NOT EXISTS settings (
`key` VARCHAR(100) NOT NULL PRIMARY KEY,
value TEXT NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SQL,
];
}
/**
* 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 = [];
$tables = $pdo->query(
"SELECT LOWER(table_name) FROM information_schema.tables WHERE table_schema = DATABASE()"
)->fetchAll(PDO::FETCH_COLUMN);
foreach (schema_table_upgrades() as $table => $sql) {
if (!in_array($table, $tables, true)) {
$statements[] = $sql;
}
}
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;
}