diff --git a/public/index.php b/public/index.php index 97d84a0..9a174e2 100644 --- a/public/index.php +++ b/public/index.php @@ -107,6 +107,7 @@ $router->post('/logout', fn($p) => (new LoginController())->handleLogout($p)); // ── Benutzerverwaltung ──────────────────────────────────────── $router->get('/useradmin', fn($p) => (new UserAdminController())->index($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}/toggle', fn($p) => (new UserAdminController())->toggleActive($p)); diff --git a/src/Controllers/UserAdminController.php b/src/Controllers/UserAdminController.php index 3ac80bd..a44072d 100644 --- a/src/Controllers/UserAdminController.php +++ b/src/Controllers/UserAdminController.php @@ -57,6 +57,50 @@ class UserAdminController 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 { $id = (int)($params['id'] ?? 0); diff --git a/src/Models/User.php b/src/Models/User.php index 0e45d41..efad6b0 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -45,6 +45,22 @@ class User 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 { Database::execute( diff --git a/src/Views/useradmin/list.php b/src/Views/useradmin/list.php index 4e1a61c..019e95a 100644 --- a/src/Views/useradmin/list.php +++ b/src/Views/useradmin/list.php @@ -1,13 +1,16 @@ - View::h($v); ?> + View::h($v); +$editId = $formData['edit_id'] ?? null; +?>