Skip to content

Commit

Permalink
pass guess token to load_chart_data task for user context
Browse files Browse the repository at this point in the history
  • Loading branch information
zef committed Dec 22, 2023
1 parent 1df0373 commit 59d8703
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
11 changes: 11 additions & 0 deletions superset/async_events/async_query_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,19 @@ def submit_explore_json_job(
def submit_chart_data_job(
self, channel_id: str, form_data: dict[str, Any], user_id: Optional[int]
) -> dict[str, Any]:
# pylint: disable=import-outside-toplevel
from superset import security_manager

# if it's guest user, we want to pass the guest token to the celery task
# chart data cache key is calculated based on the current user
# this way we can keep the cache key consistent between sync and async command
# so that it can be looked up consistently
job_metadata = self.init_job(channel_id, user_id)
if guest_user := security_manager.get_current_guest_user_if_guest():
job_metadata["guest_token"] = guest_user.guest_token
self._load_chart_data_into_cache_job.delay(job_metadata, form_data)
if "guest_token" in job_metadata:
del job_metadata["guest_token"]
return job_metadata

def read_events(
Expand Down
14 changes: 10 additions & 4 deletions superset/tasks/async_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@ def load_chart_data_into_cache(
# pylint: disable=import-outside-toplevel
from superset.commands.chart.data.get_data_command import ChartDataCommand

user = (
security_manager.get_user_by_id(job_metadata.get("user_id"))
or security_manager.get_anonymous_user()
)
if user_id := job_metadata.get("user_id"):
# logged in user
user = security_manager.get_user_by_id(user_id)
elif guest_token := job_metadata.get("guest_token"):
# embedded guest user
user = security_manager.get_guest_user_from_token(guest_token)
del job_metadata["guest_token"]
else:
# default to anonymous user if no user is found
user = security_manager.get_anonymous_user()

with override_user(user, force=False):
try:
Expand Down

0 comments on commit 59d8703

Please sign in to comment.