Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend userprofile endpoint with all data on the current user from Jupyter Server #510

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions cylc/uiserver/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
)

from cylc.uiserver.authorise import Authorization, AuthorizationMiddleware
from cylc.uiserver.resolvers import Resolvers
from cylc.uiserver.websockets import authenticated as websockets_authenticated
from cylc.uiserver.websockets.tornado import TornadoSubscriptionServer
if TYPE_CHECKING:
from cylc.uiserver.resolvers import Resolvers
from cylc.uiserver.websockets.tornado import TornadoSubscriptionServer
from graphql.execution import ExecutionResult


Expand Down Expand Up @@ -122,25 +122,34 @@ def _authorise(
return False


def get_usernames(handler: 'CylcAppHandler'):
def get_initials(username: str):
if ('.' in username):
first_inital = username.split('.')[0][0].upper()
last_initial = username.split('.')[1][0].upper()
return first_inital + last_initial
markgrahamdawson marked this conversation as resolved.
Show resolved Hide resolved
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
if ('.' in ME):
first_inital = ME.split('.')[0][0].upper()
last_initial = ME.split('.')[1][0].upper()
initials = first_inital + last_initial
else:
initials = ME[0].upper()
return {'name': ME, 'initials': initials, 'username': ME}
return {'name': ME, 'initials': get_initials(ME), 'username': ME}
else:
if (handler.current_user.initials):
initials = handler.current_user.initials
else:
initials = get_initials(handler.current_user.username)
markgrahamdawson marked this conversation as resolved.
Show resolved Hide resolved
return {
'name': handler.current_user.username,
'initials': '',
'name': handler.current_user.name,
'initials': initials,
'username': handler.current_user.username
}

Expand Down Expand Up @@ -258,7 +267,7 @@ def set_default_headers(self) -> None:
def get(self):
user_info = {
**self.current_user.__dict__,
**get_usernames(self)
**get_user_info(self)
}

# add an entry for the workflow owner
Expand Down Expand Up @@ -327,7 +336,7 @@ def context(self):
'graphql_params': self.graphql_params,
'request': self.request,
'resolvers': self.resolvers,
'current_user': get_usernames(self)['username'],
'current_user': get_user_info(self)['username'],
}

@web.authenticated # type: ignore[arg-type]
Expand Down Expand Up @@ -395,7 +404,7 @@ def context(self):
return {
'request': self.request,
'resolvers': self.resolvers,
'current_user': get_usernames(self)['username'],
'current_user': get_user_info(self)['username'],
'ops_queue': {},
'sub_statuses': self.sub_statuses
}