Add user creation time to users.get_current_user

This commit is contained in:
allegroai 2022-07-08 17:59:45 +03:00
parent ab495ae586
commit 68a54c34f3
2 changed files with 42 additions and 26 deletions

View File

@ -139,11 +139,20 @@ get_current_user {
} }
} }
} }
"999.0": ${get_current_user."2.1"} { "2.20": ${get_current_user."2.1"} {
response.properties.getting_started { response {
type: object properties {
description: Getting stated info getting_started {
additionalProperties: true type: object
description: Getting stated info
additionalProperties: true
}
created {
type: string
description: User creation time
format: date-time
}
}
} }
} }
} }

View File

@ -12,7 +12,7 @@ from apiserver.bll.project import ProjectBLL
from apiserver.bll.user import UserBLL from apiserver.bll.user import UserBLL
from apiserver.config_repo import config from apiserver.config_repo import config
from apiserver.database.errors import translate_errors_context 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.company import Company
from apiserver.database.model.user import User from apiserver.database.model.user import User
from apiserver.database.utils import parse_from_call from apiserver.database.utils import parse_from_call
@ -95,29 +95,36 @@ def get_all(call: APICall, company_id, _):
@endpoint("users.get_current_user") @endpoint("users.get_current_user")
def get_current_user(call: APICall, company_id, _): def get_current_user(call: APICall, company_id, _):
with translate_errors_context("retrieving users"): user_id = call.identity.user
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,
)
if not res: projection = (
raise errors.bad_request.InvalidUser("failed loading user") {"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] if not res:
user["role"] = call.identity.role raise errors.bad_request.InvalidUser("failed loading user")
resp = { user = res[0]
"user": user, user["role"] = call.identity.role
"getting_started": config.get("apiserver.getting_started_info", None),
} auth_user: AuthUser = AuthUser.objects(id=user_id, company=company_id).first()
call.result.data = resp 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 = { create_fields = {