diff --git a/public/index.php b/public/index.php index e5d078d..7ee1f24 100644 --- a/public/index.php +++ b/public/index.php @@ -53,6 +53,69 @@ if ($page === 'login') { Auth::requireLogin(); Auth::requireAdmin(); require __DIR__ . '/../src/Views/admin.php'; +} elseif ($page === 'admin_users') { + Auth::requireLogin(); + Auth::requireAdmin(); + + $message = null; + $error = null; + + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $action = $_POST['action'] ?? ''; + $userId = $_POST['id'] ?? null; + $redirect = '/index.php?page=admin_users'; + + try { + if ($action === 'create' || $action === 'update') { + $data = [ + 'firstname' => $_POST['firstname'], + 'lastname' => $_POST['lastname'], + 'email' => $_POST['email'], + 'password' => $_POST['password'], + 'admin' => isset($_POST['admin']) ? 1 : 0, + ]; + + if ($action === 'create') { + if (empty($data['password'])) { + throw new \Exception('Passwort ist erforderlich.'); + } + User::create($data); + $_SESSION['admin_message'] = "Benutzer erfolgreich erstellt."; + } elseif ($userId) { + User::update($userId, $data); + $_SESSION['admin_message'] = "Benutzer erfolgreich aktualisiert."; + } + } elseif ($action === 'delete' && $userId) { + User::delete($userId); + $_SESSION['admin_message'] = "Benutzer erfolgreich gelöscht."; + } + } catch (\Throwable $e) { + $_SESSION['admin_error'] = "Fehler: " . $e->getMessage(); + if ($action === 'update' && $userId) { + $redirect = '/index.php?page=admin_users&edit_id=' . $userId; + } + } + + header('Location: ' . $redirect); + exit; + } + + if (isset($_SESSION['admin_message'])) { + $message = $_SESSION['admin_message']; + unset($_SESSION['admin_message']); + } + if (isset($_SESSION['admin_error'])) { + $error = $_SESSION['admin_error']; + unset($_SESSION['admin_error']); + } + + $users = User::getAll(); + $editUser = null; + if (isset($_GET['edit_id'])) { + $editUser = User::findById((int)$_GET['edit_id']); + } + + require __DIR__ . '/../src/Views/admin/users.php'; } elseif ($page === 'round' && isset($_GET['id'])) { Auth::requireLogin(); try { diff --git a/src/Models/User.php b/src/Models/User.php index 89c8044..9ac6af1 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -27,7 +27,7 @@ class User { */ public static function findByFirstAndLastName($firstname, $lastname) { $db = Database::getConnection(); - $stmt = $db->prepare("SELECT * FROM users WHERE firstname = ? AND lastname = ?"); + $stmt = $db->prepare("SELECT * FROM users WHERE firstname = ? AND lastname = ? AND deleted IS NULL"); $stmt->execute([$firstname, $lastname]); return $stmt->fetch(\PDO::FETCH_ASSOC); } @@ -40,8 +40,88 @@ class User { */ public static function findById($id) { $db = Database::getConnection(); - $stmt = $db->prepare("SELECT * FROM users WHERE id = ?"); + $stmt = $db->prepare("SELECT * FROM users WHERE id = ? AND deleted IS NULL"); $stmt->execute([$id]); return $stmt->fetch(\PDO::FETCH_ASSOC); } + + /** + * Holt alle (nicht gelöschten) Benutzer. + * + * @return array + */ + public static function getAll() { + $db = Database::getConnection(); + $stmt = $db->prepare("SELECT * FROM users WHERE deleted IS NULL ORDER BY name ASC"); + $stmt->execute(); + return $stmt->fetchAll(\PDO::FETCH_ASSOC); + } + + /** + * Erstellt einen neuen Benutzer. + * + * @param array $data + * @return bool + */ + public static function create($data) { + $db = Database::getConnection(); + $data['name'] = ($data['firstname'] ?? '') . ' ' . ($data['lastname'] ?? ''); + $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT); + + $stmt = $db->prepare( + "INSERT INTO users (firstname, lastname, name, email, password, admin, created_at, updated_at) VALUES (:firstname, :lastname, :name, :email, :password, :admin, NOW(), NOW())" + ); + + return $stmt->execute([ + ':firstname' => $data['firstname'], + ':lastname' => $data['lastname'], + ':name' => $data['name'], + ':email' => $data['email'], + ':password' => $data['password'], + ':admin' => $data['admin'] ?? 0, + ]); + } + + /** + * Aktualisiert einen Benutzer. + * + * @param int $id + * @param array $data + * @return bool + */ + public static function update($id, $data) { + $db = Database::getConnection(); + $data['name'] = ($data['firstname'] ?? '') . ' ' . ($data['lastname'] ?? ''); + + $sql = "UPDATE users SET firstname = :firstname, lastname = :lastname, name = :name, email = :email, admin = :admin, updated_at = NOW()"; + $params = [ + ':id' => $id, + ':firstname' => $data['firstname'], + ':lastname' => $data['lastname'], + ':name' => $data['name'], + ':email' => $data['email'], + ':admin' => $data['admin'] ?? 0, + ]; + + if (!empty($data['password'])) { + $sql .= ", password = :password"; + $params[':password'] = password_hash($data['password'], PASSWORD_DEFAULT); + } + + $sql .= " WHERE id = :id"; + $stmt = $db->prepare($sql); + return $stmt->execute($params); + } + + /** + * Löscht einen Benutzer (Soft-Delete). + * + * @param int $id + * @return bool + */ + public static function delete($id) { + $db = Database::getConnection(); + $stmt = $db->prepare("UPDATE users SET deleted = NOW() WHERE id = ?"); + return $stmt->execute([$id]); + } } diff --git a/src/Views/admin.php b/src/Views/admin.php index 0b51a11..14350a3 100644 --- a/src/Views/admin.php +++ b/src/Views/admin.php @@ -7,7 +7,7 @@ require __DIR__ . '/partials/header.php';