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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to