mirror of
https://github.com/apache/superset.git
synced 2024-09-12 00:29:39 -04:00
fix: load examples as anon user (#23600)
This commit is contained in:
parent
4c44dd84d2
commit
f78798149a
@ -27,9 +27,12 @@ from superset.models.slice import Slice
|
|||||||
|
|
||||||
|
|
||||||
def import_chart(
|
def import_chart(
|
||||||
session: Session, config: Dict[str, Any], overwrite: bool = False
|
session: Session,
|
||||||
|
config: Dict[str, Any],
|
||||||
|
overwrite: bool = False,
|
||||||
|
ignore_permissions: bool = False,
|
||||||
) -> Slice:
|
) -> Slice:
|
||||||
can_write = security_manager.can_access("can_write", "Chart")
|
can_write = ignore_permissions or security_manager.can_access("can_write", "Chart")
|
||||||
existing = session.query(Slice).filter_by(uuid=config["uuid"]).first()
|
existing = session.query(Slice).filter_by(uuid=config["uuid"]).first()
|
||||||
if existing:
|
if existing:
|
||||||
if not overwrite or not can_write:
|
if not overwrite or not can_write:
|
||||||
|
@ -21,7 +21,7 @@ from sqlalchemy.orm import Session
|
|||||||
from sqlalchemy.orm.exc import MultipleResultsFound
|
from sqlalchemy.orm.exc import MultipleResultsFound
|
||||||
from sqlalchemy.sql import select
|
from sqlalchemy.sql import select
|
||||||
|
|
||||||
from superset import db, security_manager
|
from superset import db
|
||||||
from superset.charts.commands.importers.v1 import ImportChartsCommand
|
from superset.charts.commands.importers.v1 import ImportChartsCommand
|
||||||
from superset.charts.commands.importers.v1.utils import import_chart
|
from superset.charts.commands.importers.v1.utils import import_chart
|
||||||
from superset.charts.schemas import ImportV1ChartSchema
|
from superset.charts.schemas import ImportV1ChartSchema
|
||||||
@ -42,7 +42,7 @@ from superset.datasets.commands.importers.v1 import ImportDatasetsCommand
|
|||||||
from superset.datasets.commands.importers.v1.utils import import_dataset
|
from superset.datasets.commands.importers.v1.utils import import_dataset
|
||||||
from superset.datasets.schemas import ImportV1DatasetSchema
|
from superset.datasets.schemas import ImportV1DatasetSchema
|
||||||
from superset.models.dashboard import dashboard_slices
|
from superset.models.dashboard import dashboard_slices
|
||||||
from superset.utils.core import get_example_default_schema, override_user
|
from superset.utils.core import get_example_default_schema
|
||||||
from superset.utils.database import get_example_database
|
from superset.utils.database import get_example_database
|
||||||
|
|
||||||
|
|
||||||
@ -69,13 +69,12 @@ class ImportExamplesCommand(ImportModelsCommand):
|
|||||||
|
|
||||||
# rollback to prevent partial imports
|
# rollback to prevent partial imports
|
||||||
try:
|
try:
|
||||||
with override_user(security_manager.find_user(username="admin")):
|
self._import(
|
||||||
self._import(
|
db.session,
|
||||||
db.session,
|
self._configs,
|
||||||
self._configs,
|
self.overwrite,
|
||||||
self.overwrite,
|
self.force_data,
|
||||||
self.force_data,
|
)
|
||||||
)
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
db.session.rollback()
|
db.session.rollback()
|
||||||
@ -102,7 +101,12 @@ class ImportExamplesCommand(ImportModelsCommand):
|
|||||||
database_ids: Dict[str, int] = {}
|
database_ids: Dict[str, int] = {}
|
||||||
for file_name, config in configs.items():
|
for file_name, config in configs.items():
|
||||||
if file_name.startswith("databases/"):
|
if file_name.startswith("databases/"):
|
||||||
database = import_database(session, config, overwrite=overwrite)
|
database = import_database(
|
||||||
|
session,
|
||||||
|
config,
|
||||||
|
overwrite=overwrite,
|
||||||
|
ignore_permissions=True,
|
||||||
|
)
|
||||||
database_ids[str(database.uuid)] = database.id
|
database_ids[str(database.uuid)] = database.id
|
||||||
|
|
||||||
# import datasets
|
# import datasets
|
||||||
@ -131,9 +135,10 @@ class ImportExamplesCommand(ImportModelsCommand):
|
|||||||
config,
|
config,
|
||||||
overwrite=overwrite,
|
overwrite=overwrite,
|
||||||
force_data=force_data,
|
force_data=force_data,
|
||||||
|
ignore_permissions=True,
|
||||||
)
|
)
|
||||||
except MultipleResultsFound:
|
except MultipleResultsFound:
|
||||||
# Multiple result can be found for datasets. There was a bug in
|
# Multiple results can be found for datasets. There was a bug in
|
||||||
# load-examples that resulted in datasets being loaded with a NULL
|
# load-examples that resulted in datasets being loaded with a NULL
|
||||||
# schema. Users could then add a new dataset with the same name in
|
# schema. Users could then add a new dataset with the same name in
|
||||||
# the correct schema, resulting in duplicates, since the uniqueness
|
# the correct schema, resulting in duplicates, since the uniqueness
|
||||||
@ -156,7 +161,12 @@ class ImportExamplesCommand(ImportModelsCommand):
|
|||||||
):
|
):
|
||||||
# update datasource id, type, and name
|
# update datasource id, type, and name
|
||||||
config.update(dataset_info[config["dataset_uuid"]])
|
config.update(dataset_info[config["dataset_uuid"]])
|
||||||
chart = import_chart(session, config, overwrite=overwrite)
|
chart = import_chart(
|
||||||
|
session,
|
||||||
|
config,
|
||||||
|
overwrite=overwrite,
|
||||||
|
ignore_permissions=True,
|
||||||
|
)
|
||||||
chart_ids[str(chart.uuid)] = chart.id
|
chart_ids[str(chart.uuid)] = chart.id
|
||||||
|
|
||||||
# store the existing relationship between dashboards and charts
|
# store the existing relationship between dashboards and charts
|
||||||
@ -173,7 +183,12 @@ class ImportExamplesCommand(ImportModelsCommand):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
dashboard = import_dashboard(session, config, overwrite=overwrite)
|
dashboard = import_dashboard(
|
||||||
|
session,
|
||||||
|
config,
|
||||||
|
overwrite=overwrite,
|
||||||
|
ignore_permissions=True,
|
||||||
|
)
|
||||||
dashboard.published = True
|
dashboard.published = True
|
||||||
|
|
||||||
for uuid in find_chart_uuids(config["position"]):
|
for uuid in find_chart_uuids(config["position"]):
|
||||||
|
@ -146,9 +146,15 @@ def update_id_refs( # pylint: disable=too-many-locals
|
|||||||
|
|
||||||
|
|
||||||
def import_dashboard(
|
def import_dashboard(
|
||||||
session: Session, config: Dict[str, Any], overwrite: bool = False
|
session: Session,
|
||||||
|
config: Dict[str, Any],
|
||||||
|
overwrite: bool = False,
|
||||||
|
ignore_permissions: bool = False,
|
||||||
) -> Dashboard:
|
) -> Dashboard:
|
||||||
can_write = security_manager.can_access("can_write", "Dashboard")
|
can_write = ignore_permissions or security_manager.can_access(
|
||||||
|
"can_write",
|
||||||
|
"Dashboard",
|
||||||
|
)
|
||||||
existing = session.query(Dashboard).filter_by(uuid=config["uuid"]).first()
|
existing = session.query(Dashboard).filter_by(uuid=config["uuid"]).first()
|
||||||
if existing:
|
if existing:
|
||||||
if not overwrite or not can_write:
|
if not overwrite or not can_write:
|
||||||
|
@ -30,8 +30,12 @@ def import_database(
|
|||||||
session: Session,
|
session: Session,
|
||||||
config: Dict[str, Any],
|
config: Dict[str, Any],
|
||||||
overwrite: bool = False,
|
overwrite: bool = False,
|
||||||
|
ignore_permissions: bool = False,
|
||||||
) -> Database:
|
) -> Database:
|
||||||
can_write = security_manager.can_access("can_write", "Database")
|
can_write = ignore_permissions or security_manager.can_access(
|
||||||
|
"can_write",
|
||||||
|
"Database",
|
||||||
|
)
|
||||||
existing = session.query(Database).filter_by(uuid=config["uuid"]).first()
|
existing = session.query(Database).filter_by(uuid=config["uuid"]).first()
|
||||||
if existing:
|
if existing:
|
||||||
if not overwrite or not can_write:
|
if not overwrite or not can_write:
|
||||||
|
@ -105,8 +105,12 @@ def import_dataset(
|
|||||||
config: Dict[str, Any],
|
config: Dict[str, Any],
|
||||||
overwrite: bool = False,
|
overwrite: bool = False,
|
||||||
force_data: bool = False,
|
force_data: bool = False,
|
||||||
|
ignore_permissions: bool = False,
|
||||||
) -> SqlaTable:
|
) -> SqlaTable:
|
||||||
can_write = security_manager.can_access("can_write", "Dataset")
|
can_write = ignore_permissions or security_manager.can_access(
|
||||||
|
"can_write",
|
||||||
|
"Dataset",
|
||||||
|
)
|
||||||
existing = session.query(SqlaTable).filter_by(uuid=config["uuid"]).first()
|
existing = session.query(SqlaTable).filter_by(uuid=config["uuid"]).first()
|
||||||
if existing:
|
if existing:
|
||||||
if not overwrite or not can_write:
|
if not overwrite or not can_write:
|
||||||
|
Loading…
Reference in New Issue
Block a user