|
62 | 62 | get_columns_from_db_model, |
63 | 63 | transaction_context, |
64 | 64 | ) |
| 65 | +from simcore_service_webserver.groups.api import list_all_user_groups_ids |
65 | 66 | from simcore_service_webserver.users.api import get_user_primary_group_id |
66 | 67 | from sqlalchemy import Text, cast |
67 | 68 | from sqlalchemy.ext.asyncio import AsyncConnection |
68 | 69 | from sqlalchemy.sql import func |
69 | 70 |
|
70 | | -from ..db.models import groups, user_to_groups |
71 | 71 | from ..db.plugin import get_asyncpg_engine |
72 | 72 |
|
73 | 73 | _FUNCTIONS_TABLE_COLS = get_columns_from_db_model(functions_table, RegisteredFunctionDB) |
@@ -299,12 +299,13 @@ async def list_functions( |
299 | 299 | ) -> tuple[list[RegisteredFunctionDB], PageMetaInfoLimitOffset]: |
300 | 300 |
|
301 | 301 | async with transaction_context(get_asyncpg_engine(app), connection) as conn: |
302 | | - # Filter functions by user read access |
| 302 | + user_groups = await list_all_user_groups_ids(app, user_id=user_id) |
| 303 | + |
303 | 304 | subquery = ( |
304 | 305 | functions_access_rights_table.select() |
305 | 306 | .with_only_columns(functions_access_rights_table.c.function_uuid) |
306 | 307 | .where( |
307 | | - functions_access_rights_table.c.user_id == user_id, |
| 308 | + functions_access_rights_table.c.group_id.in_(user_groups), |
308 | 309 | functions_access_rights_table.c.read, |
309 | 310 | ) |
310 | 311 | ) |
@@ -348,11 +349,13 @@ async def list_function_jobs( |
348 | 349 | ) -> tuple[list[RegisteredFunctionJobDB], PageMetaInfoLimitOffset]: |
349 | 350 |
|
350 | 351 | async with transaction_context(get_asyncpg_engine(app), connection) as conn: |
| 352 | + user_groups = await list_all_user_groups_ids(app, user_id=user_id) |
| 353 | + |
351 | 354 | access_subquery = ( |
352 | 355 | function_jobs_access_rights_table.select() |
353 | 356 | .with_only_columns(function_jobs_access_rights_table.c.function_job_uuid) |
354 | 357 | .where( |
355 | | - function_jobs_access_rights_table.c.user_id == user_id, |
| 358 | + function_jobs_access_rights_table.c.group_id.in_(user_groups), |
356 | 359 | function_jobs_access_rights_table.c.read, |
357 | 360 | ) |
358 | 361 | ) |
@@ -434,17 +437,21 @@ async def list_function_job_collections( |
434 | 437 | .where(function_jobs_table.c.function_uuid == function_id) |
435 | 438 | ) |
436 | 439 | filter_condition = function_job_collections_table.c.uuid.in_(subquery) |
| 440 | + user_groups = await list_all_user_groups_ids(app, user_id=user_id) |
437 | 441 |
|
438 | 442 | access_subquery = ( |
439 | 443 | function_job_collections_access_rights_table.select() |
440 | 444 | .with_only_columns( |
441 | 445 | function_job_collections_access_rights_table.c.function_job_collection_uuid |
442 | 446 | ) |
443 | 447 | .where( |
444 | | - function_job_collections_access_rights_table.c.user_id == user_id, |
| 448 | + function_job_collections_access_rights_table.c.group_id.in_( |
| 449 | + user_groups |
| 450 | + ), |
445 | 451 | function_job_collections_access_rights_table.c.read, |
446 | 452 | ) |
447 | 453 | ) |
| 454 | + |
448 | 455 | filter_and_access_condition = sqlalchemy.and_( |
449 | 456 | filter_condition, |
450 | 457 | function_job_collections_table.c.uuid.in_(access_subquery), |
@@ -877,18 +884,7 @@ async def get_user_permissions( |
877 | 884 | assert access_rights_table is not None # nosec |
878 | 885 |
|
879 | 886 | async with transaction_context(get_asyncpg_engine(app), connection) as conn: |
880 | | - # Get all groups the user belongs to |
881 | | - user_groups_result = await conn.stream( |
882 | | - sqlalchemy.select(groups.c.gid) |
883 | | - .select_from( |
884 | | - user_to_groups.join(groups, user_to_groups.c.gid == groups.c.gid), |
885 | | - ) |
886 | | - .where(user_to_groups.c.uid == user_id) |
887 | | - ) |
888 | | - user_groups = [row["gid"] for row in await user_groups_result.all()] |
889 | | - |
890 | | - if not user_groups: |
891 | | - return None |
| 887 | + user_groups = await list_all_user_groups_ids(app, user_id=user_id) |
892 | 888 |
|
893 | 889 | # Combine permissions for all groups the user belongs to |
894 | 890 | result = await conn.stream( |
|
0 commit comments