feat: Implementiere Admin-Benutzerverwaltung inkl. Soft-Delete

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
torsten 2026-10-04 17:34:43 +02:00
parent cf10316ae6
commit 9767767b13
4 changed files with 257 additions and 3 deletions

View file

@ -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 {

View file

@ -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]);
}
}

View file

@ -7,7 +7,7 @@ require __DIR__ . '/partials/header.php';
<div class="admin-menu">
<ul>
<li><a href="#">Benutzer verwalten</a></li>
<li><a href="/index.php?page=admin_users">Benutzer verwalten</a></li>
<li><a href="#">Saisons & Runden verwalten</a></li>
<li><a href="#">Spiele & Ergebnisse eintragen</a></li>
<li><a href="#">Zahlungen verwalten</a></li>

111
src/Views/admin/users.php Normal file
View file

@ -0,0 +1,111 @@
<?php
$pageTitle = 'Benutzer verwalten';
require __DIR__ . '/../partials/header.php';
?>
<h1>Benutzer verwalten</h1>
<?php if (!empty($message)): ?>
<p style="color: green;"><?php echo htmlspecialchars($message); ?></p>
<?php endif; ?>
<?php if (!empty($error)): ?>
<p style="color: red;"><?php echo htmlspecialchars($error); ?></p>
<?php endif; ?>
<!-- Form for creating/editing a user -->
<h2><?php echo $editUser ? 'Benutzer bearbeiten' : 'Neuen Benutzer anlegen'; ?></h2>
<form action="/index.php?page=admin_users" method="post">
<?php if ($editUser): ?>
<input type="hidden" name="action" value="update">
<input type="hidden" name="id" value="<?php echo htmlspecialchars($editUser['id']); ?>">
<?php else: ?>
<input type="hidden" name="action" value="create">
<?php endif; ?>
<div class="form-group">
<label for="firstname">Vorname:</label>
<input type="text" id="firstname" name="firstname" value="<?php echo htmlspecialchars($editUser['firstname'] ?? ''); ?>" required>
</div>
<div class="form-group">
<label for="lastname">Nachname:</label>
<input type="text" id="lastname" name="lastname" value="<?php echo htmlspecialchars($editUser['lastname'] ?? ''); ?>" required>
</div>
<div class="form-group">
<label for="email">E-Mail:</label>
<input type="email" id="email" name="email" value="<?php echo htmlspecialchars($editUser['email'] ?? ''); ?>" required>
</div>
<div class="form-group">
<label for="password">Passwort:</label>
<input type="password" id="password" name="password" placeholder="<?php echo $editUser ? 'Leer lassen, um nicht zu ändern' : ''; ?>">
</div>
<div class="form-group">
<label for="admin">Admin:</label>
<input type="checkbox" id="admin" name="admin" value="1" <?php echo !empty($editUser['admin']) ? 'checked' : ''; ?>>
</div>
<div class="form-group">
<button type="submit"><?php echo $editUser ? 'Speichern' : 'Anlegen'; ?></button>
<?php if ($editUser): ?>
<a href="/index.php?page=admin_users">Abbrechen</a>
<?php endif; ?>
</div>
</form>
<hr>
<!-- User List -->
<h2>Benutzerliste</h2>
<table>
<thead>
<tr>
<th>Name</th>
<th>E-Mail</th>
<th>Admin</th>
<th>Aktionen</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user): ?>
<tr>
<td><?php echo htmlspecialchars($user['name']); ?></td>
<td><?php echo htmlspecialchars($user['email']); ?></td>
<td><?php echo $user['admin'] ? 'Ja' : 'Nein'; ?></td>
<td>
<a href="/index.php?page=admin_users&edit_id=<?php echo $user['id']; ?>">Bearbeiten</a>
<form action="/index.php?page=admin_users" method="post" style="display: inline;" onsubmit="return confirm('Sicher, dass du diesen Benutzer löschen willst?');">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?php echo $user['id']; ?>">
<button type="submit" class="link-button">Löschen</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<style>
/* From login.php, for consistency */
.form-group {
margin-bottom: 1rem;
}
.form-group label {
display: inline-block;
width: 150px;
text-align: right;
margin-right: 10px;
}
.form-group button, .form-group a {
margin-left: calc(150px + 10px);
}
.link-button {
background: none;
border: none;
color: #007bff;
cursor: pointer;
padding: 0;
font-size: inherit;
text-decoration: underline;
font-family: inherit;
}
</style>
<?php require __DIR__ . '/../partials/footer.php'; ?>