Benutzer bearbeiten: Bearbeiten-Button + Update-Route
This commit is contained in:
parent
a338631dbd
commit
628e636454
4 changed files with 109 additions and 9 deletions
|
|
@ -107,6 +107,7 @@ $router->post('/logout', fn($p) => (new LoginController())->handleLogout($p));
|
||||||
// ── Benutzerverwaltung ────────────────────────────────────────
|
// ── Benutzerverwaltung ────────────────────────────────────────
|
||||||
$router->get('/useradmin', fn($p) => (new UserAdminController())->index($p));
|
$router->get('/useradmin', fn($p) => (new UserAdminController())->index($p));
|
||||||
$router->post('/useradmin', fn($p) => (new UserAdminController())->store($p));
|
$router->post('/useradmin', fn($p) => (new UserAdminController())->store($p));
|
||||||
|
$router->post('/useradmin/{id}', fn($p) => (new UserAdminController())->update($p));
|
||||||
$router->post('/useradmin/{id}/delete', fn($p) => (new UserAdminController())->delete($p));
|
$router->post('/useradmin/{id}/delete', fn($p) => (new UserAdminController())->delete($p));
|
||||||
$router->post('/useradmin/{id}/toggle', fn($p) => (new UserAdminController())->toggleActive($p));
|
$router->post('/useradmin/{id}/toggle', fn($p) => (new UserAdminController())->toggleActive($p));
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,50 @@ class UserAdminController
|
||||||
View::redirect('/useradmin', 'Benutzer angelegt.');
|
View::redirect('/useradmin', 'Benutzer angelegt.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function update(array $params): void
|
||||||
|
{
|
||||||
|
$id = (int)($params['id'] ?? 0);
|
||||||
|
$username = trim($_POST['username'] ?? '');
|
||||||
|
$password = $_POST['password'] ?? '';
|
||||||
|
$errors = [];
|
||||||
|
|
||||||
|
if (!User::find($id)) {
|
||||||
|
View::redirect('/useradmin', 'Benutzer nicht gefunden.', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($username === '') {
|
||||||
|
$errors['username'] = 'Pflichtfeld';
|
||||||
|
} elseif (strlen($username) > 64) {
|
||||||
|
$errors['username'] = 'Maximal 64 Zeichen';
|
||||||
|
} else {
|
||||||
|
$existing = User::findByUsername($username);
|
||||||
|
if ($existing && (int)$existing['id'] !== $id) {
|
||||||
|
$errors['username'] = 'Benutzername bereits vergeben';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($password !== '' && strlen($password) < 8) {
|
||||||
|
$errors['password'] = 'Mindestens 8 Zeichen erforderlich';
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($errors) {
|
||||||
|
$view = new View();
|
||||||
|
$view->render('layout/base.php', [
|
||||||
|
'pageTitle' => 'Benutzerverwaltung',
|
||||||
|
'content' => $this->renderView('useradmin/list', [
|
||||||
|
'users' => User::all(),
|
||||||
|
'errors' => $errors,
|
||||||
|
'formData' => ['username' => $username, 'edit_id' => $id],
|
||||||
|
]),
|
||||||
|
]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
User::update($id, $username, $password);
|
||||||
|
View::redirect('/useradmin', 'Benutzer gespeichert.');
|
||||||
|
}
|
||||||
|
|
||||||
public function delete(array $params): void
|
public function delete(array $params): void
|
||||||
{
|
{
|
||||||
$id = (int)($params['id'] ?? 0);
|
$id = (int)($params['id'] ?? 0);
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,22 @@ class User
|
||||||
Database::execute('DELETE FROM users WHERE id = ?', [$id]);
|
Database::execute('DELETE FROM users WHERE id = ?', [$id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function update(int $id, string $username, string $password = ''): void
|
||||||
|
{
|
||||||
|
if ($password !== '') {
|
||||||
|
$hash = password_hash($password, PASSWORD_BCRYPT);
|
||||||
|
Database::execute(
|
||||||
|
'UPDATE users SET username = ?, password_hash = ? WHERE id = ?',
|
||||||
|
[$username, $hash, $id]
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
Database::execute(
|
||||||
|
'UPDATE users SET username = ? WHERE id = ?',
|
||||||
|
[$username, $id]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static function setActive(int $id, bool $active): void
|
public static function setActive(int $id, bool $active): void
|
||||||
{
|
{
|
||||||
Database::execute(
|
Database::execute(
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,16 @@
|
||||||
<?php use Steuerkram\View; $h = fn($v) => View::h($v); ?>
|
<?php use Steuerkram\View; $h = fn($v) => View::h($v);
|
||||||
|
$editId = $formData['edit_id'] ?? null;
|
||||||
|
?>
|
||||||
|
|
||||||
<div class="card" style="margin-bottom:1.5rem;">
|
<div class="card" style="margin-bottom:1.5rem;">
|
||||||
<div class="card-header"><div class="card-title">Neuen Benutzer anlegen</div></div>
|
<div class="card-header"><div class="card-title" id="form-title"><?= $editId ? 'Benutzer bearbeiten' : 'Neuen Benutzer anlegen' ?></div></div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<form method="POST" action="/useradmin">
|
<form method="POST" id="user-form"
|
||||||
|
action="<?= $editId ? '/useradmin/' . (int)$editId : '/useradmin' ?>">
|
||||||
<div class="form-row">
|
<div class="form-row">
|
||||||
<div class="form-group" style="flex:1">
|
<div class="form-group" style="flex:1">
|
||||||
<label>Benutzername</label>
|
<label>Benutzername</label>
|
||||||
<input type="text" name="username"
|
<input type="text" id="field-username" name="username"
|
||||||
value="<?= $h($formData['username'] ?? '') ?>"
|
value="<?= $h($formData['username'] ?? '') ?>"
|
||||||
class="<?= isset($errors['username']) ? 'input-error' : '' ?>"
|
class="<?= isset($errors['username']) ? 'input-error' : '' ?>"
|
||||||
maxlength="64" required>
|
maxlength="64" required>
|
||||||
|
|
@ -16,16 +19,22 @@
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group" style="flex:1">
|
<div class="form-group" style="flex:1">
|
||||||
<label>Passwort</label>
|
<label id="password-label"><?= $editId ? 'Neues Passwort (leer lassen = unverändert)' : 'Passwort' ?></label>
|
||||||
<input type="password" name="password"
|
<input type="password" id="field-password" name="password"
|
||||||
class="<?= isset($errors['password']) ? 'input-error' : '' ?>"
|
class="<?= isset($errors['password']) ? 'input-error' : '' ?>"
|
||||||
autocomplete="new-password" required>
|
autocomplete="new-password"
|
||||||
|
<?= $editId ? '' : 'required' ?>>
|
||||||
<?php if (isset($errors['password'])): ?>
|
<?php if (isset($errors['password'])): ?>
|
||||||
<div class="error-text"><?= $h($errors['password']) ?></div>
|
<div class="error-text"><?= $h($errors['password']) ?></div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
<div style="padding-bottom:.05rem">
|
<div style="display:flex;gap:.5rem;align-items:flex-end;padding-bottom:.05rem">
|
||||||
<button type="submit" class="btn btn-primary">Anlegen</button>
|
<button type="submit" id="submit-btn" class="btn btn-primary">
|
||||||
|
<?= $editId ? 'Speichern' : 'Anlegen' ?>
|
||||||
|
</button>
|
||||||
|
<button type="button" id="cancel-btn" class="btn btn-secondary"
|
||||||
|
style="<?= $editId ? '' : 'display:none' ?>"
|
||||||
|
onclick="resetForm()">Abbrechen</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
@ -59,6 +68,10 @@
|
||||||
</td>
|
</td>
|
||||||
<td><?= $h(View::formatDate(substr($u['created_at'], 0, 10))) ?></td>
|
<td><?= $h(View::formatDate(substr($u['created_at'], 0, 10))) ?></td>
|
||||||
<td style="text-align:right">
|
<td style="text-align:right">
|
||||||
|
<button type="button" class="btn btn-sm btn-secondary"
|
||||||
|
onclick="editUser(<?= (int)$u['id'] ?>, <?= $h(json_encode($u['username'])) ?>)">
|
||||||
|
Bearbeiten
|
||||||
|
</button>
|
||||||
<form method="POST" action="/useradmin/<?= (int)$u['id'] ?>/toggle" style="display:inline;">
|
<form method="POST" action="/useradmin/<?= (int)$u['id'] ?>/toggle" style="display:inline;">
|
||||||
<button type="submit" class="btn btn-sm <?= $u['active'] ? 'btn-secondary' : 'btn-primary' ?>">
|
<button type="submit" class="btn btn-sm <?= $u['active'] ? 'btn-secondary' : 'btn-primary' ?>">
|
||||||
<?= $u['active'] ? 'Deaktivieren' : 'Aktivieren' ?>
|
<?= $u['active'] ? 'Deaktivieren' : 'Aktivieren' ?>
|
||||||
|
|
@ -75,3 +88,29 @@
|
||||||
</table>
|
</table>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function editUser(id, username) {
|
||||||
|
document.getElementById('field-username').value = username;
|
||||||
|
document.getElementById('field-password').value = '';
|
||||||
|
document.getElementById('field-password').removeAttribute('required');
|
||||||
|
document.getElementById('user-form').action = '/useradmin/' + id;
|
||||||
|
document.getElementById('form-title').textContent = 'Benutzer bearbeiten';
|
||||||
|
document.getElementById('password-label').textContent = 'Neues Passwort (leer lassen = unverändert)';
|
||||||
|
document.getElementById('submit-btn').textContent = 'Speichern';
|
||||||
|
document.getElementById('cancel-btn').style.display = '';
|
||||||
|
document.getElementById('field-username').focus();
|
||||||
|
document.querySelector('.card').scrollIntoView({behavior: 'smooth'});
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetForm() {
|
||||||
|
document.getElementById('field-username').value = '';
|
||||||
|
document.getElementById('field-password').value = '';
|
||||||
|
document.getElementById('field-password').setAttribute('required', '');
|
||||||
|
document.getElementById('user-form').action = '/useradmin';
|
||||||
|
document.getElementById('form-title').textContent = 'Neuen Benutzer anlegen';
|
||||||
|
document.getElementById('password-label').textContent = 'Passwort';
|
||||||
|
document.getElementById('submit-btn').textContent = 'Anlegen';
|
||||||
|
document.getElementById('cancel-btn').style.display = 'none';
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue