Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e900655b47 |
No files matched your search
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
namespace MVC\Controller;
|
||||
|
||||
use MVC\Core\Controller;
|
||||
use MVC\Model\Users;
|
||||
|
||||
class UserMgmtController extends Controller {
|
||||
public function user_mgmt() {
|
||||
$action = $_GET['action'] ?? 'list';
|
||||
$message = null;
|
||||
|
||||
// 1. Handle Delete (delete())
|
||||
if ($action === 'delete' && isset($_GET['id'])) {
|
||||
if (Users::delete("ID = ?", $_GET['id'])) {
|
||||
$message = ['type' => 'success', 'text' => 'Benutzer erfolgreich gelöscht.'];
|
||||
}
|
||||
$action = 'list';
|
||||
}
|
||||
|
||||
// 2. Handle Create (insert())
|
||||
if ($action === 'create' && $_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$data = [
|
||||
'USERNAME' => $_POST['username'],
|
||||
'PASSWORD' => password_hash($_POST['password'], PASSWORD_DEFAULT),
|
||||
'EMAIL' => $_POST['email'],
|
||||
'ROLE' => $_POST['role']
|
||||
];
|
||||
if (Users::insert($data)) {
|
||||
$message = ['type' => 'success', 'text' => 'Benutzer erstellt.'];
|
||||
$action = 'list';
|
||||
} else {
|
||||
$message = ['type' => 'error', 'text' => 'Fehler beim Erstellen.'];
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Handle Update (update())
|
||||
if ($action === 'edit' && $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['id'])) {
|
||||
$data = [
|
||||
'USERNAME' => $_POST['username'],
|
||||
'EMAIL' => $_POST['email'],
|
||||
'ROLE' => $_POST['role']
|
||||
];
|
||||
// Nur Passwort updaten wenn ausgefüllt
|
||||
if (!empty($_POST['password'])) {
|
||||
$data['PASSWORD'] = password_hash($_POST['password'], PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
if (Users::update($data, "ID = ?", $_POST['id'])) {
|
||||
$message = ['type' => 'success', 'text' => 'Benutzer aktualisiert.'];
|
||||
$action = 'list';
|
||||
} else {
|
||||
$message = ['type' => 'error', 'text' => 'Fehler beim Update.'];
|
||||
}
|
||||
}
|
||||
|
||||
// --- View Logic ---
|
||||
|
||||
$users = [];
|
||||
$editUser = null;
|
||||
|
||||
if ($action === 'list') {
|
||||
// Test select() & where() & all()
|
||||
if (!empty($_GET['search'])) {
|
||||
$users = Users::where("USERNAME LIKE ?", "%" . $_GET['search'] . "%");
|
||||
} else if (isset($_GET['simple'])) {
|
||||
$users = Users::select(["ID", "USERNAME", "ROLE"]);
|
||||
} else {
|
||||
$users = Users::all();
|
||||
}
|
||||
} else if ($action === 'edit' && isset($_GET['id'])) {
|
||||
// Test find()
|
||||
$editUser = Users::find($_GET['id']);
|
||||
}
|
||||
|
||||
return $this->render('user_mgmt', [
|
||||
'action' => $action,
|
||||
'users' => $users,
|
||||
'editUser' => $editUser,
|
||||
'message' => $message
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<h2>👥 User Management (Full CRUD Demo)</h2>
|
||||
|
||||
<?php if (isset($message)): ?>
|
||||
<div style="padding:15px;margin-bottom:20px;border-radius:5px;background:<?= $message['type']==='success'?'rgba(46,204,113,0.2)':'rgba(231,76,60,0.2)'?>; border: 1px solid <?= $message['type']==='success'?'#2ecc71':'#e74c3c'?>;">
|
||||
<?= $message['text'] ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div style="margin-bottom: 20px;">
|
||||
<a href="?service=user_mgmt&action=list" class="btn">Alle Benutzer</a>
|
||||
<a href="?service=user_mgmt&action=list&simple=1" class="btn">Nur Basisdaten (select())</a>
|
||||
<a href="?service=user_mgmt&action=create" class="btn btn-primary">➕ Neu erstellen (insert())</a>
|
||||
</div>
|
||||
|
||||
<?php if ($action === 'list'): ?>
|
||||
<form method="GET" style="margin-bottom: 20px; display: flex; gap: 10px;">
|
||||
<input type="hidden" name="service" value="user_mgmt">
|
||||
<input type="text" name="search" placeholder="Suche nach Username (where())..." value="<?= htmlspecialchars($_GET['search'] ?? '') ?>" style="flex: 1; padding: 10px; border-radius: 4px; border: 1px solid var(--border); background: var(--bg); color: var(--text);">
|
||||
<button type="submit" class="btn">Suchen</button>
|
||||
</form>
|
||||
|
||||
<table style="width: 100%; border-collapse: collapse;">
|
||||
<thead>
|
||||
<tr style="background: var(--sidebar); text-align: left;">
|
||||
<th style="padding: 12px; border: 1px solid var(--border);">ID</th>
|
||||
<th style="padding: 12px; border: 1px solid var(--border);">Username</th>
|
||||
<?php if (!isset($_GET['simple'])): ?>
|
||||
<th style="padding: 12px; border: 1px solid var(--border);">Email</th>
|
||||
<?php endif; ?>
|
||||
<th style="padding: 12px; border: 1px solid var(--border);">Role</th>
|
||||
<th style="padding: 12px; border: 1px solid var(--border);">Aktionen</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($users as $user): ?>
|
||||
<tr>
|
||||
<td style="padding: 12px; border: 1px solid var(--border);"><?= $user['ID'] ?></td>
|
||||
<td style="padding: 12px; border: 1px solid var(--border);"><?= htmlspecialchars($user['USERNAME']) ?></td>
|
||||
<?php if (!isset($_GET['simple'])): ?>
|
||||
<td style="padding: 12px; border: 1px solid var(--border);"><?= htmlspecialchars($user['EMAIL']) ?></td>
|
||||
<?php endif; ?>
|
||||
<td style="padding: 12px; border: 1px solid var(--border);"><span style="background: var(--accent); color: white; padding: 2px 6px; border-radius: 4px; font-size: 0.8rem;"><?= $user['ROLE'] ?></span></td>
|
||||
<td style="padding: 12px; border: 1px solid var(--border);">
|
||||
<a href="?service=user_mgmt&action=edit&id=<?= $user['ID'] ?>" style="color: var(--accent); text-decoration: none;">Bearbeiten (find())</a> |
|
||||
<a href="?service=user_mgmt&action=delete&id=<?= $user['ID'] ?>" style="color: #e74c3c; text-decoration: none;" onclick="return confirm('Sicher?')">Löschen (delete())</a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<?php elseif ($action === 'create' || $action === 'edit'): ?>
|
||||
<h3><?= $action === 'edit' ? '✏️ Benutzer bearbeiten' : '➕ Neuen Benutzer anlegen' ?></h3>
|
||||
<form method="POST" action="?service=user_mgmt&action=<?= $action ?>" style="background: var(--sidebar); padding: 25px; border-radius: 8px; border: 1px solid var(--border);">
|
||||
<?php if ($action === 'edit'): ?>
|
||||
<input type="hidden" name="id" value="<?= $editUser['ID'] ?>">
|
||||
<?php endif; ?>
|
||||
|
||||
<div style="margin-bottom: 15px;">
|
||||
<label style="display: block; margin-bottom: 5px;">Username</label>
|
||||
<input type="text" name="username" required value="<?= htmlspecialchars($editUser['USERNAME'] ?? '') ?>" style="width: 100%; padding: 10px; border-radius: 4px; border: 1px solid var(--border); background: var(--bg); color: var(--text);">
|
||||
</div>
|
||||
|
||||
<div style="margin-bottom: 15px;">
|
||||
<label style="display: block; margin-bottom: 5px;">E-Mail</label>
|
||||
<input type="email" name="email" value="<?= htmlspecialchars($editUser['EMAIL'] ?? '') ?>" style="width: 100%; padding: 10px; border-radius: 4px; border: 1px solid var(--border); background: var(--bg); color: var(--text);">
|
||||
</div>
|
||||
|
||||
<div style="margin-bottom: 15px;">
|
||||
<label style="display: block; margin-bottom: 5px;">Passwort <?= $action === 'edit' ? '(leer lassen zum Behalten)' : '' ?></label>
|
||||
<input type="password" name="password" <?= $action === 'create' ? 'required' : '' ?> style="width: 100%; padding: 10px; border-radius: 4px; border: 1px solid var(--border); background: var(--bg); color: var(--text);">
|
||||
</div>
|
||||
|
||||
<div style="margin-bottom: 20px;">
|
||||
<label style="display: block; margin-bottom: 5px;">Rolle</label>
|
||||
<select name="role" style="width: 100%; padding: 10px; border-radius: 4px; border: 1px solid var(--border); background: var(--bg); color: var(--text);">
|
||||
<option value="user" <?= ($editUser['ROLE'] ?? '') === 'user' ? 'selected' : '' ?>>User</option>
|
||||
<option value="admin" <?= ($editUser['ROLE'] ?? '') === 'admin' ? 'selected' : '' ?>>Admin</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-primary"><?= $action === 'edit' ? 'Speichern (update())' : 'Erstellen (insert())' ?></button>
|
||||
<a href="?service=user_mgmt&action=list" class="btn">Abbrechen</a>
|
||||
</form>
|
||||
<?php endif; ?>
|
||||
|
||||
<style>
|
||||
.btn { display: inline-block; padding: 10px 20px; text-decoration: none; border-radius: 4px; background: var(--sidebar); color: var(--text); border: 1px solid var(--border); cursor: pointer; font-size: 0.9rem; }
|
||||
.btn:hover { background: rgba(0,0,0,0.1); }
|
||||
.btn-primary { background: var(--accent); color: white; border: none; }
|
||||
.btn-primary:hover { opacity: 0.9; }
|
||||
</style>
|
||||
@@ -10,4 +10,5 @@ return [
|
||||
'admin_mail' => ['label' => '📧 Mail Service'],
|
||||
'admin_users' => ['label' => '👤 User Service'],
|
||||
'docs' => ['label' => '📚 Dokumentation'],
|
||||
'user_mgmt' => ['label' => '👥 User Management'],
|
||||
];
|
||||
@@ -14,3 +14,4 @@ $router->add('login', \MVC\Controller\AuthController::class, 'login');
|
||||
$router->add('logout', \MVC\Controller\AuthController::class, 'logout');
|
||||
$router->add('admin_mail', \MVC\Controller\AdminController::class, 'mail_service', ['auth', 'admin']);
|
||||
$router->add('admin_users', \MVC\Controller\AdminController::class, 'user_service', ['auth', 'admin']);
|
||||
$router->add('user_mgmt', \MVC\Controller\UserMgmtController::class, 'user_mgmt', ['auth', 'admin']);
|
||||
Reference in new issue
Block a user