Skip to content

Commit 24f7dbc

Browse files
committed
splits decorator from actual function
1 parent 9e6aecb commit 24f7dbc

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

services/web/server/src/simcore_service_webserver/security/_authz_web.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,29 @@ async def check_user_permission(
5454
else:
5555
msg += f" {permission}"
5656
raise web.HTTPForbidden(text=msg)
57+
58+
59+
async def check_user_permission_with_groups(
60+
request: web.Request, permission: str
61+
) -> None:
62+
"""Checker that passes to authorized users with given permission via roles OR groups.
63+
64+
Raises:
65+
web.HTTPUnauthorized: If user is not authorized
66+
web.HTTPForbidden: If user is authorized but lacks both role and group permissions
67+
"""
68+
from ..products import products_web
69+
70+
context = {
71+
"authorized_uid": await check_user_authorized(request),
72+
"enable_group_permissions": True,
73+
"request": request,
74+
"product_support_group_id": products_web.get_current_product(
75+
request
76+
).support_standard_group_id,
77+
}
78+
79+
allowed = await aiohttp_security.api.permits(request, permission, context)
80+
if not allowed:
81+
msg = f"You do not have sufficient access rights for {permission}"
82+
raise web.HTTPForbidden(text=msg)

services/web/server/src/simcore_service_webserver/security/decorators.py

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from functools import wraps
22

3-
import aiohttp_security.api # type: ignore[import-untyped]
43
from aiohttp import web
54
from servicelib.aiohttp.typing_extension import Handler
6-
from simcore_service_webserver.products import products_web
75

8-
from ._authz_web import check_user_authorized
6+
from ._authz_web import check_user_permission_with_groups
97
from .security_web import check_user_permission
108

119

@@ -43,20 +41,9 @@ def group_or_role_permission_required(permission: str):
4341
def _decorator(handler: Handler):
4442
@wraps(handler)
4543
async def _wrapped(request: web.Request):
46-
context = {
47-
"authorized_uid": await check_user_authorized(request),
48-
"product_support_group_id": products_web.get_current_product(
49-
request
50-
).support_standard_group_id,
51-
}
52-
53-
# Check both role-based and group-based permissions
54-
if await aiohttp_security.api.permits(request, permission, context):
55-
return await handler(request)
56-
57-
# Neither role nor group permissions granted
58-
msg = f"You do not have sufficient access rights for {permission}"
59-
raise web.HTTPForbidden(text=msg)
44+
await check_user_permission_with_groups(request, permission)
45+
46+
return await handler(request)
6047

6148
return _wrapped
6249

0 commit comments

Comments
 (0)