Skip to content

Commit

Permalink
one place for groups
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Dec 6, 2024
1 parent f924e27 commit 24696f5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
40 changes: 22 additions & 18 deletions services/web/server/src/simcore_service_webserver/groups/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from ..db.plugin import get_database_engine
from ..users.api import get_user
from . import _db
from . import _groups_db
from ._utils import AccessRightsDict
from .exceptions import GroupsError

Expand All @@ -22,15 +22,17 @@ async def list_user_groups_with_read_access(
# NOTE: Careful! It seems we are filtering out groups, such as Product Groups,
# because they do not have read access. I believe this was done because the frontend did not want to display them.
async with get_database_engine(app).acquire() as conn:
return await _db.get_all_user_groups_with_read_access(conn, user_id=user_id)
return await _groups_db.get_all_user_groups_with_read_access(
conn, user_id=user_id
)


async def list_all_user_groups(app: web.Application, user_id: UserID) -> list[Group]:
"""
Return all user groups
"""
async with get_database_engine(app).acquire() as conn:
groups_db = await _db.get_all_user_groups(conn, user_id=user_id)
groups_db = await _groups_db.get_all_user_groups(conn, user_id=user_id)

return [Group.model_construct(**group.model_dump()) for group in groups_db]

Expand All @@ -45,7 +47,7 @@ async def get_user_group(
raises UserInsufficientRightsError
"""
async with get_database_engine(app).acquire() as conn:
return await _db.get_user_group(conn, user_id=user_id, gid=gid)
return await _groups_db.get_user_group(conn, user_id=user_id, gid=gid)


async def get_product_group_for_user(
Expand All @@ -56,7 +58,7 @@ async def get_product_group_for_user(
raises GroupNotFoundError
"""
async with get_database_engine(app).acquire() as conn:
return await _db.get_product_group_for_user(
return await _groups_db.get_product_group_for_user(
conn, user_id=user_id, product_gid=product_gid
)

Expand All @@ -65,7 +67,9 @@ async def create_user_group(
app: web.Application, user_id: UserID, new_group: dict
) -> dict[str, Any]:
async with get_database_engine(app).acquire() as conn:
return await _db.create_user_group(conn, user_id=user_id, new_group=new_group)
return await _groups_db.create_user_group(
conn, user_id=user_id, new_group=new_group
)


async def update_user_group(
Expand All @@ -75,7 +79,7 @@ async def update_user_group(
new_group_values: dict[str, str],
) -> dict[str, str]:
async with get_database_engine(app).acquire() as conn:
return await _db.update_user_group(
return await _groups_db.update_user_group(
conn, user_id=user_id, gid=gid, new_group_values=new_group_values
)

Expand All @@ -84,28 +88,28 @@ async def delete_user_group(
app: web.Application, user_id: UserID, gid: GroupID
) -> None:
async with get_database_engine(app).acquire() as conn:
return await _db.delete_user_group(conn, user_id=user_id, gid=gid)
return await _groups_db.delete_user_group(conn, user_id=user_id, gid=gid)


async def list_users_in_group(
app: web.Application, user_id: UserID, gid: GroupID
) -> list[dict[str, str]]:
async with get_database_engine(app).acquire() as conn:
return await _db.list_users_in_group(conn, user_id=user_id, gid=gid)
return await _groups_db.list_users_in_group(conn, user_id=user_id, gid=gid)


async def auto_add_user_to_groups(app: web.Application, user_id: UserID) -> None:
user: dict = await get_user(app, user_id)

async with get_database_engine(app).acquire() as conn:
return await _db.auto_add_user_to_groups(conn, user=user)
return await _groups_db.auto_add_user_to_groups(conn, user=user)


async def auto_add_user_to_product_group(
app: web.Application, user_id: UserID, product_name: str
) -> GroupID:
async with get_database_engine(app).acquire() as conn:
return await _db.auto_add_user_to_product_group(
return await _groups_db.auto_add_user_to_product_group(
conn, user_id=user_id, product_name=product_name
)

Expand All @@ -114,7 +118,7 @@ async def is_user_by_email_in_group(
app: web.Application, user_email: LowerCaseEmailStr, group_id: GroupID
) -> bool:
async with get_database_engine(app).acquire() as conn:
return await _db.is_user_by_email_in_group(
return await _groups_db.is_user_by_email_in_group(
conn,
email=user_email,
group_id=group_id,
Expand Down Expand Up @@ -143,14 +147,14 @@ async def add_user_in_group(

async with get_database_engine(app).acquire() as conn:
if new_user_email:
user: RowProxy = await _db.get_user_from_email(conn, new_user_email)
user: RowProxy = await _groups_db.get_user_from_email(conn, new_user_email)
new_user_id = user["id"]

if not new_user_id:
msg = "Missing new user in arguments"
raise GroupsError(msg=msg)

return await _db.add_new_user_in_group(
return await _groups_db.add_new_user_in_group(
conn,
user_id=user_id,
gid=gid,
Expand All @@ -163,7 +167,7 @@ async def get_user_in_group(
app: web.Application, user_id: UserID, gid: GroupID, the_user_id_in_group: int
) -> dict[str, str]:
async with get_database_engine(app).acquire() as conn:
return await _db.get_user_in_group(
return await _groups_db.get_user_in_group(
conn, user_id=user_id, gid=gid, the_user_id_in_group=the_user_id_in_group
)

Expand All @@ -176,7 +180,7 @@ async def update_user_in_group(
access_rights: dict,
) -> dict[str, str]:
async with get_database_engine(app).acquire() as conn:
return await _db.update_user_in_group(
return await _groups_db.update_user_in_group(
conn,
user_id=user_id,
gid=gid,
Expand All @@ -189,14 +193,14 @@ async def delete_user_in_group(
app: web.Application, user_id: UserID, gid: GroupID, the_user_id_in_group: int
) -> None:
async with get_database_engine(app).acquire() as conn:
return await _db.delete_user_in_group(
return await _groups_db.delete_user_in_group(
conn, user_id=user_id, gid=gid, the_user_id_in_group=the_user_id_in_group
)


async def get_group_from_gid(app: web.Application, gid: GroupID) -> Group | None:
async with get_database_engine(app).acquire() as conn:
group_db = await _db.get_group_from_gid(conn, gid=gid)
group_db = await _groups_db.get_group_from_gid(conn, gid=gid)

if group_db:
return Group.model_construct(**group_db.model_dump())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from .._constants import APP_SETTINGS_KEY
from ..products.plugin import setup_products
from . import _classifiers_handlers, _handlers
from . import _classifiers_handlers, _groups_handlers

_logger = logging.getLogger(__name__)

Expand All @@ -23,5 +23,5 @@ def setup_groups(app: web.Application):
# plugin dependencies
setup_products(app)

app.router.add_routes(_handlers.routes)
app.router.add_routes(_groups_handlers.routes)
app.router.add_route(_classifiers_handlers.routes)
2 changes: 1 addition & 1 deletion services/web/server/tests/unit/with_dbs/01/test_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from simcore_service_webserver._meta import API_VTAG
from simcore_service_webserver.application_settings import setup_settings
from simcore_service_webserver.db.plugin import setup_db
from simcore_service_webserver.groups._db import (
from simcore_service_webserver.groups._groups_db import (
_DEFAULT_GROUP_OWNER_ACCESS_RIGHTS,
_DEFAULT_GROUP_READ_ACCESS_RIGHTS,
)
Expand Down

0 comments on commit 24696f5

Please sign in to comment.