Skip to content

Commit 1318951

Browse files
authored
Merge pull request #34 from bugout-dev/fix-app-users-query
fixed app id check for get user action
2 parents 6faacb0 + b6e8461 commit 1318951

3 files changed

Lines changed: 21 additions & 11 deletions

File tree

brood/actions.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def get_user(
483483
"In order to get user, at least one of username, email, or user_id must be specified"
484484
)
485485

486-
query = session.query(User)
486+
query = session.query(User).filter(User.application_id == application_id)
487487

488488
if username is not None:
489489
username = cast(str, username)
@@ -498,16 +498,17 @@ def get_user(
498498
if user_id is not None:
499499
query = query.filter(User.id == user_id)
500500

501-
if application_id is not None:
502-
query = query.filter(User.application_id == application_id)
503-
504-
user = query.first()
505-
if user is None:
501+
users = query.all()
502+
if len(users) == 0:
506503
raise UserNotFound(
507-
f"Did not find user with filters username={username}, normalized_email={normalized_email}"
504+
f"Did not find user with filters username={username}, normalized_email={normalized_email}, id={user_id} and application_id={application_id}"
505+
)
506+
if len(users) > 1:
507+
raise Exception(
508+
f"Too many results for user with filters username={username}, normalized_email={normalized_email}, id={user_id} and application_id={application_id}"
508509
)
509510

510-
return user
511+
return users[0]
511512

512513

513514
def update_user(

brood/api.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,17 @@ async def get_user_handler(
371371
"""
372372
Get current user.
373373
"""
374-
user_id = current_user.id
375-
user = actions.get_user(session=db_session, user_id=user_id)
374+
try:
375+
user = actions.get_user(
376+
session=db_session,
377+
user_id=current_user.id,
378+
application_id=current_user.application_id,
379+
)
380+
except actions.UserNotFound:
381+
raise HTTPException(status_code=404, detail="User not found")
382+
except Exception:
383+
logger.error("Unhandled error")
384+
raise HTTPException(status_code=500)
376385

377386
return user
378387

brood/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
Brood library and API version.
33
"""
44

5-
BROOD_VERSION = "0.2.2"
5+
BROOD_VERSION = "0.2.3"

0 commit comments

Comments
 (0)