2121 GroupAndRole ,
2222 UserAndRole ,
2323)
24+ from app .models .files import FileDB , FileOut
2425from app .models .groups import GroupDB
2526from app .models .users import UserDB
2627from app .routers .authentication import get_admin , get_admin_mode
27- from app .search .index import index_dataset
28+ from app .search .index import index_dataset , index_dataset_files
2829from beanie import PydanticObjectId
2930from beanie .operators import In , Or
3031from bson import ObjectId
@@ -39,6 +40,7 @@ async def save_authorization(
3940 dataset_id : str ,
4041 authorization_in : AuthorizationBase ,
4142 user = Depends (get_current_username ),
43+ es = Depends (get_elasticsearchclient ),
4244 allow : bool = Depends (Authorization ("editor" )),
4345):
4446 """Save authorization info in Mongo. This is a triple of dataset_id/user_id/role/group_id."""
@@ -61,6 +63,7 @@ async def save_authorization(
6163 ** authorization_in .dict (), creator = user , user_ids = user_ids
6264 )
6365 await authorization .insert ()
66+ await index_dataset_files (es , dataset_id , update = True )
6467 return authorization .dict ()
6568
6669
@@ -205,6 +208,7 @@ async def set_dataset_group_role(
205208 await index_dataset (
206209 es , DatasetOut (** dataset .dict ()), auth_db .user_ids
207210 )
211+ await index_dataset_files (es , str (dataset_id ))
208212 if len (readonly_user_ids ) > 0 :
209213 readonly_auth_db = AuthorizationDB (
210214 creator = user_id ,
@@ -217,6 +221,7 @@ async def set_dataset_group_role(
217221 await index_dataset (
218222 es , DatasetOut (** dataset .dict ()), readonly_auth_db .user_ids
219223 )
224+ await index_dataset_files (es , str (dataset_id ), update = True )
220225 return auth_db .dict ()
221226 else :
222227 # Create new role entry for this dataset
@@ -228,27 +233,33 @@ async def set_dataset_group_role(
228233 else :
229234 user_ids .append (u .user .email )
230235 # add the users who get the role
231- auth_db = AuthorizationDB (
232- creator = user_id ,
233- dataset_id = PydanticObjectId (dataset_id ),
234- role = role ,
235- group_ids = [PydanticObjectId (group_id )],
236- user_ids = user_ids ,
237- )
238- readonly_auth_db = AuthorizationDB (
239- creator = user_id ,
240- dataset_id = PydanticObjectId (dataset_id ),
241- role = RoleType .VIEWER ,
242- group_ids = [PydanticObjectId (group_id )],
243- user_ids = readonly_user_ids ,
244- )
245- # if there are read only users add them with the role of viewer
246- await auth_db .insert ()
247- await index_dataset (es , DatasetOut (** dataset .dict ()), auth_db .user_ids )
248- await readonly_auth_db .insert ()
249- await index_dataset (
250- es , DatasetOut (** dataset .dict ()), readonly_auth_db .user_ids
251- )
236+ if len (readonly_user_ids ) > 0 :
237+ readonly_auth_db = AuthorizationDB (
238+ creator = user_id ,
239+ dataset_id = PydanticObjectId (dataset_id ),
240+ role = RoleType .VIEWER ,
241+ group_ids = [PydanticObjectId (group_id )],
242+ user_ids = readonly_user_ids ,
243+ )
244+ await readonly_auth_db .insert ()
245+ await index_dataset (
246+ es , DatasetOut (** dataset .dict ()), readonly_auth_db .user_ids
247+ )
248+ await index_dataset_files (es , str (dataset_id ))
249+ if len (user_ids ) > 0 :
250+ auth_db = AuthorizationDB (
251+ creator = user_id ,
252+ dataset_id = PydanticObjectId (dataset_id ),
253+ role = role ,
254+ group_ids = [PydanticObjectId (group_id )],
255+ user_ids = user_ids ,
256+ )
257+ # if there are read only users add them with the role of viewer
258+ await auth_db .insert ()
259+ await index_dataset (
260+ es , DatasetOut (** dataset .dict ()), auth_db .user_ids
261+ )
262+ await index_dataset_files (es , str (dataset_id ))
252263 return auth_db .dict ()
253264 else :
254265 raise HTTPException (status_code = 404 , detail = f"Group { group_id } not found" )
@@ -303,6 +314,7 @@ async def set_dataset_user_role(
303314 auth_db .user_ids .append (username )
304315 await auth_db .save ()
305316 await index_dataset (es , DatasetOut (** dataset .dict ()), auth_db .user_ids )
317+ await index_dataset_files (es , dataset_id , update = True )
306318 return auth_db .dict ()
307319 else :
308320 # Create a new entry
@@ -314,6 +326,7 @@ async def set_dataset_user_role(
314326 )
315327 await auth_db .insert ()
316328 await index_dataset (es , DatasetOut (** dataset .dict ()), [username ])
329+ await index_dataset_files (es , dataset_id )
317330 return auth_db .dict ()
318331 else :
319332 raise HTTPException (status_code = 404 , detail = f"User { username } not found" )
@@ -351,6 +364,7 @@ async def remove_dataset_group_role(
351364 await auth_db .save ()
352365 # Update elasticsearch index with new users
353366 await index_dataset (es , DatasetOut (** dataset .dict ()), auth_db .user_ids )
367+ await index_dataset_files (es , str (dataset_id ), update = True )
354368 return auth_db .dict ()
355369 else :
356370 raise HTTPException (status_code = 404 , detail = f"Group { group_id } not found" )
@@ -387,6 +401,7 @@ async def remove_dataset_user_role(
387401 await auth_db .save ()
388402 # Update elasticsearch index with updated users
389403 await index_dataset (es , DatasetOut (** dataset .dict ()), auth_db .user_ids )
404+ await index_dataset_files (es , dataset_id , update = True )
390405 return auth_db .dict ()
391406 else :
392407 raise HTTPException (status_code = 404 , detail = f"User { username } not found" )
0 commit comments