feat!: pass datasource_type and datasource_id to form_data (#19981)

* pass datasource_type and datasource_id to form_data

* add datasource_type to delete command

* add datasource_type to delete command

* fix old keys implementation

* add more tests
This commit is contained in:
Elizabeth Thompson
2022-06-02 16:48:16 -07:00
committed by GitHub
parent a813528958
commit 32bb1ce3ff
47 changed files with 959 additions and 176 deletions

View File

@@ -39,20 +39,24 @@ class CreateFormDataCommand(BaseCommand):
def run(self) -> str:
self.validate()
try:
dataset_id = self._cmd_params.dataset_id
datasource_id = self._cmd_params.datasource_id
datasource_type = self._cmd_params.datasource_type
chart_id = self._cmd_params.chart_id
tab_id = self._cmd_params.tab_id
actor = self._cmd_params.actor
form_data = self._cmd_params.form_data
check_access(dataset_id, chart_id, actor)
contextual_key = cache_key(session.get("_id"), tab_id, dataset_id, chart_id)
check_access(datasource_id, chart_id, actor, datasource_type)
contextual_key = cache_key(
session.get("_id"), tab_id, datasource_id, chart_id, datasource_type
)
key = cache_manager.explore_form_data_cache.get(contextual_key)
if not key or not tab_id:
key = random_key()
if form_data:
state: TemporaryExploreState = {
"owner": get_owner(actor),
"dataset_id": dataset_id,
"datasource_id": datasource_id,
"datasource_type": datasource_type,
"chart_id": chart_id,
"form_data": form_data,
}

View File

@@ -16,6 +16,7 @@
# under the License.
import logging
from abc import ABC
from typing import Optional
from flask import session
from sqlalchemy.exc import SQLAlchemyError
@@ -31,6 +32,7 @@ from superset.temporary_cache.commands.exceptions import (
TemporaryCacheDeleteFailedError,
)
from superset.temporary_cache.utils import cache_key
from superset.utils.core import DatasourceType
logger = logging.getLogger(__name__)
@@ -47,14 +49,15 @@ class DeleteFormDataCommand(BaseCommand, ABC):
key
)
if state:
dataset_id = state["dataset_id"]
chart_id = state["chart_id"]
check_access(dataset_id, chart_id, actor)
datasource_id: int = state["datasource_id"]
chart_id: Optional[int] = state["chart_id"]
datasource_type = DatasourceType(state["datasource_type"])
check_access(datasource_id, chart_id, actor, datasource_type)
if state["owner"] != get_owner(actor):
raise TemporaryCacheAccessDeniedError()
tab_id = self._cmd_params.tab_id
contextual_key = cache_key(
session.get("_id"), tab_id, dataset_id, chart_id
session.get("_id"), tab_id, datasource_id, chart_id, datasource_type
)
cache_manager.explore_form_data_cache.delete(contextual_key)
return cache_manager.explore_form_data_cache.delete(key)

View File

@@ -27,6 +27,7 @@ from superset.explore.form_data.commands.state import TemporaryExploreState
from superset.explore.form_data.commands.utils import check_access
from superset.extensions import cache_manager
from superset.temporary_cache.commands.exceptions import TemporaryCacheGetFailedError
from superset.utils.core import DatasourceType
logger = logging.getLogger(__name__)
@@ -45,7 +46,12 @@ class GetFormDataCommand(BaseCommand, ABC):
key
)
if state:
check_access(state["dataset_id"], state["chart_id"], actor)
check_access(
state["datasource_id"],
state["chart_id"],
actor,
DatasourceType(state["datasource_type"]),
)
if self._refresh_timeout:
cache_manager.explore_form_data_cache.set(key, state)
return state["form_data"]

View File

@@ -19,11 +19,14 @@ from typing import Optional
from flask_appbuilder.security.sqla.models import User
from superset.utils.core import DatasourceType
@dataclass
class CommandParameters:
actor: User
dataset_id: int = 0
datasource_type: DatasourceType = DatasourceType.TABLE
datasource_id: int = 0
chart_id: int = 0
tab_id: Optional[int] = None
key: Optional[str] = None

View File

@@ -21,6 +21,7 @@ from typing_extensions import TypedDict
class TemporaryExploreState(TypedDict):
owner: Optional[int]
dataset_id: int
datasource_id: int
datasource_type: str
chart_id: Optional[int]
form_data: str

View File

@@ -47,12 +47,13 @@ class UpdateFormDataCommand(BaseCommand, ABC):
def run(self) -> Optional[str]:
self.validate()
try:
dataset_id = self._cmd_params.dataset_id
datasource_id = self._cmd_params.datasource_id
chart_id = self._cmd_params.chart_id
datasource_type = self._cmd_params.datasource_type
actor = self._cmd_params.actor
key = self._cmd_params.key
form_data = self._cmd_params.form_data
check_access(dataset_id, chart_id, actor)
check_access(datasource_id, chart_id, actor, datasource_type)
state: TemporaryExploreState = cache_manager.explore_form_data_cache.get(
key
)
@@ -64,7 +65,7 @@ class UpdateFormDataCommand(BaseCommand, ABC):
# Generate a new key if tab_id changes or equals 0
tab_id = self._cmd_params.tab_id
contextual_key = cache_key(
session.get("_id"), tab_id, dataset_id, chart_id
session.get("_id"), tab_id, datasource_id, chart_id, datasource_type
)
key = cache_manager.explore_form_data_cache.get(contextual_key)
if not key or not tab_id:
@@ -73,7 +74,8 @@ class UpdateFormDataCommand(BaseCommand, ABC):
new_state: TemporaryExploreState = {
"owner": owner,
"dataset_id": dataset_id,
"datasource_id": datasource_id,
"datasource_type": datasource_type,
"chart_id": chart_id,
"form_data": form_data,
}

View File

@@ -31,11 +31,17 @@ from superset.temporary_cache.commands.exceptions import (
TemporaryCacheAccessDeniedError,
TemporaryCacheResourceNotFoundError,
)
from superset.utils.core import DatasourceType
def check_access(dataset_id: int, chart_id: Optional[int], actor: User) -> None:
def check_access(
datasource_id: int,
chart_id: Optional[int],
actor: User,
datasource_type: DatasourceType,
) -> None:
try:
explore_check_access(dataset_id, chart_id, actor)
explore_check_access(datasource_id, chart_id, actor, datasource_type)
except (ChartNotFoundError, DatasetNotFoundError) as ex:
raise TemporaryCacheResourceNotFoundError from ex
except (ChartAccessDeniedError, DatasetAccessDeniedError) as ex: