Skip to content

Commit

Permalink
Extend userprofile endpoint with all data on the current user from Ju…
Browse files Browse the repository at this point in the history
…pyter Server (cylc#510)

Extend userprofile endpoint with all data on the current user from Jupyter Server
  • Loading branch information
markgrahamdawson authored and MetRonnie committed Nov 2, 2023
1 parent 5965809 commit 255ca79
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions cylc/uiserver/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,34 @@ 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
account that this server instance is running under.
"""
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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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
}

0 comments on commit 255ca79

Please sign in to comment.