LuminaWebsite/includes/auth.php
Maaxxs 8c703f0e1f Add Discord account linking and raid signup/management from the website
- Class name on raid signups now shows only as an icon tooltip instead
  of trailing text (falls back to text when no icon matches).
- Discord OAuth2 linking (discord_login.php/discord_callback.php,
  includes/discord.php): members connect their Discord account from
  the dashboard, storing discord_id/discord_username so raid signups
  can be attributed to their real Discord identity. Needs a Discord
  application's client_id/client_secret in config.php.
- raids.php gets self-service signup/withdraw controls (class + group
  picker, upsert semantics) for members with a linked Discord account,
  plus archive/restore controls gated behind a new raids.manage
  permission. New action scripts raid_signup.php, raid_unsignup.php,
  raid_archive.php, raid_restore.php call the corresponding
  includes/raids_api.php functions.
- Archive/restore already work today (verified live against the bot's
  API - archived and restored the one active raid, signups intact).
  Signup/withdraw are fully wired on the website side but need two new
  bot-side endpoints (POST .../signup, DELETE .../signup/{user_id})
  that don't exist yet - contract documented in the README.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-18 13:25:41 +02:00

69 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
function current_user(): ?array
{
static $user = null;
static $loaded = false;
if ($loaded) {
return $user;
}
$loaded = true;
if (empty($_SESSION['user_id'])) {
return null;
}
$stmt = db()->prepare('SELECT id, username, display_name, status, must_change_password, discord_id, discord_username FROM users WHERE id = ?');
$stmt->execute([$_SESSION['user_id']]);
$row = $stmt->fetch();
if (!$row || $row['status'] !== 'active') {
// Benutzer wurde gelöscht oder deaktiviert -> Session beenden.
session_destroy();
return null;
}
$user = $row;
return $user;
}
function require_login(): void
{
$user = current_user();
if (!$user) {
redirect('/login.php');
}
$exemptScripts = ['change_password.php', 'logout.php'];
$currentScript = basename((string) ($_SERVER['SCRIPT_NAME'] ?? ''));
if (!empty($user['must_change_password']) && !in_array($currentScript, $exemptScripts, true)) {
redirect('/change_password.php');
}
}
function attempt_login(string $username, string $password): bool
{
$stmt = db()->prepare('SELECT id, password_hash, status FROM users WHERE username = ?');
$stmt->execute([$username]);
$row = $stmt->fetch();
if (!$row || $row['status'] !== 'active' || !password_verify($password, $row['password_hash'])) {
return false;
}
session_regenerate_id(true);
$_SESSION['user_id'] = $row['id'];
$update = db()->prepare('UPDATE users SET last_login = NOW() WHERE id = ?');
$update->execute([$row['id']]);
return true;
}
function logout(): void
{
$_SESSION = [];
session_destroy();
}