1+ """
2+ This module provides utility functions for managing access between interconnected accounts.
3+
4+ Functions:
5+ - choose_shared_account(None) -> None - select account from the list of client and organization accounts interactively
6+ - choose_shared_account(email: str, optional) -> None - select account matching the provided email (if exists)
7+ - shared_account_info(None) -> str - return current shared account email address (if exists, None otherwise)
8+ - leave_shared_account(None) -> None - leave current shared account
9+ """
10+
111from requests import HTTPError
212
313from flow360 .cloud .http_util import http
414from flow360 .environment import Env
15+ from flow360 .log import log
516
617from .exceptions import WebError
718
8- """
9- This module provides utility functions for managing access between interconnected accounts.
10-
11- Functions:
12- - choose_account(None) -> None - select account from the list of supported and organization accounts interactively
13- - choose_account(email: str, optional) -> None - select account matching the provided email (if exists)
14- """
15-
1619
1720class AccountsUtils :
21+ """
22+ Current account info and utility functions.
23+ """
24+
1825 def __init__ (self ):
1926 self ._current_email = None
2027 self ._current_user_identity = None
@@ -37,11 +44,10 @@ def _interactive_selection(users):
3744 )
3845 if value == "q" :
3946 return None
40- elif int (value ) in range (0 , user_count ):
47+ if int (value ) in range (0 , user_count ):
4148 return int (value )
42- else :
43- print (f"Value out of range [0 - { user_count - 1 } ]" )
44- continue
49+ print (f"Value out of range [0 - { user_count - 1 } ]" )
50+ continue
4551 except ValueError :
4652 print ("Invalid input type, please input an integer value:" )
4753 continue
@@ -71,11 +77,32 @@ def _get_company_users():
7177 except HTTPError as error :
7278 raise WebError ("Failed to retrieve company user data from server" ) from error
7379
80+ def _check_state_consistency (self ):
81+ if Env .impersonate != self ._current_user_identity :
82+ log .warning (
83+ (
84+ f"Environment impersonation ({ Env .impersonate } ) does "
85+ f"not match current account ({ self ._current_user_identity } ), "
86+ "this may be caused by explicit modification of impersonation "
87+ "in the environment, use choose_shared_account() instead."
88+ )
89+ )
90+ self ._current_email = None
91+ self ._current_user_identity = None
92+
7493 def choose_shared_account (self , email = None ):
94+ """choose a shared account to impersonate
95+
96+ Parameters
97+ ----------
98+ email : str, optional
99+ user email to impersonate (if email exists among shared accounts),
100+ if email is not provided user can select the account interactively
101+ """
75102 company_users = self ._get_company_users ()
76103
77104 if len (company_users ) == 0 :
78- print ("There are no accounts shared with the current user" )
105+ log . info ("There are no accounts shared with the current user" )
79106 return
80107
81108 selected = None
@@ -99,17 +126,31 @@ def choose_shared_account(self, email=None):
99126 self ._current_user_identity = user_id
100127
101128 def shared_account_info (self ):
102- # Impersonate has been modified from the outside, discard current state
103- if Env . impersonate != self . _current_user_identity :
104- self . _current_email = None
105- self ._current_user_identity = None
129+ """
130+ retrieve current shared account name, if possible
131+ """
132+ self ._check_state_consistency ()
106133
107134 if self ._current_email is not None :
108- print (f"Currently operating as { self ._current_email } " )
135+ log . info (f"Currently operating as { self ._current_email } " )
109136 else :
110- print ("Currently not logged into a shared account" )
137+ log . info ("Currently not logged into a shared account" )
111138
112139 return self ._current_email
113140
141+ def leave_shared_account (self ):
142+ """
143+ leave current shared account name, if possible
144+ """
145+ self ._check_state_consistency ()
146+
147+ if Env .impersonate is None :
148+ log .warning ("You are not currently logged into any shared account" )
149+ else :
150+ log .info (f"Leaving shared account { self ._current_email } " )
151+ self ._current_email = None
152+ self ._current_user_identity = None
153+ Env .impersonate = None
154+
114155
115156Accounts = AccountsUtils ()
0 commit comments