Server ownership, team access, role-based UI #7
@@ -25,15 +25,22 @@ class DashboardController
|
|||||||
|
|
||||||
public function index(): void
|
public function index(): void
|
||||||
{
|
{
|
||||||
$stats = $this->monitoringService->getDashboardStats();
|
$role = Session::get('user_role');
|
||||||
$recentActivity = $this->auditService->getRecentActivity(10);
|
|
||||||
|
|
||||||
$serverModel = new Server();
|
$serverModel = new Server();
|
||||||
$userId = (int) Session::get('user_id');
|
$userId = (int) Session::get('user_id');
|
||||||
$accessibleIds = $serverModel->getAccessibleServerIds($userId);
|
|
||||||
$servers = !empty($accessibleIds) ? array_values(array_filter($serverModel->findAll(true), function ($s) use ($accessibleIds) {
|
if ($role === 'super_admin') {
|
||||||
return in_array((int) $s['id'], $accessibleIds, true);
|
$stats = $this->monitoringService->getDashboardStats();
|
||||||
})) : [];
|
$servers = $serverModel->findAll(true);
|
||||||
|
} else {
|
||||||
|
$accessibleIds = $serverModel->getAccessibleServerIds($userId);
|
||||||
|
$stats = $this->monitoringService->getDashboardStats($accessibleIds);
|
||||||
|
$servers = !empty($accessibleIds) ? array_values(array_filter($serverModel->findAll(true), function ($s) use ($accessibleIds) {
|
||||||
|
return in_array((int) $s['id'], $accessibleIds, true);
|
||||||
|
})) : [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$recentActivity = $this->auditService->getRecentActivity(10);
|
||||||
|
|
||||||
$this->view->display('dashboard.index', [
|
$this->view->display('dashboard.index', [
|
||||||
'title' => 'Dashboard - ServerManager',
|
'title' => 'Dashboard - ServerManager',
|
||||||
@@ -45,7 +52,15 @@ class DashboardController
|
|||||||
|
|
||||||
public function stats(): void
|
public function stats(): void
|
||||||
{
|
{
|
||||||
$stats = $this->monitoringService->getDashboardStats();
|
$role = Session::get('user_role');
|
||||||
|
|
||||||
|
if ($role === 'super_admin') {
|
||||||
|
$stats = $this->monitoringService->getDashboardStats();
|
||||||
|
} else {
|
||||||
|
$serverModel = new Server();
|
||||||
|
$accessibleIds = $serverModel->getAccessibleServerIds((int) Session::get('user_id'));
|
||||||
|
$stats = $this->monitoringService->getDashboardStats($accessibleIds);
|
||||||
|
}
|
||||||
|
|
||||||
$this->view->json([
|
$this->view->json([
|
||||||
'success' => true,
|
'success' => true,
|
||||||
|
|||||||
@@ -173,20 +173,41 @@ class MonitoringService
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDashboardStats(): array
|
public function getDashboardStats(?array $serverIds = null): array
|
||||||
{
|
{
|
||||||
$totalServers = $this->db->fetch("SELECT COUNT(*) as total FROM servers");
|
$where = '';
|
||||||
$onlineServers = $this->db->fetch("SELECT COUNT(*) as total FROM servers WHERE current_status = 'online' AND is_active = 1");
|
$params = [];
|
||||||
$offlineServers = $this->db->fetch("SELECT COUNT(*) as total FROM servers WHERE current_status = 'offline' AND is_active = 1");
|
|
||||||
|
if ($serverIds !== null) {
|
||||||
|
if (empty($serverIds)) {
|
||||||
|
$serverIds = [-1];
|
||||||
|
}
|
||||||
|
$placeholders = implode(',', array_fill(0, count($serverIds), '?'));
|
||||||
|
$where = " AND s.id IN ({$placeholders})";
|
||||||
|
$params = $serverIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
$totalServers = $this->db->fetch("SELECT COUNT(*) as total FROM servers s WHERE 1=1{$where}", $params);
|
||||||
|
$onlineServers = $this->db->fetch(
|
||||||
|
"SELECT COUNT(*) as total FROM servers s WHERE s.current_status = 'online' AND s.is_active = 1{$where}",
|
||||||
|
$params
|
||||||
|
);
|
||||||
|
$offlineServers = $this->db->fetch(
|
||||||
|
"SELECT COUNT(*) as total FROM servers s WHERE s.current_status = 'offline' AND s.is_active = 1{$where}",
|
||||||
|
$params
|
||||||
|
);
|
||||||
|
|
||||||
$avgCpu = $this->db->fetch(
|
$avgCpu = $this->db->fetch(
|
||||||
"SELECT AVG(cpu_usage) as avg FROM servers WHERE is_active = 1 AND current_status = 'online'"
|
"SELECT AVG(s.cpu_usage) as avg FROM servers s WHERE s.is_active = 1 AND s.current_status = 'online'{$where}",
|
||||||
|
$params
|
||||||
);
|
);
|
||||||
$avgRam = $this->db->fetch(
|
$avgRam = $this->db->fetch(
|
||||||
"SELECT AVG(ram_usage) as avg FROM servers WHERE is_active = 1 AND current_status = 'online'"
|
"SELECT AVG(s.ram_usage) as avg FROM servers s WHERE s.is_active = 1 AND s.current_status = 'online'{$where}",
|
||||||
|
$params
|
||||||
);
|
);
|
||||||
$avgDisk = $this->db->fetch(
|
$avgDisk = $this->db->fetch(
|
||||||
"SELECT AVG(disk_usage) as avg FROM servers WHERE is_active = 1 AND current_status = 'online'"
|
"SELECT AVG(s.disk_usage) as avg FROM servers s WHERE s.is_active = 1 AND s.current_status = 'online'{$where}",
|
||||||
|
$params
|
||||||
);
|
);
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|||||||
Reference in New Issue
Block a user