LuminaWebsite/includes/permissions.php
Maaxxs 4d4a030b95 Add a Premades management page mirroring the Discord bot's /premade
Ports the bot's premade feature set (cogs/premades.py) to the
website using the raid API's existing, already-deployed premade
endpoints - no bot/API changes needed:

- Overview of all premades: lead, accepted members, open invites
- Found/rename/dissolve your own premade
- Invite a linked website member (sends the same Discord DM with
  Accept/Decline buttons the bot itself sends), remove a member,
  transfer leadership
- "Meine Einladungen": accept/decline invites addressed to you
- premades.manage lets admins manage any premade, not just their own,
  mirroring the bot's admin-only "Admin-Verwaltung" flow

New permissions premades.view/premades.manage (view granted to the
default Mitglied role on fresh installs; existing installs need to
add them manually, documented in README.md). Every mutation is
re-validated server-side against the current premade list (ownership,
one-premade-per-person, can't remove the lead, accept/decline only
for an invite that's actually pending for that user) since the UI's
button visibility alone isn't an authorization check.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-19 08:00:45 +02:00

53 lines
1.9 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'],
['key' => 'premades.view', 'description' => 'Premade-Gruppen einsehen'],
['key' => 'premades.manage', 'description' => 'Alle Premade-Gruppen verwalten (nicht nur die eigene)'],
];
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;
}
}