Simplifies Plesk/shared-hosting deployment (no need to set a separate document root subfolder). config.php, includes/ and sql/ are now protected via .htaccess rules instead of living outside the webroot. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
149 lines
6.3 KiB
PHP
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. fuer spaetere 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-Schluessel 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-Schluessel 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 fuer 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">Fuer zukuenftige Funktionen kannst du hier weitere Berechtigungs-Schluessel 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">Schluessel</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'; ?>
|