philippurbschat.de/home/app/controllers/AdminController.php

89 lines
No EOL
3 KiB
PHP

<?php
require_once __DIR__ . '/../../core/Controller.php';
require_once __DIR__ . '/../services/HealthService.php';
class AdminController extends Controller {
// Auth-Check
public function __construct() {
if (session_status() === PHP_SESSION_NONE) { session_start(); }
if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) {
header('Location: ' . Config::get('BASE_URL', '/'));
exit;
}
}
// Dashboard
public function index() {
$report = HealthService::getHealthReport();
$this->view('admin/dashboard', [
'title' => 'Admin Dashboard',
'health' => $report
]);
}
// Settings
public function settings() {
$this->view('admin/settings', [
'title' => 'Einstellungen'
]);
}
// User List
public function users() {
$userModel = $this->model('User');
$users = $userModel->getAll();
$this->view('admin/users', [
'title' => 'Benutzerverwaltung',
'users' => $users
]);
}
// User Form
public function user_form($id = null) {
$user = null;
if ($id) {
$userModel = $this->model('User');
$user = $userModel->getById($id);
if (!$user) {
Flash::set('Nutzer nicht gefunden.', 'error');
header('Location: ' . Config::get('BASE_URL') . 'admin/users');
exit;
}
}
$this->view('admin/user_form', [
'title' => $id ? 'Benutzer bearbeiten' : 'Neuen Benutzer anlegen',
'user' => $user
]);
}
// Save User
public function user_save() {
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
header('Location: ' . Config::get('BASE_URL') . 'admin/users');
exit;
}
try {
Security::checkCsrf($_POST['csrf_token']);
$id = !empty($_POST['id']) ? $_POST['id'] : null;
$userModel = $this->model('User');
if (empty($_POST['name']) || empty($_POST['email'])) {
throw new Exception("Name und E-Mail sind Pflichtfelder.");
}
$userModel->save($_POST, $id);
Flash::set('Nutzer erfolgreich gespeichert!', 'success');
} catch (Exception $e) {
Flash::set($e->getMessage(), 'error');
}
header('Location: ' . Config::get('BASE_URL') . 'admin/users');
exit;
}
// Delete User
public function user_delete($id) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
try {
Security::checkCsrf($_POST['csrf_token']);
$userModel = $this->model('User');
$userModel->delete($id);
Flash::set('Nutzer wurde gelöscht.', 'success');
} catch (Exception $e) {
Flash::set('Fehler beim Löschen: ' . $e->getMessage(), 'error');
}
}
header('Location: ' . Config::get('BASE_URL') . 'admin/users');
exit;
}
}