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

Show logged in user in header #244

Merged
merged 6 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,5 @@ Please add a _short_ line describing the PR you make, if the PR implements a spe
## Sprint (2022-02-09 - 2022-02-23)
* Add `dds project access fix` command for reseting user access when reset password ([236](https://github.com/ScilifelabDataCentre/dds_cli/pull/236))
* Save failed files to log and print out help message after ([237](https://github.com/ScilifelabDataCentre/dds_cli/pull/237))
* Change `--is_sensitive` to `--non-sensitive` ([#246](https://github.com/ScilifelabDataCentre/dds_cli/pull/246))
* Change `--is_sensitive` to `--non-sensitive` ([#246](https://github.com/ScilifelabDataCentre/dds_cli/pull/246))
* Display logged in user in header ([244](https://github.com/ScilifelabDataCentre/dds_cli/pull/244))
6 changes: 5 additions & 1 deletion dds_cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import dds_cli.project_creator
import dds_cli.auth
import dds_cli.project_status
import dds_cli.user
import dds_cli.utils
from dds_cli.options import (
email_arg,
Expand Down Expand Up @@ -71,6 +72,8 @@
# #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ##

# Get token metadata
username = dds_cli.user.User.get_user_name_if_logged_in()

# Print header to STDERR
dds_cli.utils.stderr_console.print(
Expand All @@ -79,7 +82,8 @@
"\n[green]( ) ) ( ( )[/] [bold]SciLifeLab Data Delivery System",
"\n[green] ︶ ( ) ) ([/] [blue][link={0}]{0}[/link]".format(dds_cli.__url__),
f"\n[green] ︶ ( )[/] [dim]Version {dds_cli.__version__}",
"\n[green] ︶\n",
"\n[green] ︶",
f"\n[green]Current user:[/] [red]{username}" if username else "",
highlight=False,
)

Expand Down
19 changes: 19 additions & 0 deletions dds_cli/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,25 @@ def __authenticate_user(self):

return token

@staticmethod
def get_user_name_if_logged_in():
"""Returns a user name if logged in, otherwise None"""
tokenfile = TokenFile()
username = None
if tokenfile.file_exists() and not tokenfile.token_expired():
token, _ = tokenfile.read_token()
try:
response = requests.get(
dds_cli.DDSEndpoint.DISPLAY_USER_INFO,
headers={"Authorization": f"Bearer {token}"},
)
# Get response
response_json = response.json()
username = response_json["info"]["username"]
except:
pass
return username


class TokenFile:
"""A class to manage the saved token."""
Expand Down