Filter teams by creator: users only see and manage their own teams

This commit is contained in:
2026-06-07 07:23:47 -04:00
parent 5607a3e507
commit e721a34d8f
2 changed files with 31 additions and 13 deletions

View File

@@ -29,7 +29,8 @@ class TeamController
public function index(): void public function index(): void
{ {
$teams = $this->teamModel->findAll(); $userId = (int) Session::get('user_id');
$teams = $this->teamModel->findByCreator($userId);
$this->view->display('teams.index', [ $this->view->display('teams.index', [
'title' => 'Teams - ServerManager', 'title' => 'Teams - ServerManager',
@@ -72,9 +73,10 @@ class TeamController
public function show(int $id): void public function show(int $id): void
{ {
$userId = (int) Session::get('user_id');
$team = $this->teamModel->findById($id); $team = $this->teamModel->findById($id);
if (!$team) { if (!$team || (int) $team['created_by'] !== $userId) {
$this->view->error(404); $this->view->error(404);
} }
@@ -103,9 +105,10 @@ class TeamController
public function update(int $id): void public function update(int $id): void
{ {
$userId = (int) Session::get('user_id');
$team = $this->teamModel->findById($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); $this->view->json(['success' => false, 'message' => 'Team not found'], 404);
} }
@@ -133,9 +136,10 @@ class TeamController
public function delete(int $id): void public function delete(int $id): void
{ {
$userId = (int) Session::get('user_id');
$team = $this->teamModel->findById($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); $this->view->json(['success' => false, 'message' => 'Team not found'], 404);
} }
@@ -151,9 +155,10 @@ class TeamController
public function addUser(int $id): void public function addUser(int $id): void
{ {
$userId = (int) Session::get('user_id');
$team = $this->teamModel->findById($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); $this->view->json(['success' => false, 'message' => 'Team not found'], 404);
} }
@@ -174,9 +179,10 @@ class TeamController
public function removeUser(int $id): void public function removeUser(int $id): void
{ {
$userId = (int) Session::get('user_id');
$team = $this->teamModel->findById($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); $this->view->json(['success' => false, 'message' => 'Team not found'], 404);
} }
@@ -196,9 +202,10 @@ class TeamController
public function addServer(int $id): void public function addServer(int $id): void
{ {
$userId = (int) Session::get('user_id');
$team = $this->teamModel->findById($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); $this->view->json(['success' => false, 'message' => 'Team not found'], 404);
} }
@@ -232,9 +239,10 @@ class TeamController
public function removeServer(int $id): void public function removeServer(int $id): void
{ {
$userId = (int) Session::get('user_id');
$team = $this->teamModel->findById($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); $this->view->json(['success' => false, 'message' => 'Team not found'], 404);
} }
@@ -254,9 +262,10 @@ class TeamController
public function updateServerRole(int $id): void public function updateServerRole(int $id): void
{ {
$userId = (int) Session::get('user_id');
$team = $this->teamModel->findById($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); $this->view->json(['success' => false, 'message' => 'Team not found'], 404);
} }

View File

@@ -16,17 +16,26 @@ class Team
$this->db = Database::getInstance(); $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( 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_user WHERE team_id = t.id) AS member_count,
(SELECT COUNT(*) FROM team_server WHERE team_id = t.id) AS server_count (SELECT COUNT(*) FROM team_server WHERE team_id = t.id) AS server_count
FROM teams t FROM teams t{$where}
ORDER BY t.name ASC' ORDER BY t.name ASC",
$params
); );
} }
public function findByCreator(int $userId): array
{
return $this->findAll($userId);
}
public function findById(int $id): ?array public function findById(int $id): ?array
{ {
$team = $this->db->fetch( $team = $this->db->fetch(