@@ -1032,39 +1032,47 @@ async def get_user_api_access_rights(
10321032 user_id : UserID ,
10331033 product_name : ProductName ,
10341034) -> FunctionUserApiAccessRights :
1035- async with transaction_context (get_asyncpg_engine (app ), connection ) as conn :
1035+ async with pass_or_acquire_connection (get_asyncpg_engine (app ), connection ) as conn :
10361036 user_groups = await list_all_user_groups_ids (app , user_id = user_id )
10371037
1038- rows = [
1039- row
1040- async for row in await conn .stream (
1041- funcapi_api_access_rights_table .select ().where (
1042- funcapi_api_access_rights_table .c .group_id .in_ (user_groups ),
1043- funcapi_api_access_rights_table .c .product_name == product_name ,
1044- )
1038+ # Initialize combined permissions with False values
1039+ combined_permissions = FunctionUserApiAccessRights (
1040+ user_id = user_id ,
1041+ read_functions = False ,
1042+ write_functions = False ,
1043+ execute_functions = False ,
1044+ read_function_jobs = False ,
1045+ write_function_jobs = False ,
1046+ execute_function_jobs = False ,
1047+ read_function_job_collections = False ,
1048+ write_function_job_collections = False ,
1049+ execute_function_job_collections = False ,
1050+ )
1051+
1052+ # Process each row only once and combine permissions
1053+ async for row in await conn .stream (
1054+ funcapi_api_access_rights_table .select ().where (
1055+ funcapi_api_access_rights_table .c .group_id .in_ (user_groups ),
1056+ funcapi_api_access_rights_table .c .product_name == product_name ,
10451057 )
1046- ]
1047- if not rows :
1048- return FunctionUserApiAccessRights (user_id = user_id )
1049- combined_permissions = {
1050- "read_functions" : any (row .read_functions for row in rows ),
1051- "write_functions" : any (row .write_functions for row in rows ),
1052- "execute_functions" : any (row .execute_functions for row in rows ),
1053- "read_function_jobs" : any (row .read_function_jobs for row in rows ),
1054- "write_function_jobs" : any (row .write_function_jobs for row in rows ),
1055- "execute_function_jobs" : any (row .execute_function_jobs for row in rows ),
1056- "read_function_job_collections" : any (
1057- row .read_function_job_collections for row in rows
1058- ),
1059- "write_function_job_collections" : any (
1060- row .write_function_job_collections for row in rows
1061- ),
1062- "execute_function_job_collections" : any (
1063- row .execute_function_job_collections for row in rows
1064- ),
1065- "user_id" : user_id ,
1066- }
1067- return FunctionUserApiAccessRights .model_validate (combined_permissions )
1058+ ):
1059+ combined_permissions .read_functions |= row .read_functions
1060+ combined_permissions .write_functions |= row .write_functions
1061+ combined_permissions .execute_functions |= row .execute_functions
1062+ combined_permissions .read_function_jobs |= row .read_function_jobs
1063+ combined_permissions .write_function_jobs |= row .write_function_jobs
1064+ combined_permissions .execute_function_jobs |= row .execute_function_jobs
1065+ combined_permissions .read_function_job_collections |= (
1066+ row .read_function_job_collections
1067+ )
1068+ combined_permissions .write_function_job_collections |= (
1069+ row .write_function_job_collections
1070+ )
1071+ combined_permissions .execute_function_job_collections |= (
1072+ row .execute_function_job_collections
1073+ )
1074+
1075+ return combined_permissions
10681076
10691077
10701078async def get_user_permissions (
@@ -1076,7 +1084,7 @@ async def get_user_permissions(
10761084 object_id : UUID ,
10771085 object_type : Literal ["function" , "function_job" , "function_job_collection" ],
10781086) -> FunctionAccessRightsDB | None :
1079- async with transaction_context (get_asyncpg_engine (app ), connection ) as conn :
1087+ async with pass_or_acquire_connection (get_asyncpg_engine (app ), connection ) as conn :
10801088 await check_exists (
10811089 app ,
10821090 conn ,
@@ -1099,29 +1107,26 @@ async def get_user_permissions(
10991107
11001108 user_groups = await list_all_user_groups_ids (app , user_id = user_id )
11011109
1102- # Collect rows using streaming to efficiently handle permissions
1103- result = await conn .stream (
1110+ # Initialize combined permissions with False values
1111+ combined_permissions = FunctionAccessRightsDB (
1112+ read = False , write = False , execute = False
1113+ )
1114+
1115+ # Process each row only once and combine permissions
1116+ async for row in await conn .stream (
11041117 access_rights_table .select ()
11051118 .with_only_columns (* cols )
11061119 .where (
11071120 getattr (access_rights_table .c , f"{ object_type } _uuid" ) == object_id ,
11081121 access_rights_table .c .product_name == product_name ,
11091122 access_rights_table .c .group_id .in_ (user_groups ),
11101123 )
1111- )
1112- rows = [row async for row in result ]
1113-
1114- if not rows :
1115- return None
1116-
1117- # Combine permissions across all rows
1118- combined_permissions = {
1119- "read" : any (row .read for row in rows ),
1120- "write" : any (row .write for row in rows ),
1121- "execute" : any (row .execute for row in rows ),
1122- }
1124+ ):
1125+ combined_permissions .read |= row .read
1126+ combined_permissions .write |= row .write
1127+ combined_permissions .execute |= row .execute
11231128
1124- return FunctionAccessRightsDB . model_validate ( combined_permissions )
1129+ return combined_permissions
11251130
11261131
11271132async def check_exists (
0 commit comments