LuminaWebsite/bot_api.php
Maaxxs 592269d300 Permissions per person: grant single permissions to users in addition to their groups
In the user form (admin/user_form.php) a new section "Einzelne Berechtigungen" lists all
permissions, with the group-assign rights (roles.assign.<ID>) shown by group name as in the role
form. It opens by itself when the user already has some. A shield marks what the user already
gets through one of the ticked groups; the tooltip says who granted a permission and when.

- New table user_permissions (user, permission, granted_by, created_at; schema 52, also in
  sql/schema.sql). user_has_permission() now resolves through user_permission_keys() (groups UNION
  direct permissions, falls back to groups only while the table is missing), and
  discord_ids_with_permission() and the raids.manage recipients in bot_api.php include direct
  permissions too.
- New core permission users.permissions: only with it can someone grant or revoke direct
  permissions, and only ones they have themselves (current_user_permission_ids()); others are
  greyed out and ignored on the server even if submitted. Your own direct permissions can only be
  changed by someone else (except developers). Without the right the section is a read-only list.
  On update, groups with roles.manage get users.permissions.
- Saving applies only the changes, so the remaining entries keep who granted them and when.
  Grants and revokes go into the audit log ("Einzelrechte erteilt/entzogen/jetzt").
- User list: "+N Einzelrechte" badge next to the groups (title lists them), searchable; the live
  fingerprint of the page includes user_permissions.
- Data export gets a section "einzelrechte", account deletion removes them, and the privacy
  policy mentions both. Patch notes, README and the roles page intro updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 02:20:58 +02:00

92 lines
3.7 KiB
PHP

<?php
/**
* Rueckkanal fuer den Discord-Bot (cogs/attendance.py im Bot).
*
* Wenn die Uhrzeit eines Raids erreicht ist, fragt der Bot hier nach, wer eine Erinnerung zur
* Anwesenheitsliste bekommen soll: der Raid-Lead (hat den Raid auf der Website angelegt) - auch wenn er
* selbst nicht angemeldet ist. Hat der Raid keinen (z.B. im Discord erstellt), alle Angemeldeten mit
* einem aktiven Website-Konto und der Berechtigung raids.manage. Die Discord-PN schickt der Bot selbst.
*
* GET /bot_api.php?action=attendance_recipients&raid_id=...&discord_ids=1,2,3
* Header: X-Bot-Key: <config.php bot_callback.key>
*/
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/raid_attendance.php';
header('Content-Type: application/json; charset=UTF-8');
function bot_api_reply(int $status, array $data): void
{
http_response_code($status);
echo json_encode($data, JSON_UNESCAPED_UNICODE);
exit;
}
$expectedKey = bot_callback_key();
if ($expectedKey === null) {
bot_api_reply(503, ['status' => 'error', 'message' => 'bot_callback.key ist in config.php nicht eingetragen.']);
}
$givenKey = (string) ($_SERVER['HTTP_X_BOT_KEY'] ?? '');
if (!hash_equals($expectedKey, $givenKey)) {
bot_api_reply(403, ['status' => 'error', 'message' => 'Ungültiger Schlüssel.']);
}
$action = (string) ($_GET['action'] ?? '');
if ($action !== 'attendance_recipients') {
bot_api_reply(400, ['status' => 'error', 'message' => 'Unbekannte Aktion.']);
}
$raidId = (string) ($_GET['raid_id'] ?? '');
if (!ctype_digit($raidId)) {
bot_api_reply(400, ['status' => 'error', 'message' => 'raid_id fehlt.']);
}
$discordIds = array_values(array_filter(
array_map('trim', explode(',', (string) ($_GET['discord_ids'] ?? ''))),
static fn (string $id): bool => $id !== '' && ctype_digit($id)
));
// Der Raid-Lead ist fuer die Anwesenheit zustaendig
$leadStmt = db()->prepare(
"SELECT u.discord_id FROM raid_settings s JOIN users u ON u.id = s.lead_user_id
WHERE s.raid_id = ? AND u.status = 'active' AND u.discord_id IS NOT NULL"
);
$leadStmt->execute([$raidId]);
$leadDiscordId = $leadStmt->fetchColumn();
$recipients = [];
if ($leadDiscordId !== false && $leadDiscordId !== null) {
$recipients = [(string) $leadDiscordId];
} elseif (!empty($discordIds)) {
$placeholders = implode(',', array_fill(0, count($discordIds), '?'));
$stmt = db()->prepare(
"SELECT DISTINCT u.discord_id FROM users u
WHERE u.status = 'active' AND u.discord_id IN ($placeholders)
AND (u.is_admin = 1 OR EXISTS (
SELECT 1 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 = u.id AND p.`key` = 'raids.manage'
) OR EXISTS (
SELECT 1 FROM user_permissions up
JOIN permissions p ON p.id = up.permission_id
WHERE up.user_id = u.id AND p.`key` = 'raids.manage'
))"
);
$stmt->execute($discordIds);
$recipients = array_map('strval', $stmt->fetchAll(PDO::FETCH_COLUMN));
}
// Raidbeginn: Einstellungen (fester Punkt) auch fuer im Discord erstellte Raids anlegen.
raid_settings_ensure_defaults([(int) $raidId]);
$settings = raid_settings_get((int) $raidId);
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = (string) ($_SERVER['HTTP_HOST'] ?? '');
bot_api_reply(200, [
'status' => 'success',
'recipients' => $recipients,
// Schon kontrolliert (z.B. vor Raidbeginn)? Dann braucht es keine Erinnerung mehr.
'checked' => $settings['attendance_checked_at'] !== null,
'url' => $scheme . '://' . $host . '/raid_attendance.php?raid=' . $raidId,
]);