Per-user administrator flag with all permissions, managed via users.admin
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
f773169a7e
commit
9412a59b72
11 changed files with 120 additions and 19 deletions
|
|
@ -653,6 +653,11 @@ Berechtigung `bugs.manage`), offene Meldungen stehen als Zahl am Menüpunkt. Die
|
|||
einzelnen Gruppe, `roles.assign.<ID>`. Diese Rechte legt die Website für jede Gruppe selbst
|
||||
an; im Gruppenformular stehen sie unter „Darf diese Gruppen an Benutzer vergeben“. Beim
|
||||
Update auf diese Version bekommt jede Gruppe mit `users.edit` einmalig `roles.assign`.
|
||||
- **Administratoren:** Einzelnen Benutzern kann man im Benutzerformular den Haken
|
||||
**Administrator** geben. Sie haben dann alle Berechtigungen, unabhängig von ihren Gruppen
|
||||
(Spalte `users.is_admin`). Vergeben und entziehen darf das nur, wer `users.admin` hat - nur
|
||||
diese dürfen Administratoren auch bearbeiten oder löschen. Die eigenen Admin-Rechte kann man
|
||||
sich nicht selbst entziehen. Das bei der Installation angelegte Konto ist Administrator.
|
||||
- Konten, die auf die Freischaltung warten, haben keinerlei Rechte.
|
||||
- Die Benutzerverwaltung selbst ist durch die Berechtigung `users.view` geschützt und daher nur
|
||||
für Mitglieder der Administrator-Gruppe (oder jeder Gruppe mit dieser Berechtigung) sichtbar.
|
||||
|
|
|
|||
|
|
@ -16,6 +16,14 @@ if ($id === (int) $currentUser['id']) {
|
|||
redirect('/admin/users.php');
|
||||
}
|
||||
|
||||
// Administratoren darf nur loeschen, wer selbst Administratoren verwalten darf.
|
||||
$adminStmt = db()->prepare('SELECT is_admin FROM users WHERE id = ?');
|
||||
$adminStmt->execute([$id]);
|
||||
if ((int) $adminStmt->fetchColumn() === 1 && !user_has_permission('users.admin')) {
|
||||
set_flash('error', 'Administratoren kann nur löschen, wer die Berechtigung users.admin hat.');
|
||||
redirect('/admin/users.php');
|
||||
}
|
||||
|
||||
$stmt = db()->prepare('DELETE FROM users WHERE id = ?');
|
||||
$stmt->execute([$id]);
|
||||
|
||||
|
|
|
|||
|
|
@ -19,14 +19,19 @@ $user = [
|
|||
'status' => 'active',
|
||||
'must_change_password' => 1,
|
||||
'password_login' => 1,
|
||||
'is_admin' => 0,
|
||||
'discord_id' => null,
|
||||
'discord_username' => null,
|
||||
];
|
||||
$selectedRoleIds = [];
|
||||
|
||||
// Administrator-Rechte (users.is_admin = alle Berechtigungen) vergibt nur, wer users.admin hat.
|
||||
$canGrantAdmin = user_has_permission('users.admin');
|
||||
$isSelf = $isEdit && $editId === (int) current_user()['id'];
|
||||
|
||||
if ($isEdit) {
|
||||
$stmt = $pdo->prepare(
|
||||
'SELECT id, username, display_name, status, must_change_password, password_login, discord_id, discord_username
|
||||
'SELECT id, username, display_name, status, must_change_password, password_login, is_admin, discord_id, discord_username
|
||||
FROM users WHERE id = ?'
|
||||
);
|
||||
$stmt->execute([$editId]);
|
||||
|
|
@ -35,6 +40,12 @@ if ($isEdit) {
|
|||
set_flash('error', 'Benutzer wurde nicht gefunden.');
|
||||
redirect('/admin/users.php');
|
||||
}
|
||||
// Ein Administrator hat alle Rechte - bearbeiten (z.B. sperren oder das Passwort setzen)
|
||||
// darf ihn deshalb nur, wer selbst Administratoren verwalten darf.
|
||||
if (!empty($found['is_admin']) && !$canGrantAdmin) {
|
||||
set_flash('error', 'Administratoren kann nur bearbeiten, wer die Berechtigung users.admin hat.');
|
||||
redirect('/admin/users.php');
|
||||
}
|
||||
$user = $found;
|
||||
|
||||
$roleStmt = $pdo->prepare('SELECT role_id FROM user_roles WHERE user_id = ?');
|
||||
|
|
@ -70,6 +81,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||
$passwordLogin = isset($_POST['password_login']) ? 1 : 0;
|
||||
$submittedRoleIds = array_map('intval', $_POST['roles'] ?? []);
|
||||
|
||||
// Administrator-Haken: nur mit users.admin aenderbar, und die eigenen Admin-Rechte kann
|
||||
// man sich nicht selbst entziehen (sonst sperrt man sich womoeglich aus).
|
||||
$isAdmin = (int) !empty($user['is_admin']);
|
||||
if ($canGrantAdmin && !($isSelf && $isAdmin)) {
|
||||
$isAdmin = isset($_POST['is_admin']) ? 1 : 0;
|
||||
}
|
||||
|
||||
// Discord-gesteuerte und nicht vergebbare Gruppen behalten ihren Stand, egal was im Formular steht.
|
||||
$submittedRoleIds = array_values(array_unique(array_merge(
|
||||
array_diff($submittedRoleIds, $lockedRoleIds),
|
||||
|
|
@ -94,19 +112,19 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||
|
||||
if ($isEdit) {
|
||||
if ($password !== '') {
|
||||
$stmt = $pdo->prepare('UPDATE users SET username=?, display_name=?, status=?, password_hash=?, must_change_password=?, password_login=? WHERE id=?');
|
||||
$stmt->execute([$username, $displayName, $status, password_hash($password, PASSWORD_DEFAULT), $forceChange, $passwordLogin, $editId]);
|
||||
$stmt = $pdo->prepare('UPDATE users SET username=?, display_name=?, status=?, password_hash=?, must_change_password=?, password_login=?, is_admin=? WHERE id=?');
|
||||
$stmt->execute([$username, $displayName, $status, password_hash($password, PASSWORD_DEFAULT), $forceChange, $passwordLogin, $isAdmin, $editId]);
|
||||
} else {
|
||||
$stmt = $pdo->prepare('UPDATE users SET username=?, display_name=?, status=?, must_change_password=?, password_login=? WHERE id=?');
|
||||
$stmt->execute([$username, $displayName, $status, $forceChange, $passwordLogin, $editId]);
|
||||
$stmt = $pdo->prepare('UPDATE users SET username=?, display_name=?, status=?, must_change_password=?, password_login=?, is_admin=? WHERE id=?');
|
||||
$stmt->execute([$username, $displayName, $status, $forceChange, $passwordLogin, $isAdmin, $editId]);
|
||||
}
|
||||
$userId = $editId;
|
||||
} else {
|
||||
// Neue Benutzer bekommen immer ein zufälliges Einmalpasswort
|
||||
// und müssen es beim ersten Login ändern.
|
||||
$password = generate_temp_password();
|
||||
$stmt = $pdo->prepare('INSERT INTO users (username, display_name, status, password_hash, must_change_password, password_login) VALUES (?, ?, ?, ?, 1, 1)');
|
||||
$stmt->execute([$username, $displayName, $status, password_hash($password, PASSWORD_DEFAULT)]);
|
||||
$stmt = $pdo->prepare('INSERT INTO users (username, display_name, status, password_hash, must_change_password, password_login, is_admin) VALUES (?, ?, ?, ?, 1, 1, ?)');
|
||||
$stmt->execute([$username, $displayName, $status, password_hash($password, PASSWORD_DEFAULT), $isAdmin]);
|
||||
$userId = (int) $pdo->lastInsertId();
|
||||
}
|
||||
|
||||
|
|
@ -138,6 +156,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||
$user['status'] = $status;
|
||||
$user['must_change_password'] = $forceChange;
|
||||
$user['password_login'] = $passwordLogin;
|
||||
$user['is_admin'] = $isAdmin;
|
||||
$selectedRoleIds = $submittedRoleIds;
|
||||
}
|
||||
|
||||
|
|
@ -205,6 +224,25 @@ include __DIR__ . '/../includes/header.php';
|
|||
<option value="disabled" <?= $user['status'] === 'disabled' ? 'selected' : '' ?>>Deaktiviert</option>
|
||||
</select>
|
||||
</div>
|
||||
<?php if ($canGrantAdmin): ?>
|
||||
<div class="admin-toggle">
|
||||
<label>
|
||||
<input type="checkbox" name="is_admin" value="1"
|
||||
<?= !empty($user['is_admin']) ? 'checked' : '' ?>
|
||||
<?= $isSelf && !empty($user['is_admin']) ? 'disabled' : '' ?>>
|
||||
<?= icon('crown') ?> Administrator - hat alle Berechtigungen
|
||||
</label>
|
||||
<div class="field-hint">
|
||||
Unabhängig von den Berechtigungsgruppen darf ein Administrator alles, auch
|
||||
Funktionen, die erst später dazukommen. Nur sparsam vergeben.
|
||||
<?php if ($isSelf && !empty($user['is_admin'])): ?>
|
||||
Deine eigenen Administrator-Rechte kannst du dir nicht selbst entziehen.
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php elseif (!empty($user['is_admin'])): ?>
|
||||
<p class="field-hint"><?= icon('crown') ?> Dieser Benutzer ist Administrator.</p>
|
||||
<?php endif; ?>
|
||||
<div>
|
||||
<label>Berechtigungsgruppen</label>
|
||||
<div class="checkbox-grid">
|
||||
|
|
|
|||
|
|
@ -5,12 +5,13 @@ require_permission('users.view');
|
|||
$canCreate = user_has_permission('users.create');
|
||||
$canEdit = user_has_permission('users.edit');
|
||||
$canDelete = user_has_permission('users.delete');
|
||||
$canManageAdmins = user_has_permission('users.admin');
|
||||
$currentUser = current_user();
|
||||
|
||||
// Wer auf die Freischaltung wartet, steht oben - das ist die Liste, die man abarbeitet.
|
||||
$users = db()->query(
|
||||
"SELECT u.id, u.username, u.display_name, u.status, u.last_login, u.created_at, u.must_change_password,
|
||||
u.password_login, u.discord_id, u.discord_username,
|
||||
u.password_login, u.is_admin, u.discord_id, u.discord_username,
|
||||
GROUP_CONCAT(COALESCE(NULLIF(r.display_name, ''), r.name) ORDER BY r.name SEPARATOR ', ') AS role_names
|
||||
FROM users u
|
||||
LEFT JOIN user_roles ur ON ur.user_id = u.id
|
||||
|
|
@ -75,7 +76,11 @@ include __DIR__ . '/../includes/header.php';
|
|||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($users as $u): ?>
|
||||
<?php [$statusLabel, $statusClass] = $statusLabels[$u['status']] ?? [$u['status'], '']; ?>
|
||||
<?php
|
||||
[$statusLabel, $statusClass] = $statusLabels[$u['status']] ?? [$u['status'], ''];
|
||||
// Administratoren bearbeiten/loeschen nur mit users.admin.
|
||||
$protectedAdmin = !empty($u['is_admin']) && !$canManageAdmins;
|
||||
?>
|
||||
<tr<?= $u['status'] === 'pending' ? ' class="row-pending"' : '' ?>>
|
||||
<td><?= e($u['username']) ?></td>
|
||||
<td><?= e($u['display_name']) ?></td>
|
||||
|
|
@ -90,6 +95,9 @@ include __DIR__ . '/../includes/header.php';
|
|||
<td>
|
||||
<div class="badge-group">
|
||||
<span class="badge <?= e($statusClass) ?>"><?= e($statusLabel) ?></span>
|
||||
<?php if (!empty($u['is_admin'])): ?>
|
||||
<span class="badge badge-admin" title="Hat alle Berechtigungen"><?= icon('crown') ?> Administrator</span>
|
||||
<?php endif; ?>
|
||||
<?php if (empty($u['password_login'])): ?>
|
||||
<span class="badge" title="Anmeldung nur über Discord möglich">Nur Discord</span>
|
||||
<?php elseif ($u['must_change_password']): ?>
|
||||
|
|
@ -108,10 +116,10 @@ include __DIR__ . '/../includes/header.php';
|
|||
<button type="submit" class="button-success button-small">Freischalten</button>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
<?php if ($canEdit): ?>
|
||||
<?php if ($canEdit && !$protectedAdmin): ?>
|
||||
<a class="icon-button" href="/admin/user_form.php?id=<?= (int) $u['id'] ?>" title="Bearbeiten" aria-label="Bearbeiten"><?= icon('edit') ?></a>
|
||||
<?php endif; ?>
|
||||
<?php if ($canDelete && (int) $u['id'] !== (int) $currentUser['id']): ?>
|
||||
<?php if ($canDelete && !$protectedAdmin && (int) $u['id'] !== (int) $currentUser['id']): ?>
|
||||
<form class="inline-form" method="post" action="/admin/user_delete.php" onsubmit="return confirm('Diesen Benutzer wirklich löschen?');">
|
||||
<?= csrf_field() ?>
|
||||
<input type="hidden" name="id" value="<?= (int) $u['id'] ?>">
|
||||
|
|
|
|||
|
|
@ -3899,4 +3899,32 @@ body.home-page .navbar {
|
|||
.home-photo-section { padding-top: 3rem; }
|
||||
.home-photo { border-radius: 18px; }
|
||||
.home-photo figcaption { left: 0.9rem; right: 0.9rem; bottom: 0.8rem; font-size: 0.85rem; }
|
||||
}
|
||||
}
|
||||
/* --- Administrator-Rechte (users.is_admin) --- */
|
||||
.badge-admin {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 0.25rem;
|
||||
border-color: var(--gold);
|
||||
color: var(--gold);
|
||||
background: rgba(201, 162, 39, 0.12);
|
||||
}
|
||||
|
||||
.badge-admin .icon { width: 13px; height: 13px; }
|
||||
|
||||
.admin-toggle {
|
||||
padding: 0.85rem 1rem;
|
||||
border: 1px solid rgba(201, 162, 39, 0.4);
|
||||
border-radius: 8px;
|
||||
background: rgba(201, 162, 39, 0.07);
|
||||
}
|
||||
|
||||
.admin-toggle label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
color: var(--gold);
|
||||
}
|
||||
|
||||
.admin-toggle .icon { width: 18px; height: 18px; }
|
||||
.field-hint .icon { width: 14px; height: 14px; vertical-align: -2px; }
|
||||
|
|
|
|||
11
bot_api.php
11
bot_api.php
|
|
@ -50,10 +50,13 @@ if (!empty($discordIds)) {
|
|||
$placeholders = implode(',', array_fill(0, count($discordIds), '?'));
|
||||
$stmt = db()->prepare(
|
||||
"SELECT DISTINCT u.discord_id FROM users u
|
||||
JOIN user_roles ur ON ur.user_id = u.id
|
||||
JOIN role_permissions rp ON rp.role_id = ur.role_id
|
||||
JOIN permissions p ON p.id = rp.permission_id
|
||||
WHERE u.status = 'active' AND p.`key` = 'raids.manage' AND u.discord_id IN ($placeholders)"
|
||||
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'
|
||||
))"
|
||||
);
|
||||
$stmt->execute($discordIds);
|
||||
$recipients = array_map('strval', $stmt->fetchAll(PDO::FETCH_COLUMN));
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ function current_user(): ?array
|
|||
|
||||
$stmt = db()->prepare(
|
||||
'SELECT id, username, display_name, status, must_change_password, password_login, discord_id, discord_username,
|
||||
avatar_filename, tour_completed_at
|
||||
avatar_filename, tour_completed_at, is_admin
|
||||
FROM users WHERE id = ?'
|
||||
);
|
||||
$stmt->execute([$_SESSION['user_id']]);
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ const CORE_PERMISSIONS = [
|
|||
['key' => 'users.create', 'description' => 'Neue Benutzer anlegen'],
|
||||
['key' => 'users.edit', 'description' => 'Benutzer bearbeiten'],
|
||||
['key' => 'users.delete', 'description' => 'Benutzer löschen'],
|
||||
['key' => 'users.admin', 'description' => 'Einzelnen Benutzern Administrator-Rechte (alle Berechtigungen) geben oder entziehen und Administratoren bearbeiten'],
|
||||
['key' => 'roles.manage', 'description' => 'Berechtigungsgruppen verwalten'],
|
||||
['key' => 'roles.assign', 'description' => 'Alle Berechtigungsgruppen an Benutzer vergeben und entziehen (einzelne Gruppen: roles.assign.<ID>)'],
|
||||
['key' => 'raids.view', 'description' => 'Raids einsehen'],
|
||||
|
|
@ -43,6 +44,12 @@ function user_has_permission(string $key): bool
|
|||
return false;
|
||||
}
|
||||
|
||||
// Administratoren (users.is_admin) haben alle Berechtigungen - auch solche, die es
|
||||
// spaeter erst gibt - unabhaengig von ihren Gruppen.
|
||||
if (!empty($user['is_admin'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
static $cache = [];
|
||||
if (array_key_exists($key, $cache)) {
|
||||
return $cache[$key];
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ declare(strict_types=1);
|
|||
*/
|
||||
|
||||
// Hochzaehlen, sobald unten eine Aenderung dazukommt - dann pruefen alle Sitzungen neu.
|
||||
const SCHEMA_VERSION = 13;
|
||||
const SCHEMA_VERSION = 14;
|
||||
|
||||
/**
|
||||
* Tabellen, die nachtraeglich dazugekommen sind: Tabelle => CREATE-Befehl (wie in sql/schema.sql).
|
||||
|
|
@ -226,6 +226,7 @@ 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",
|
||||
'is_admin' => "ALTER TABLE users ADD COLUMN is_admin TINYINT(1) NOT NULL DEFAULT 0 AFTER password_login",
|
||||
'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",
|
||||
|
|
|
|||
|
|
@ -87,7 +87,8 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|||
|
||||
$seeded = seed_defaults($pdo);
|
||||
|
||||
$stmt = $pdo->prepare('INSERT INTO users (username, password_hash, display_name) VALUES (?, ?, ?)');
|
||||
// Das erste Konto ist zusaetzlich Administrator (alle Rechte, auch ohne Gruppe).
|
||||
$stmt = $pdo->prepare('INSERT INTO users (username, password_hash, display_name, is_admin) VALUES (?, ?, ?, 1)');
|
||||
$stmt->execute([$username, password_hash($password, PASSWORD_DEFAULT), $displayName]);
|
||||
$newUserId = (int) $pdo->lastInsertId();
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@ CREATE TABLE IF NOT EXISTS users (
|
|||
must_change_password TINYINT(1) NOT NULL DEFAULT 0,
|
||||
-- 0 = Anmeldung nur ueber Discord (Standard fuer per Discord angelegte Konten).
|
||||
password_login TINYINT(1) NOT NULL DEFAULT 1,
|
||||
-- 1 = Administrator: hat alle Berechtigungen, unabhaengig von den Gruppen
|
||||
is_admin TINYINT(1) NOT NULL DEFAULT 0,
|
||||
discord_id BIGINT UNSIGNED NULL UNIQUE,
|
||||
discord_username VARCHAR(100) NULL,
|
||||
-- OAuth-Token des Discord-Logins (Scopes identify + guilds.members.read), damit
|
||||
|
|
|
|||
Loading…
Reference in a new issue