Skip to content

Commit

Permalink
fix: add user_id to cache key when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Jun 8, 2022
1 parent d1c24f8 commit f695816
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions superset/connectors/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from enum import Enum
from typing import Any, Dict, Hashable, List, Optional, Set, Type, TYPE_CHECKING, Union

from flask import g
from flask_appbuilder.security.sqla.models import User
from sqlalchemy import and_, Boolean, Column, Integer, String, Text
from sqlalchemy.ext.declarative import declared_attr
Expand Down Expand Up @@ -554,7 +555,7 @@ def update_from_object(self, obj: Dict[str, Any]) -> None:
else []
)

def get_extra_cache_keys( # pylint: disable=no-self-use
def get_extra_cache_keys(
self, query_obj: QueryObjectDict # pylint: disable=unused-argument
) -> List[Hashable]:
"""If a datasource needs to provide additional keys for calculation of
Expand All @@ -563,7 +564,11 @@ def get_extra_cache_keys( # pylint: disable=no-self-use
:param query_obj: The dict representation of a query object
:return: list of keys
"""
return []
# If a database has user impersonation set the exact same query might return
# different results, so we should cache results at the user grain.
user_id = g.user.id if hasattr(g, "user") else None
# pylint: disable=no-member
return [user_id] if user_id and self.database.impersonate_user else []

def __hash__(self) -> int:
return hash(self.uid)
Expand Down

0 comments on commit f695816

Please sign in to comment.