Skip to content

Commit 76a4a5f

Browse files
committed
updating db in api module
1 parent 7c252d2 commit 76a4a5f

File tree

7 files changed

+37
-40
lines changed

7 files changed

+37
-40
lines changed

services/web/server/src/simcore_service_webserver/garbage_collector/_core_guests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ async def remove_guest_user_with_all_its_resources(
145145
"""Removes a GUEST user with all its associated projects and S3/MinIO files"""
146146

147147
try:
148-
user_role: UserRole = await get_user_role(app, user_id)
148+
user_role: UserRole = await get_user_role(app, user_id=user_id)
149149
if user_role > UserRole.GUEST:
150150
# NOTE: This acts as a protection barrier to avoid removing resources to more
151151
# priviledge users

services/web/server/src/simcore_service_webserver/garbage_collector/_core_orphans.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async def _remove_service(
3737
save_service_state = False
3838
else:
3939
try:
40-
if await get_user_role(app, service.user_id) <= UserRole.GUEST:
40+
if await get_user_role(app, user_id=service.user_id) <= UserRole.GUEST:
4141
save_service_state = False
4242
else:
4343
save_service_state = await has_user_project_access_rights(

services/web/server/src/simcore_service_webserver/projects/_nodes_handlers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ async def stop_node(request: web.Request) -> web.Response:
376376
permission="write",
377377
)
378378

379-
user_role = await get_user_role(request.app, req_ctx.user_id)
379+
user_role = await get_user_role(request.app, user_id=req_ctx.user_id)
380380
if user_role is None or user_role <= UserRole.GUEST:
381381
save_state = False
382382

services/web/server/src/simcore_service_webserver/projects/_states_handlers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@ async def open_project(request: web.Request) -> web.Response:
109109
project_type: ProjectType = await projects_api.get_project_type(
110110
request.app, path_params.project_id
111111
)
112-
user_role: UserRole = await api.get_user_role(request.app, req_ctx.user_id)
112+
user_role: UserRole = await api.get_user_role(
113+
request.app, user_id=req_ctx.user_id
114+
)
113115
if project_type is ProjectType.TEMPLATE and user_role < UserRole.USER:
114116
# only USERS/TESTERS can do that
115117
raise web.HTTPForbidden(reason="Wrong user role to open/edit a template")

services/web/server/src/simcore_service_webserver/projects/projects_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ async def _start_dynamic_service(
593593
raise
594594

595595
save_state = False
596-
user_role: UserRole = await get_user_role(request.app, user_id)
596+
user_role: UserRole = await get_user_role(request.app, user_id=user_id)
597597
if user_role > UserRole.GUEST:
598598
save_state = await has_user_project_access_rights(
599599
request.app, project_id=project_uuid, user_id=user_id, permission="write"
@@ -1716,7 +1716,7 @@ async def remove_project_dynamic_services(
17161716

17171717
user_role: UserRole | None = None
17181718
try:
1719-
user_role = await get_user_role(app, user_id)
1719+
user_role = await get_user_role(app, user_id=user_id)
17201720
except UserNotFoundError:
17211721
user_role = None
17221722

services/web/server/src/simcore_service_webserver/users/api.py

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"""
77

88
import logging
9-
from collections import deque
109
from typing import Any, NamedTuple, TypedDict
1110

1211
import simcore_postgres_database.errors as db_errors
@@ -28,10 +27,13 @@
2827
from simcore_postgres_database.utils_groups_extra_properties import (
2928
GroupExtraPropertiesNotFoundError,
3029
)
30+
from simcore_postgres_database.utils_repos import (
31+
pass_or_acquire_connection,
32+
transaction_context,
33+
)
3134
from simcore_postgres_database.utils_users import generate_alternative_username
3235

33-
from ..db.plugin import get_database_engine
34-
from ..db.plugin import get_asyncpg_engine, get_database_engine
36+
from ..db.plugin import get_asyncpg_engine
3537
from ..login.storage import AsyncpgStorage, get_plugin_storage
3638
from ..security.api import clean_auth_policy_cache
3739
from . import _users_repository
@@ -82,23 +84,19 @@ def _parse_as_user(user_id: Any) -> UserID:
8284

8385

8486
async def get_user_profile(
85-
app: web.Application, user_id: UserID, product_name: ProductName
87+
app: web.Application, *, user_id: UserID, product_name: ProductName
8688
) -> MyProfileGet:
8789
"""
8890
:raises UserNotFoundError:
8991
:raises MissingGroupExtraPropertiesForProductError: when product is not properly configured
9092
"""
91-
92-
engine = get_database_engine(app)
9393
user_profile: dict[str, Any] = {}
9494
user_primary_group = everyone_group = {}
9595
user_standard_groups = []
9696
user_id = _parse_as_user(user_id)
9797

98-
async with engine.acquire() as conn:
99-
row: RowProxy
100-
101-
async for row in conn.execute(
98+
async with pass_or_acquire_connection(engine=get_asyncpg_engine(app)) as conn:
99+
result = await conn.stream(
102100
sa.select(users, groups, user_to_groups.c.access_rights)
103101
.select_from(
104102
users.join(user_to_groups, users.c.id == user_to_groups.c.uid).join(
@@ -108,7 +106,9 @@ async def get_user_profile(
108106
.where(users.c.id == user_id)
109107
.order_by(sa.asc(groups.c.name))
110108
.set_label_style(sa.LABEL_STYLE_TABLENAME_PLUS_COL)
111-
):
109+
)
110+
111+
async for row in result:
112112
if not user_profile:
113113
user_profile = {
114114
"id": row.users_id,
@@ -199,13 +199,12 @@ async def update_user_profile(
199199
user_id = _parse_as_user(user_id)
200200

201201
if updated_values := ToUserUpdateDB.from_api(update).to_db():
202-
async with get_database_engine(app).acquire() as conn:
202+
203+
async with transaction_context(engine=get_asyncpg_engine(app)) as conn:
203204
query = users.update().where(users.c.id == user_id).values(**updated_values)
204205

205206
try:
206-
207-
resp = await conn.execute(query)
208-
assert resp.rowcount == 1 # nosec
207+
await conn.execute(query)
209208

210209
except db_errors.UniqueViolation as err:
211210
user_name = updated_values.get("name")
@@ -218,15 +217,14 @@ async def update_user_profile(
218217
) from err
219218

220219

221-
async def get_user_role(app: web.Application, user_id: UserID) -> UserRole:
220+
async def get_user_role(app: web.Application, *, user_id: UserID) -> UserRole:
222221
"""
223222
:raises UserNotFoundError:
224223
"""
225224
user_id = _parse_as_user(user_id)
226225

227-
engine = get_database_engine(app)
228-
async with engine.acquire() as conn:
229-
user_role: RowProxy | None = await conn.scalar(
226+
async with pass_or_acquire_connection(engine=get_asyncpg_engine(app)) as conn:
227+
user_role = await conn.scalar(
230228
sa.select(users.c.role).where(users.c.id == user_id)
231229
)
232230
if user_role is None:
@@ -289,14 +287,11 @@ async def get_user_display_and_id_names(
289287

290288

291289
async def get_guest_user_ids_and_names(app: web.Application) -> list[tuple[int, str]]:
292-
engine = get_database_engine(app)
293-
result: deque = deque()
294-
async with engine.acquire() as conn:
295-
async for row in conn.execute(
290+
async with pass_or_acquire_connection(engine=get_asyncpg_engine(app)) as conn:
291+
result = await conn.stream(
296292
sa.select(users.c.id, users.c.name).where(users.c.role == UserRole.GUEST)
297-
):
298-
result.append(row.as_tuple())
299-
return list(result)
293+
)
294+
return [(row.id, row.name) async for row in result]
300295

301296

302297
async def delete_user_without_projects(app: web.Application, user_id: UserID) -> None:
@@ -305,6 +300,7 @@ async def delete_user_without_projects(app: web.Application, user_id: UserID) ->
305300
# otherwise this function will raise asyncpg.exceptions.ForeignKeyViolationError
306301
# Consider "marking" users as deleted and havning a background job that
307302
# cleans it up
303+
# TODO: upgrade!!!
308304
db: AsyncpgStorage = get_plugin_storage(app)
309305
user = await db.get_user({"id": user_id})
310306
if not user:
@@ -331,8 +327,8 @@ async def get_user_fullname(app: web.Application, user_id: UserID) -> FullNameDi
331327
"""
332328
user_id = _parse_as_user(user_id)
333329

334-
async with get_database_engine(app).acquire() as conn:
335-
result = await conn.execute(
330+
async with pass_or_acquire_connection(engine=get_asyncpg_engine(app)) as conn:
331+
result = await conn.stream(
336332
sa.select(users.c.first_name, users.c.last_name).where(
337333
users.c.id == user_id
338334
)
@@ -354,12 +350,11 @@ async def get_user(app: web.Application, user_id: UserID) -> dict[str, Any]:
354350
row = await _users_repository.get_user_or_raise(
355351
engine=get_asyncpg_engine(app), user_id=user_id
356352
)
357-
return dict(row)
353+
return row._asdict()
358354

359355

360356
async def get_user_id_from_gid(app: web.Application, primary_gid: int) -> UserID:
361-
engine = get_database_engine(app)
362-
async with engine.acquire() as conn:
357+
async with pass_or_acquire_connection(engine=get_asyncpg_engine(app)) as conn:
363358
user_id: UserID = await conn.scalar(
364359
sa.select(users.c.id).where(users.c.primary_gid == primary_gid)
365360
)

services/web/server/tests/unit/with_dbs/04/studies_dispatcher/test_studies_dispatcher_studies_access.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,15 +365,15 @@ async def test_access_cookie_of_expired_user(
365365
resp = await client.get(f"{me_url}")
366366

367367
data, _ = await assert_status(resp, status.HTTP_200_OK)
368-
assert await get_user_role(app, data["id"]) == UserRole.GUEST
368+
assert await get_user_role(app, user_id=data["id"]) == UserRole.GUEST
369369

370370
async def enforce_garbage_collect_guest(uid):
371371
# TODO: can be replaced now by actual GC
372372
# Emulates garbage collector:
373373
# - GUEST user expired, cleaning it up
374374
# - client still holds cookie with its identifier nonetheless
375375
#
376-
assert await get_user_role(app, uid) == UserRole.GUEST
376+
assert await get_user_role(app, user_id=uid) == UserRole.GUEST
377377
projects = await _get_user_projects(client)
378378
assert len(projects) == 1
379379

@@ -401,7 +401,7 @@ async def enforce_garbage_collect_guest(uid):
401401
# as a guest user
402402
resp = await client.get(f"{me_url}")
403403
data, _ = await assert_status(resp, status.HTTP_200_OK)
404-
assert await get_user_role(app, data["id"]) == UserRole.GUEST
404+
assert await get_user_role(app, user_id=data["id"]) == UserRole.GUEST
405405

406406
# But I am another user
407407
assert data["id"] != user_id

0 commit comments

Comments
 (0)