refactor: Cleanup user get_id/get_user_id (#20492)

Co-authored-by: John Bodley <john.bodley@airbnb.com>
This commit is contained in:
John Bodley
2022-06-24 17:57:04 -07:00
committed by GitHub
parent c56e37cda2
commit 3483446c28
27 changed files with 182 additions and 137 deletions

View File

@@ -1422,13 +1422,36 @@ def split_adhoc_filters_into_base_filters( # pylint: disable=invalid-name
def get_username() -> Optional[str]:
"""Get username if within the flask context, otherwise return noffin'"""
"""
Get username (if defined) associated with the current user.
:returns: The username
"""
try:
return g.user.username
except Exception: # pylint: disable=broad-except
return None
def get_user_id() -> Optional[int]:
"""
Get the user identifier (if defined) associated with the current user.
Though the Flask-AppBuilder `User` and Flask-Login `AnonymousUserMixin` and
`UserMixin` models provide a convenience `get_id` method, for generality, the
identifier is encoded as a `str` whereas in Superset all identifiers are encoded as
an `int`.
returns: The user identifier
"""
try:
return g.user.id
except Exception: # pylint: disable=broad-except
return None
@contextmanager
def override_user(user: Optional[User]) -> Iterator[Any]:
"""