LuminaWebsite/includes/permissions.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

51 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
// Feste Kern-Berechtigungen, die das System versteht/pflegt.
// Admins können in der Oberfläche zusätzliche eigene Berechtigungen anlegen,
// die dann z.B. für spätere Features verwendet werden können.
const CORE_PERMISSIONS = [
['key' => 'dashboard.view', 'description' => 'Zugriff auf das Mitglieder-Dashboard'],
['key' => 'admin.access', 'description' => 'Zugriff auf den Admin-Bereich'],
['key' => 'users.view', 'description' => 'Benutzerliste einsehen'],
['key' => 'users.create', 'description' => 'Neue Benutzer anlegen'],
['key' => 'users.edit', 'description' => 'Benutzer bearbeiten'],
['key' => 'users.delete', 'description' => 'Benutzer löschen'],
['key' => 'roles.manage', 'description' => 'Berechtigungsgruppen verwalten'],
['key' => 'raids.view', 'description' => 'Raids einsehen'],
['key' => 'raids.manage', 'description' => 'Raids archivieren/wiederherstellen'],
];
function user_has_permission(string $key): bool
{
$user = current_user();
if (!$user) {
return false;
}
static $cache = [];
if (array_key_exists($key, $cache)) {
return $cache[$key];
}
$stmt = db()->prepare(
'SELECT COUNT(*) FROM user_roles ur
JOIN role_permissions rp ON rp.role_id = ur.role_id
JOIN permissions p ON p.id = rp.permission_id
WHERE ur.user_id = ? AND p.key = ?'
);
$stmt->execute([$user['id'], $key]);
$cache[$key] = ((int) $stmt->fetchColumn()) > 0;
return $cache[$key];
}
function require_permission(string $key): void
{
require_login();
if (!user_has_permission($key)) {
http_response_code(403);
include __DIR__ . '/forbidden.php';
exit;
}
}