From 255ca79b74178a96a592f28fda2b022b8d56c3bc Mon Sep 17 00:00:00 2001 From: Mark Dawson Date: Wed, 25 Oct 2023 13:04:29 +0100 Subject: [PATCH] Extend userprofile endpoint with all data on the current user from Jupyter Server (#510) Extend userprofile endpoint with all data on the current user from Jupyter Server --- cylc/uiserver/handlers.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/cylc/uiserver/handlers.py b/cylc/uiserver/handlers.py index 0ec9341e..daa6930f 100644 --- a/cylc/uiserver/handlers.py +++ b/cylc/uiserver/handlers.py @@ -122,7 +122,17 @@ def _authorise( return False -def get_username(handler: 'CylcAppHandler'): +def get_initials(username: str): + if ('.' in username): + first, last = username.split('.', maxsplit=1) + return f"{first[0]}{last[0]}".upper() + elif (username != ''): + return username[0].upper() + else: + return None + + +def get_user_info(handler: 'CylcAppHandler'): """Return the username for the authenticated user. If the handler is token authenticated, then we return the username of the @@ -130,9 +140,16 @@ def get_username(handler: 'CylcAppHandler'): """ if is_token_authenticated(handler): # the bearer of the token has full privileges - return ME + return {'name': ME, 'initials': get_initials(ME), 'username': ME} else: - return handler.current_user.username + initials = handler.current_user.initials or get_initials( + handler.current_user.username + ) + return { + 'name': handler.current_user.name, + 'initials': initials, + 'username': handler.current_user.username + } class CylcAppHandler(JupyterHandler): @@ -247,7 +264,8 @@ def set_default_headers(self) -> None: # @authorised TODO: I can't think why we would want to authorise this def get(self): user_info = { - 'name': get_username(self) + **self.current_user.__dict__, + **get_user_info(self) } # add an entry for the workflow owner @@ -316,7 +334,7 @@ def context(self): 'graphql_params': self.graphql_params, 'request': self.request, 'resolvers': self.resolvers, - 'current_user': get_username(self), + 'current_user': get_user_info(self)['username'], } @web.authenticated # type: ignore[arg-type] @@ -384,7 +402,7 @@ def context(self): return { 'request': self.request, 'resolvers': self.resolvers, - 'current_user': get_username(self), + 'current_user': get_user_info(self)['username'], 'ops_queue': {}, 'sub_statuses': self.sub_statuses }