diff --git a/CHANGELOG.md b/CHANGELOG.md index 08756fea2..9bef83224 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) \ No newline at end of file +* 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)) \ No newline at end of file diff --git a/dds_cli/__main__.py b/dds_cli/__main__.py index c073f794c..68263052d 100644 --- a/dds_cli/__main__.py +++ b/dds_cli/__main__.py @@ -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, @@ -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( @@ -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, ) diff --git a/dds_cli/user.py b/dds_cli/user.py index b0653595d..6afd1b753 100644 --- a/dds_cli/user.py +++ b/dds_cli/user.py @@ -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."""