ServerManager project files
This commit is contained in:
166
src/Controllers/AdminController.php
Executable file
166
src/Controllers/AdminController.php
Executable file
@@ -0,0 +1,166 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ServerManager\Controllers;
|
||||
|
||||
use ServerManager\Core\App;
|
||||
use ServerManager\Core\Session;
|
||||
use ServerManager\Core\Validator;
|
||||
use ServerManager\Models\User;
|
||||
use ServerManager\Services\AuditService;
|
||||
|
||||
class AdminController
|
||||
{
|
||||
private \ServerManager\Core\View $view;
|
||||
private User $userModel;
|
||||
private AuditService $auditService;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->view = App::getInstance()->getView();
|
||||
$this->userModel = new User();
|
||||
$this->auditService = new AuditService();
|
||||
}
|
||||
|
||||
public function users(): void
|
||||
{
|
||||
$page = (int) ($_GET['page'] ?? 1);
|
||||
$users = $this->userModel->getAll($page, 20);
|
||||
|
||||
$this->view->display('admin.users', [
|
||||
'title' => 'User Management - ServerManager',
|
||||
'users' => $users,
|
||||
]);
|
||||
}
|
||||
|
||||
public function createUser(): void
|
||||
{
|
||||
$validator = new Validator();
|
||||
$rules = [
|
||||
'username' => 'required|min:3|max:50|alphaNum',
|
||||
'email' => 'required|email',
|
||||
'password' => 'required|min:8',
|
||||
'role' => 'required|in:super_admin,admin,operator',
|
||||
];
|
||||
|
||||
if (!$validator->validate($_POST, $rules)) {
|
||||
$this->view->json(['success' => false, 'message' => $validator->getFirstError()]);
|
||||
}
|
||||
|
||||
$existingUser = $this->userModel->findByUsername($_POST['username']);
|
||||
if ($existingUser) {
|
||||
$this->view->json(['success' => false, 'message' => 'Username already exists.']);
|
||||
}
|
||||
|
||||
$existingEmail = $this->userModel->findByEmail($_POST['email']);
|
||||
if ($existingEmail) {
|
||||
$this->view->json(['success' => false, 'message' => 'Email already exists.']);
|
||||
}
|
||||
|
||||
$id = $this->userModel->create([
|
||||
'username' => $_POST['username'],
|
||||
'email' => $_POST['email'],
|
||||
'password' => $_POST['password'],
|
||||
'role' => $_POST['role'],
|
||||
'is_active' => 1,
|
||||
]);
|
||||
|
||||
$this->auditService->logUserManagement($id, 'user_created');
|
||||
|
||||
$this->view->json([
|
||||
'success' => true,
|
||||
'message' => 'User created successfully.',
|
||||
'user_id' => $id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateUser(int $id): void
|
||||
{
|
||||
$user = $this->userModel->findById($id);
|
||||
|
||||
if (!$user) {
|
||||
$this->view->json(['success' => false, 'message' => 'User not found'], 404);
|
||||
}
|
||||
|
||||
$updateData = [];
|
||||
|
||||
if (!empty($_POST['email'])) {
|
||||
$updateData['email'] = $_POST['email'];
|
||||
}
|
||||
|
||||
if (!empty($_POST['role'])) {
|
||||
$updateData['role'] = $_POST['role'];
|
||||
}
|
||||
|
||||
if (!empty($_POST['password'])) {
|
||||
$updateData['password'] = $_POST['password'];
|
||||
}
|
||||
|
||||
if (isset($_POST['is_active'])) {
|
||||
$updateData['is_active'] = (int) $_POST['is_active'];
|
||||
}
|
||||
|
||||
if (empty($updateData)) {
|
||||
$this->view->json(['success' => false, 'message' => 'No data to update.']);
|
||||
}
|
||||
|
||||
$this->userModel->update($id, $updateData);
|
||||
|
||||
$this->auditService->logUserManagement($id, 'user_updated');
|
||||
|
||||
$this->view->json([
|
||||
'success' => true,
|
||||
'message' => 'User updated successfully.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function deleteUser(int $id): void
|
||||
{
|
||||
$user = $this->userModel->findById($id);
|
||||
|
||||
if (!$user) {
|
||||
$this->view->json(['success' => false, 'message' => 'User not found'], 404);
|
||||
}
|
||||
|
||||
if ($user['id'] === Session::get('user_id')) {
|
||||
$this->view->json(['success' => false, 'message' => 'You cannot delete your own account.']);
|
||||
}
|
||||
|
||||
$this->userModel->delete($id);
|
||||
|
||||
$this->auditService->logUserManagement($id, 'user_deleted');
|
||||
|
||||
$this->view->json([
|
||||
'success' => true,
|
||||
'message' => 'User deleted successfully.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function auditLogs(): void
|
||||
{
|
||||
$page = (int) ($_GET['page'] ?? 1);
|
||||
$action = $_GET['action'] ?? '';
|
||||
$userId = $_GET['user_id'] ?? '';
|
||||
|
||||
$filters = [];
|
||||
if ($action) $filters['action'] = $action;
|
||||
if ($userId) $filters['user_id'] = (int) $userId;
|
||||
|
||||
$logs = $this->auditService->getAuditLogs($page, 50, $filters);
|
||||
|
||||
$this->view->display('admin.audit', [
|
||||
'title' => 'Audit Logs - ServerManager',
|
||||
'logs' => $logs,
|
||||
'action' => $action,
|
||||
'userId' => $userId,
|
||||
]);
|
||||
}
|
||||
|
||||
public function securitySettings(): void
|
||||
{
|
||||
$this->view->display('admin.security', [
|
||||
'title' => 'Security Settings - ServerManager',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user