1- from fastapi import APIRouter , Depends , status
1+ from fastapi import APIRouter , Depends , HTTPException , status
22from sqlmodel .ext .asyncio .session import AsyncSession
33
44from api .core .database import get_osm_session , get_task_session
99 WorkspaceTeamItem ,
1010 WorkspaceTeamUpdate ,
1111)
12- from api .src .workspaces .repository import OSMRepository , WorkspaceRepository
13- from api .src .workspaces .schemas import User
12+ from api .src .users .repository import UserRepository
13+ from api .src .users .schemas import User
14+ from api .src .workspaces .repository import WorkspaceRepository
1415
1516router = APIRouter (prefix = "/workspaces/{workspace_id}/teams" , tags = ["teams" ])
1617
@@ -22,10 +23,10 @@ def get_workspace_repo(
2223 return repo
2324
2425
25- def get_osm_repo (
26+ def get_user_repo (
2627 session : AsyncSession = Depends (get_osm_session ),
27- ) -> OSMRepository :
28- repository = OSMRepository (session )
28+ ) -> UserRepository :
29+ repository = UserRepository (session )
2930 return repository
3031
3132
@@ -56,6 +57,12 @@ async def create_team_for_workspace(
5657 team_repo = Depends (get_team_repo ),
5758 current_user : UserInfo = Depends (validate_token ),
5859) -> int :
60+ if not current_user .isWorkspaceLead (workspace_id ):
61+ raise HTTPException (
62+ status_code = status .HTTP_403_FORBIDDEN ,
63+ detail = "Only workspace leads can create teams" ,
64+ )
65+
5966 # Repo guards if workspace doesn't exist or user cannot access:
6067 await workspace_repo .getById (current_user , workspace_id )
6168 return await team_repo .create (workspace_id , team )
@@ -84,6 +91,12 @@ async def update_team_for_workspace(
8491 team_repo = Depends (get_team_repo ),
8592 current_user : UserInfo = Depends (validate_token ),
8693):
94+ if not current_user .isWorkspaceLead (workspace_id ):
95+ raise HTTPException (
96+ status_code = status .HTTP_403_FORBIDDEN ,
97+ detail = "Only workspace leads can update teams" ,
98+ )
99+
87100 # Repo guards if workspace doesn't exist or user cannot access:
88101 await workspace_repo .getById (current_user , workspace_id )
89102 await team_repo .assert_team_in_workspace (team_id , workspace_id )
@@ -98,6 +111,12 @@ async def delete_team_from_workspace(
98111 team_repo = Depends (get_team_repo ),
99112 current_user : UserInfo = Depends (validate_token ),
100113):
114+ if not current_user .isWorkspaceLead (workspace_id ):
115+ raise HTTPException (
116+ status_code = status .HTTP_403_FORBIDDEN ,
117+ detail = "Only workspace leads can delete teams" ,
118+ )
119+
101120 # Repo guards if workspace doesn't exist or user cannot access:
102121 await workspace_repo .getById (current_user , workspace_id )
103122 await team_repo .assert_team_in_workspace (team_id , workspace_id )
@@ -123,14 +142,14 @@ async def join_workspace_team(
123142 workspace_id : int ,
124143 team_id : int ,
125144 workspace_repo = Depends (get_workspace_repo ),
126- osm_repo = Depends (get_osm_repo ),
145+ user_repo = Depends (get_user_repo ),
127146 team_repo = Depends (get_team_repo ),
128147 current_user : UserInfo = Depends (validate_token ),
129148) -> User :
130149 # Repo guards if workspace doesn't exist or user cannot access:
131150 await workspace_repo .getById (current_user , workspace_id )
132151 await team_repo .assert_team_in_workspace (team_id , workspace_id )
133- user = await osm_repo .get_current_user (current_user )
152+ user = await user_repo .get_current_user (current_user )
134153 await team_repo .add_member (team_id , user .id )
135154 return user
136155
@@ -144,6 +163,12 @@ async def add_member_to_workspace_team(
144163 team_repo = Depends (get_team_repo ),
145164 current_user : UserInfo = Depends (validate_token ),
146165):
166+ if not current_user .isWorkspaceLead (workspace_id ):
167+ raise HTTPException (
168+ status_code = status .HTTP_403_FORBIDDEN ,
169+ detail = "Only workspace leads can add team members" ,
170+ )
171+
147172 # Repo guards if workspace doesn't exist or user cannot access:
148173 await workspace_repo .getById (current_user , workspace_id )
149174 await team_repo .assert_team_in_workspace (team_id , workspace_id )
@@ -159,6 +184,12 @@ async def delete_member_from_workspace_team(
159184 team_repo = Depends (get_team_repo ),
160185 current_user : UserInfo = Depends (validate_token ),
161186):
187+ if not current_user .isWorkspaceLead (workspace_id ):
188+ raise HTTPException (
189+ status_code = status .HTTP_403_FORBIDDEN ,
190+ detail = "Only workspace leads can remove team members" ,
191+ )
192+
162193 # Repo guards if workspace doesn't exist or user cannot access:
163194 await workspace_repo .getById (current_user , workspace_id )
164195 await team_repo .assert_team_in_workspace (team_id , workspace_id )
0 commit comments