LuminaWebsite/admin/role_form.php
Maaxxs 77bb1b3ff5 Use real German umlauts instead of ASCII transliterations
Every "oe/ae/ue" spelling throughout the UI text and comments (fuer,
loeschen, koennen, ...) was my own typo, not an encoding bug - the
charset fix in the last commit was unrelated. Replaced all of them
with proper ö/ä/ü across every PHP file and the README.

Also fix table rows rendering inconsistently: `.actions` and the
status badges were applying `display:flex` directly to a <td>, which
fights the table layout model and renders unreliably across browsers.
Moved the flex layout into a wrapper <div> inside each cell instead,
and restyled row-level delete buttons as plain text links so they
match the height of the "Bearbeiten" link exactly. Tables also get a
horizontal-scroll wrapper so narrow viewports scroll instead of
squeezing cell content.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-18 07:16:15 +02:00

149 lines
6.3 KiB
PHP

<?php
require_once __DIR__ . '/../includes/bootstrap.php';
require_permission('roles.manage');
$pdo = db();
$editId = isset($_GET['id']) ? (int) $_GET['id'] : null;
$isEdit = $editId !== null;
$role = ['id' => null, 'name' => '', 'description' => '', 'is_system' => 0];
$selectedPermissionIds = [];
if ($isEdit) {
$stmt = $pdo->prepare('SELECT id, name, description, is_system FROM roles WHERE id = ?');
$stmt->execute([$editId]);
$found = $stmt->fetch();
if (!$found) {
set_flash('error', 'Berechtigungsgruppe wurde nicht gefunden.');
redirect('/admin/roles.php');
}
$role = $found;
$permStmt = $pdo->prepare('SELECT permission_id FROM role_permissions WHERE role_id = ?');
$permStmt->execute([$editId]);
$selectedPermissionIds = array_map('intval', array_column($permStmt->fetchAll(), 'permission_id'));
}
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
verify_csrf();
if (isset($_POST['add_permission'])) {
// Neue eigene Berechtigung anlegen (z.B. für spätere Funktionen).
$newKey = trim((string) ($_POST['new_permission_key'] ?? ''));
$newDescription = trim((string) ($_POST['new_permission_description'] ?? ''));
if ($newKey === '' || !preg_match('/^[a-z0-9_.\-]+$/', $newKey)) {
$error = 'Der Berechtigungs-Schlüssel darf nur Kleinbuchstaben, Zahlen, Punkte, "_" und "-" enthalten.';
} else {
try {
$pdo->prepare('INSERT INTO permissions (`key`, description) VALUES (?, ?)')
->execute([$newKey, $newDescription]);
set_flash('success', 'Neue Berechtigung wurde angelegt.');
} catch (PDOException $e) {
$error = 'Dieser Berechtigungs-Schlüssel existiert bereits.';
}
}
} else {
$name = trim((string) ($_POST['name'] ?? ''));
$description = trim((string) ($_POST['description'] ?? ''));
$submittedPermissionIds = array_map('intval', $_POST['permissions'] ?? []);
if ($name === '') {
$error = 'Bitte einen Namen für die Gruppe angeben.';
} else {
try {
$pdo->beginTransaction();
if ($isEdit) {
$pdo->prepare('UPDATE roles SET name = ?, description = ? WHERE id = ?')
->execute([$name, $description, $editId]);
$roleId = $editId;
} else {
$pdo->prepare('INSERT INTO roles (name, description) VALUES (?, ?)')
->execute([$name, $description]);
$roleId = (int) $pdo->lastInsertId();
}
$pdo->prepare('DELETE FROM role_permissions WHERE role_id = ?')->execute([$roleId]);
if (!empty($submittedPermissionIds)) {
$insert = $pdo->prepare('INSERT INTO role_permissions (role_id, permission_id) VALUES (?, ?)');
foreach ($submittedPermissionIds as $permId) {
$insert->execute([$roleId, $permId]);
}
}
$pdo->commit();
set_flash('success', $isEdit ? 'Berechtigungsgruppe wurde aktualisiert.' : 'Berechtigungsgruppe wurde angelegt.');
redirect('/admin/roles.php');
} catch (PDOException $e) {
$pdo->rollBack();
$error = 'Eine Gruppe mit diesem Namen existiert bereits.';
}
}
$role['name'] = $name;
$role['description'] = $description;
$selectedPermissionIds = $submittedPermissionIds;
}
}
$allPermissions = $pdo->query('SELECT id, `key`, description FROM permissions ORDER BY `key`')->fetchAll();
$pageTitle = $isEdit ? 'Berechtigungsgruppe bearbeiten' : 'Berechtigungsgruppe anlegen';
include __DIR__ . '/../includes/header.php';
?>
<div class="panel">
<h1><?= $isEdit ? 'Berechtigungsgruppe bearbeiten' : 'Neue Berechtigungsgruppe' ?></h1>
<?php if ($error): ?>
<div class="flash flash-error"><?= e($error) ?></div>
<?php endif; ?>
<form method="post" action="/admin/role_form.php<?= $isEdit ? '?id=' . (int) $editId : '' ?>">
<?= csrf_field() ?>
<div>
<label for="name">Name</label>
<input type="text" id="name" name="name" required value="<?= e($role['name']) ?>">
</div>
<div>
<label for="description">Beschreibung</label>
<input type="text" id="description" name="description" value="<?= e($role['description']) ?>">
</div>
<div>
<label>Berechtigungen</label>
<div class="checkbox-grid">
<?php foreach ($allPermissions as $perm): ?>
<label title="<?= e($perm['description']) ?>">
<input type="checkbox" name="permissions[]" value="<?= (int) $perm['id'] ?>"
<?= in_array((int) $perm['id'], $selectedPermissionIds, true) ? 'checked' : '' ?>>
<?= e($perm['key']) ?>
</label>
<?php endforeach; ?>
</div>
</div>
<div class="actions">
<button type="submit"><?= $isEdit ? 'Speichern' : 'Anlegen' ?></button>
<a class="button button-secondary" href="/admin/roles.php">Abbrechen</a>
</div>
</form>
</div>
<div class="panel">
<h2>Neue Berechtigung anlegen</h2>
<p class="text-muted">Für zukünftige Funktionen kannst du hier weitere Berechtigungs-Schlüssel anlegen (z.B. "events.manage").</p>
<form method="post" action="/admin/role_form.php<?= $isEdit ? '?id=' . (int) $editId : '' ?>">
<?= csrf_field() ?>
<input type="hidden" name="add_permission" value="1">
<div>
<label for="new_permission_key">Schlüssel</label>
<input type="text" id="new_permission_key" name="new_permission_key" placeholder="z.B. events.manage" required>
</div>
<div>
<label for="new_permission_description">Beschreibung</label>
<input type="text" id="new_permission_description" name="new_permission_description" placeholder="z.B. Events verwalten">
</div>
<button type="submit" class="button-secondary">Berechtigung anlegen</button>
</form>
</div>
<?php include __DIR__ . '/../includes/footer.php'; ?>