From 68a54c34f3d9284a1bf0cfe676910d3bd50feede Mon Sep 17 00:00:00 2001 From: allegroai <> Date: Fri, 8 Jul 2022 17:59:45 +0300 Subject: [PATCH] Add user creation time to users.get_current_user --- apiserver/schema/services/users.conf | 19 ++++++++--- apiserver/services/users.py | 49 ++++++++++++++++------------ 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/apiserver/schema/services/users.conf b/apiserver/schema/services/users.conf index eed4313..dd3c37f 100644 --- a/apiserver/schema/services/users.conf +++ b/apiserver/schema/services/users.conf @@ -139,11 +139,20 @@ get_current_user { } } } - "999.0": ${get_current_user."2.1"} { - response.properties.getting_started { - type: object - description: Getting stated info - additionalProperties: true + "2.20": ${get_current_user."2.1"} { + response { + properties { + getting_started { + type: object + description: Getting stated info + additionalProperties: true + } + created { + type: string + description: User creation time + format: date-time + } + } } } } diff --git a/apiserver/services/users.py b/apiserver/services/users.py index 57552e4..ef33bc5 100644 --- a/apiserver/services/users.py +++ b/apiserver/services/users.py @@ -12,7 +12,7 @@ from apiserver.bll.project import ProjectBLL from apiserver.bll.user import UserBLL from apiserver.config_repo import config from apiserver.database.errors import translate_errors_context -from apiserver.database.model.auth import Role +from apiserver.database.model.auth import Role, User as AuthUser from apiserver.database.model.company import Company from apiserver.database.model.user import User from apiserver.database.utils import parse_from_call @@ -95,29 +95,36 @@ def get_all(call: APICall, company_id, _): @endpoint("users.get_current_user") def get_current_user(call: APICall, company_id, _): - with translate_errors_context("retrieving users"): - projection = ( - {"company.name"} - .union(User.get_fields()) - .difference(User.get_exclude_fields()) - ) - res = User.get_many_with_join( - query=Q(id=call.identity.user), - company=company_id, - override_projection=projection, - ) + user_id = call.identity.user - if not res: - raise errors.bad_request.InvalidUser("failed loading user") + projection = ( + {"company.name"} + .union(User.get_fields()) + .difference(User.get_exclude_fields()) + ) + res = User.get_many_with_join( + query=Q(id=user_id), + company=company_id, + override_projection=projection, + ) - user = res[0] - user["role"] = call.identity.role + if not res: + raise errors.bad_request.InvalidUser("failed loading user") - resp = { - "user": user, - "getting_started": config.get("apiserver.getting_started_info", None), - } - call.result.data = resp + user = res[0] + user["role"] = call.identity.role + + auth_user: AuthUser = AuthUser.objects(id=user_id, company=company_id).first() + if not auth_user: + raise errors.bad_request.InvalidUser("failed loading user") + + user["created"] = auth_user.created + + resp = { + "user": user, + "getting_started": config.get("apiserver.getting_started_info", None), + } + call.result.data = resp create_fields = {