@@ -26,33 +26,71 @@ def read_galleries(
2626) -> Any :
2727 """
2828 Retrieve galleries. If project_id is provided, get galleries for that project.
29- Otherwise, get all galleries for the user's organization.
29+ Otherwise, get all galleries based on user type:
30+ - Team members: all galleries from their organization
31+ - Clients: galleries from projects they have access to
3032 """
31- if not current_user .organization_id :
32- raise HTTPException (
33- status_code = 400 , detail = "User is not part of an organization"
34- )
33+ user_type = getattr (current_user , "user_type" , None )
3534
3635 if project_id :
37- # Verify project belongs to user's organization
36+ # Verify user has access to this project
3837 project = crud .get_project (session = session , project_id = project_id )
39- if not project or project .organization_id != current_user .organization_id :
40- raise HTTPException (status_code = 403 , detail = "Not enough permissions" )
38+ if not project :
39+ raise HTTPException (status_code = 404 , detail = "Project not found" )
40+
41+ # Check access based on user type
42+ if user_type == "client" :
43+ # Client must have explicit access
44+ if not crud .user_has_project_access (
45+ session = session , project_id = project_id , user_id = current_user .id
46+ ):
47+ raise HTTPException (status_code = 403 , detail = "Not enough permissions" )
48+ else :
49+ # Team member must be in same organization
50+ if not current_user .organization_id or project .organization_id != current_user .organization_id :
51+ raise HTTPException (status_code = 403 , detail = "Not enough permissions" )
4152
4253 galleries = crud .get_galleries_by_project (
4354 session = session , project_id = project_id , skip = skip , limit = limit
4455 )
4556 count = len (galleries ) # Simple count for project galleries
4657 else :
47- galleries = crud .get_galleries_by_organization (
48- session = session ,
49- organization_id = current_user .organization_id ,
50- skip = skip ,
51- limit = limit ,
52- )
53- count = crud .count_galleries_by_organization (
54- session = session , organization_id = current_user .organization_id
55- )
58+ # No specific project - list all accessible galleries
59+ if user_type == "client" :
60+ # Get galleries from all projects the client has access to
61+ accessible_projects = crud .get_user_accessible_projects (
62+ session = session , user_id = current_user .id , skip = 0 , limit = 1000
63+ )
64+ project_ids = [p .id for p in accessible_projects ]
65+
66+ # Get galleries for all accessible projects
67+ galleries = []
68+ for pid in project_ids [skip :skip + limit ]:
69+ project_galleries = crud .get_galleries_by_project (
70+ session = session , project_id = pid , skip = 0 , limit = 100
71+ )
72+ galleries .extend (project_galleries )
73+
74+ count = sum (
75+ len (crud .get_galleries_by_project (session = session , project_id = pid , skip = 0 , limit = 1000 ))
76+ for pid in project_ids
77+ )
78+ else :
79+ # Team member - get all galleries from organization
80+ if not current_user .organization_id :
81+ raise HTTPException (
82+ status_code = 400 , detail = "User is not part of an organization"
83+ )
84+
85+ galleries = crud .get_galleries_by_organization (
86+ session = session ,
87+ organization_id = current_user .organization_id ,
88+ skip = skip ,
89+ limit = limit ,
90+ )
91+ count = crud .count_galleries_by_organization (
92+ session = session , organization_id = current_user .organization_id
93+ )
5694
5795 return GalleriesPublic (data = galleries , count = count )
5896
@@ -62,8 +100,16 @@ def create_gallery(
62100 * , session : SessionDep , current_user : CurrentUser , gallery_in : GalleryCreate
63101) -> Any :
64102 """
65- Create new gallery.
103+ Create new gallery. Only team members can create galleries.
66104 """
105+ user_type = getattr (current_user , "user_type" , None )
106+
107+ # Only team members can create galleries
108+ if user_type != "team_member" :
109+ raise HTTPException (
110+ status_code = 403 , detail = "Only team members can create galleries"
111+ )
112+
67113 if not current_user .organization_id :
68114 raise HTTPException (
69115 status_code = 400 , detail = "User is not part of an organization"
@@ -87,10 +133,22 @@ def read_gallery(session: SessionDep, current_user: CurrentUser, id: uuid.UUID)
87133 if not gallery :
88134 raise HTTPException (status_code = 404 , detail = "Gallery not found" )
89135
90- # Check if gallery's project belongs to user's organization
136+ # Check access based on user type
137+ user_type = getattr (current_user , "user_type" , None )
91138 project = crud .get_project (session = session , project_id = gallery .project_id )
92- if not project or project .organization_id != current_user .organization_id :
93- raise HTTPException (status_code = 403 , detail = "Not enough permissions" )
139+ if not project :
140+ raise HTTPException (status_code = 404 , detail = "Project not found" )
141+
142+ if user_type == "client" :
143+ # Client must have access to the project
144+ if not crud .user_has_project_access (
145+ session = session , project_id = project .id , user_id = current_user .id
146+ ):
147+ raise HTTPException (status_code = 403 , detail = "Not enough permissions" )
148+ else :
149+ # Team member must be in same organization
150+ if not current_user .organization_id or project .organization_id != current_user .organization_id :
151+ raise HTTPException (status_code = 403 , detail = "Not enough permissions" )
94152
95153 return gallery
96154
@@ -104,8 +162,16 @@ def update_gallery(
104162 gallery_in : GalleryUpdate ,
105163) -> Any :
106164 """
107- Update a gallery.
165+ Update a gallery. Only team members can update galleries.
108166 """
167+ user_type = getattr (current_user , "user_type" , None )
168+
169+ # Only team members can update galleries
170+ if user_type != "team_member" :
171+ raise HTTPException (
172+ status_code = 403 , detail = "Only team members can update galleries"
173+ )
174+
109175 gallery = crud .get_gallery (session = session , gallery_id = id )
110176 if not gallery :
111177 raise HTTPException (status_code = 404 , detail = "Gallery not found" )
@@ -126,8 +192,16 @@ def delete_gallery(
126192 session : SessionDep , current_user : CurrentUser , id : uuid .UUID
127193) -> Message :
128194 """
129- Delete a gallery.
195+ Delete a gallery. Only team members can delete galleries.
130196 """
197+ user_type = getattr (current_user , "user_type" , None )
198+
199+ # Only team members can delete galleries
200+ if user_type != "team_member" :
201+ raise HTTPException (
202+ status_code = 403 , detail = "Only team members can delete galleries"
203+ )
204+
131205 gallery = crud .get_gallery (session = session , gallery_id = id )
132206 if not gallery :
133207 raise HTTPException (status_code = 404 , detail = "Gallery not found" )
0 commit comments