From e721a34d8fd52306583593432410451e48102cb5 Mon Sep 17 00:00:00 2001 From: Agent Date: Sun, 7 Jun 2026 07:23:47 -0400 Subject: [PATCH] Filter teams by creator: users only see and manage their own teams --- src/Controllers/TeamController.php | 27 ++++++++++++++++++--------- src/Models/Team.php | 17 +++++++++++++---- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Controllers/TeamController.php b/src/Controllers/TeamController.php index ed384f6..bf47522 100644 --- a/src/Controllers/TeamController.php +++ b/src/Controllers/TeamController.php @@ -29,7 +29,8 @@ class TeamController public function index(): void { - $teams = $this->teamModel->findAll(); + $userId = (int) Session::get('user_id'); + $teams = $this->teamModel->findByCreator($userId); $this->view->display('teams.index', [ 'title' => 'Teams - ServerManager', @@ -72,9 +73,10 @@ class TeamController public function show(int $id): void { + $userId = (int) Session::get('user_id'); $team = $this->teamModel->findById($id); - if (!$team) { + if (!$team || (int) $team['created_by'] !== $userId) { $this->view->error(404); } @@ -103,9 +105,10 @@ class TeamController public function update(int $id): void { + $userId = (int) Session::get('user_id'); $team = $this->teamModel->findById($id); - if (!$team) { + if (!$team || (int) $team['created_by'] !== $userId) { $this->view->json(['success' => false, 'message' => 'Team not found'], 404); } @@ -133,9 +136,10 @@ class TeamController public function delete(int $id): void { + $userId = (int) Session::get('user_id'); $team = $this->teamModel->findById($id); - if (!$team) { + if (!$team || (int) $team['created_by'] !== $userId) { $this->view->json(['success' => false, 'message' => 'Team not found'], 404); } @@ -151,9 +155,10 @@ class TeamController public function addUser(int $id): void { + $userId = (int) Session::get('user_id'); $team = $this->teamModel->findById($id); - if (!$team) { + if (!$team || (int) $team['created_by'] !== $userId) { $this->view->json(['success' => false, 'message' => 'Team not found'], 404); } @@ -174,9 +179,10 @@ class TeamController public function removeUser(int $id): void { + $userId = (int) Session::get('user_id'); $team = $this->teamModel->findById($id); - if (!$team) { + if (!$team || (int) $team['created_by'] !== $userId) { $this->view->json(['success' => false, 'message' => 'Team not found'], 404); } @@ -196,9 +202,10 @@ class TeamController public function addServer(int $id): void { + $userId = (int) Session::get('user_id'); $team = $this->teamModel->findById($id); - if (!$team) { + if (!$team || (int) $team['created_by'] !== $userId) { $this->view->json(['success' => false, 'message' => 'Team not found'], 404); } @@ -232,9 +239,10 @@ class TeamController public function removeServer(int $id): void { + $userId = (int) Session::get('user_id'); $team = $this->teamModel->findById($id); - if (!$team) { + if (!$team || (int) $team['created_by'] !== $userId) { $this->view->json(['success' => false, 'message' => 'Team not found'], 404); } @@ -254,9 +262,10 @@ class TeamController public function updateServerRole(int $id): void { + $userId = (int) Session::get('user_id'); $team = $this->teamModel->findById($id); - if (!$team) { + if (!$team || (int) $team['created_by'] !== $userId) { $this->view->json(['success' => false, 'message' => 'Team not found'], 404); } diff --git a/src/Models/Team.php b/src/Models/Team.php index 2ed87fc..b512be6 100644 --- a/src/Models/Team.php +++ b/src/Models/Team.php @@ -16,17 +16,26 @@ class Team $this->db = Database::getInstance(); } - public function findAll(): array + public function findAll(?int $userId = null): array { + $where = $userId ? ' WHERE t.created_by = ?' : ''; + $params = $userId ? [$userId] : []; + return $this->db->fetchAll( - 'SELECT t.*, + "SELECT t.*, (SELECT COUNT(*) FROM team_user WHERE team_id = t.id) AS member_count, (SELECT COUNT(*) FROM team_server WHERE team_id = t.id) AS server_count - FROM teams t - ORDER BY t.name ASC' + FROM teams t{$where} + ORDER BY t.name ASC", + $params ); } + public function findByCreator(int $userId): array + { + return $this->findAll($userId); + } + public function findById(int $id): ?array { $team = $this->db->fetch(