Skip to content

Commit fa5077b

Browse files
committed
implement user phone update functionality and refactor related services
1 parent 9f4c7f7 commit fa5077b

File tree

6 files changed

+41
-23
lines changed

6 files changed

+41
-23
lines changed

packages/postgres-database/src/simcore_postgres_database/utils_users.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,23 @@ async def get_user_by_email_or_none(
272272
)
273273
return result.one_or_none()
274274

275+
async def get_user_by_id_or_none(
276+
self, connection: AsyncConnection | None = None, *, user_id: int
277+
) -> Any | None:
278+
async with pass_or_acquire_connection(self._engine, connection) as conn:
279+
result = await conn.execute(
280+
sa.select(
281+
users.c.id,
282+
users.c.name,
283+
users.c.email,
284+
users.c.role,
285+
users.c.status,
286+
users.c.first_name,
287+
users.c.phone,
288+
).where(users.c.id == user_id)
289+
)
290+
return result.one_or_none()
291+
275292
async def update_user_phone(
276293
self, connection: AsyncConnection | None = None, *, user_id: int, phone: str
277294
) -> None:

services/web/server/src/simcore_service_webserver/login/_auth_service.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,22 @@ class UserInfoDict(TypedDict):
2626
phone: str | None
2727

2828

29-
async def get_user_by_email_or_none(
30-
app: web.Application, *, email: str
29+
async def get_user_or_none(
30+
app: web.Application, *, email: str | None = None, user_id: int | None = None
3131
) -> UserInfoDict | None:
32+
if email is None and user_id is None:
33+
msg = "Either email or user_id must be provided"
34+
raise ValueError(msg)
3235

3336
asyncpg_engine = get_asyncpg_engine(app)
3437
repo = UsersRepo(asyncpg_engine)
35-
user_row = await repo.get_user_by_email_or_none(email=email.lower())
38+
39+
if email is not None:
40+
user_row = await repo.get_user_by_email_or_none(email=email.lower())
41+
else:
42+
assert user_id is not None
43+
user_row = await repo.get_user_by_id_or_none(user_id=user_id)
44+
3645
if user_row is None:
3746
return None
3847

services/web/server/src/simcore_service_webserver/login/_controller/rest/auth.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ async def login(request: web.Request):
7373
login_data = await parse_request_body_as(LoginBody, request)
7474

7575
# Authenticate user and verify access to the product
76-
user = await _auth_service.get_user_by_email_or_none(
77-
request.app, email=login_data.email
78-
)
76+
user = await _auth_service.get_user_or_none(request.app, email=login_data.email)
7977

8078
user = _auth_service.check_not_null_user(user)
8179

@@ -235,9 +233,7 @@ async def login_2fa(request: web.Request):
235233
)
236234

237235
user = _auth_service.check_not_null_user(
238-
await _auth_service.get_user_by_email_or_none(
239-
request.app, email=login_2fa_.email
240-
)
236+
await _auth_service.get_user_or_none(request.app, email=login_2fa_.email)
241237
)
242238

243239
# NOTE: a priviledge user should not have called this entrypoint

services/web/server/src/simcore_service_webserver/login/_controller/rest/change.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from ....utils import HOUR
1717
from ....utils_rate_limiting import global_rate_limit_route
1818
from ....web_utils import flash_response
19-
from ... import _confirmation_service, _confirmation_web
19+
from ... import _auth_service, _confirmation_service, _confirmation_web
2020
from ..._emails_service import get_template_path, send_email_from_template
2121
from ..._login_repository_legacy import AsyncpgStorage, get_plugin_storage
2222
from ..._login_service import (
@@ -30,7 +30,6 @@
3030
MSG_EMAIL_SENT,
3131
MSG_OFTEN_RESET_PASSWORD,
3232
MSG_PASSWORD_CHANGED,
33-
MSG_WRONG_PASSWORD,
3433
)
3534
from ...decorators import login_required
3635
from ...settings import LoginOptions, get_plugin_options
@@ -272,13 +271,14 @@ async def change_password(request: web.Request):
272271
db: AsyncpgStorage = get_plugin_storage(request.app)
273272
passwords = await parse_request_body_as(ChangePasswordBody, request)
274273

275-
user = await db.get_user({"id": request[RQT_USERID_KEY]})
276-
assert user # nosec
274+
user = await _auth_service.get_user_or_none(request.app, user_id=user["id"])
277275

278-
if not security_service.check_password(
279-
passwords.current.get_secret_value(), user["password_hash"]
280-
):
281-
raise web.HTTPUnprocessableEntity(text=MSG_WRONG_PASSWORD) # 422
276+
await _auth_service.check_authorized_user_credentials_or_raise(
277+
request.app,
278+
user=user,
279+
password=passwords.current.get_secret_value(),
280+
product=products_web.get_current_product(request),
281+
)
282282

283283
await db.update_user(
284284
dict(user),

services/web/server/src/simcore_service_webserver/login/_controller/rest/confirmation.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,7 @@ async def phone_confirmation(request: web.Request):
227227
await _twofa_service.delete_2fa_code(request.app, request_body.email)
228228

229229
user = _auth_service.check_not_null_user(
230-
await _auth_service.get_user_by_email_or_none(
231-
request.app, email=request_body.email
232-
)
230+
await _auth_service.get_user_or_none(request.app, email=request_body.email)
233231
)
234232

235233
await _registration_service.register_user_phone(

services/web/server/src/simcore_service_webserver/login/_controller/rest/registration.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ async def register(request: web.Request):
185185
).replace(tzinfo=None)
186186

187187
# get authorized user or create new
188-
user = await _auth_service.get_user_by_email_or_none(
189-
request.app, email=registration.email
190-
)
188+
user = await _auth_service.get_user_or_none(request.app, email=registration.email)
191189
if user:
192190
await _auth_service.check_authorized_user_credentials_or_raise(
193191
request.app,

0 commit comments

Comments
 (0)