You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2022/05/10 12:21:09 UTC

[GitHub] [superset] dpgaspar commented on a diff in pull request #19914: fix: Refactor SQL engine username logic

dpgaspar commented on code in PR #19914:
URL: https://github.com/apache/superset/pull/19914#discussion_r869167018


##########
superset/utils/core.py:
##########
@@ -1400,6 +1401,30 @@ def get_username() -> Optional[str]:
         return None
 
 
+@contextmanager
+def override_user(user: Optional[User]) -> Iterator[Any]:
+    """
+    Temporarily override the current user (if defined) per `flask.g`.
+
+    Sometimes, often in the context of async Celery tasks, it is useful to switch the
+    current user (which may be undefined) to different one, execute some SQLAlchemy
+    tasks and then revert back to the original one.
+
+    :param user: The override user
+    """
+
+    # pylint: disable=assigning-non-slot
+    if hasattr(g, "user"):
+        current = g.user
+        g.user = user
+        yield
+        g.user = current
+    else:
+        g.user = user
+        yield
+        delattr(g, "user")

Review Comment:
   `g` is thread safe, it's created here: https://github.com/pallets/flask/blob/bd56d19b167822a9a23e2e9e2a07ccccc36baa8d/src/flask/globals.py#L59
   
   Uses werkzeug Local: https://werkzeug.palletsprojects.com/en/1.0.x/local/#module-werkzeug.local
   



##########
superset/utils/core.py:
##########
@@ -1400,6 +1401,30 @@ def get_username() -> Optional[str]:
         return None
 
 
+@contextmanager
+def override_user(user: Optional[User]) -> Iterator[Any]:

Review Comment:
   should it be `def override_user(user: Optional[User]) -> Iterator[None]:` ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org