diff --git a/src/Controllers/DashboardController.php b/src/Controllers/DashboardController.php index 22e3a48..a1c852e 100755 --- a/src/Controllers/DashboardController.php +++ b/src/Controllers/DashboardController.php @@ -25,15 +25,22 @@ class DashboardController public function index(): void { - $stats = $this->monitoringService->getDashboardStats(); - $recentActivity = $this->auditService->getRecentActivity(10); - + $role = Session::get('user_role'); $serverModel = new Server(); $userId = (int) Session::get('user_id'); - $accessibleIds = $serverModel->getAccessibleServerIds($userId); - $servers = !empty($accessibleIds) ? array_values(array_filter($serverModel->findAll(true), function ($s) use ($accessibleIds) { - return in_array((int) $s['id'], $accessibleIds, true); - })) : []; + + if ($role === 'super_admin') { + $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', [ 'title' => 'Dashboard - ServerManager', @@ -45,7 +52,15 @@ class DashboardController 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([ 'success' => true, diff --git a/src/Services/MonitoringService.php b/src/Services/MonitoringService.php index 21b95b8..b73fe59 100755 --- a/src/Services/MonitoringService.php +++ b/src/Services/MonitoringService.php @@ -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"); - $onlineServers = $this->db->fetch("SELECT COUNT(*) as total FROM servers WHERE current_status = 'online' AND is_active = 1"); - $offlineServers = $this->db->fetch("SELECT COUNT(*) as total FROM servers WHERE current_status = 'offline' AND is_active = 1"); + $where = ''; + $params = []; + + 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( - "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( - "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( - "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 [