1919from marshmallow import Schema
2020from marshmallow .exceptions import ValidationError
2121
22- from superset import db
22+ from superset import db , security_manager
2323from superset .annotation_layers .schemas import ImportV1AnnotationLayerSchema
2424from superset .charts .schemas import ImportV1ChartSchema
2525from superset .commands .annotation_layer .importers .v1 .utils import (
6666 SemanticReferenceError ,
6767)
6868from superset .subjects .utils import get_default_viewers_for_current_user
69+ from superset .utils .core import get_user
6970from superset .utils .decorators import on_error , transaction
7071
7172
@@ -82,6 +83,26 @@ def _on_import_error(ex: Exception) -> None:
8283 on_error (ex , catches = (Exception ,), reraise = ImportFailedError )
8384
8485
86+ def _can_write_dashboard (dashboard : Dashboard ) -> bool :
87+ """Return whether the current caller may modify ``dashboard``.
88+
89+ Mirrors the per-object write check that ``import_dashboard`` applies before
90+ updating a dashboard: the caller needs the ``can_write`` grant on Dashboard
91+ and, for an already-persisted dashboard, must be able to access it and be an
92+ editor (or admin). Background / example-loader paths with no request user
93+ established trust at the command level and are allowed, matching
94+ ``import_dashboard``'s own ``user is None`` short-circuit, and newly created
95+ dashboards pass because the caller is registered as their editor.
96+ """
97+ if not security_manager .can_access ("can_write" , "Dashboard" ):
98+ return False
99+ if get_user () is None :
100+ return True
101+ return security_manager .can_access_dashboard (dashboard ) and (
102+ security_manager .is_editor (dashboard ) or security_manager .is_admin ()
103+ )
104+
105+
85106class ImportAssetsCommand (BaseCommand ):
86107 """
87108 Command for importing databases, datasets, charts, dashboards and saved queries.
@@ -221,38 +242,51 @@ def _import( # noqa: C901
221242 )
222243
223244 # set ref in the dashboard_slices table
224- # Use ORM-level reassignment instead of Core
225- # delete()/insert() so SQLAlchemy-Continuum's M2M tracker
226- # sees per-row changes through the ORM. Bulk DML via Core
227- # would emit a malformed INSERT into
228- # ``dashboard_slices_version`` (missing the composite-PK
229- # columns) — see the parallel rewrite in
230- # ``DatasetDAO.update_columns`` and the test-factory's
231- # ``delete_dashboard_slices_associations`` for the same
232- # reason.
233- slice_ids : list [int ] = []
234- for uuid in find_chart_uuids (config ["position" ]):
235- # Skip charts that weren't part of this import; ``continue``
236- # (not ``break``) so a single missing/unresolved chart uuid
237- # doesn't truncate the rest. ``find_chart_uuids`` returns a
238- # set, so a ``break`` here would drop a non-deterministic
239- # subset of the dashboard's charts.
240- if uuid not in chart_ids :
241- continue
242- slice_ids .append (chart_ids [uuid ])
243-
244- dashboard .slices = (
245- db .session .query (Slice ).filter (Slice .id .in_ (slice_ids )).all ()
246- if slice_ids
247- else []
248- )
249- # Flush eagerly so the M2M rows land in
250- # ``dashboard_slices`` before any subsequent autoflush
251- # fires an inner-flush event handler that would reset
252- # the relationship change (cf. the SAWarning at
253- # ``superset/models/helpers.py`` re. "attribute history
254- # events accumulated ... have been reset").
255- db .session .flush ()
245+ #
246+ # Only reassign the chart associations when the caller is
247+ # permitted to write this dashboard. ``import_dashboard`` applies
248+ # a per-object write check and returns an *existing* dashboard
249+ # unchanged when the caller lacks that permission (no
250+ # ``can_write`` grant, or not an editor/admin of the dashboard);
251+ # rewriting ``dashboard.slices`` here regardless would modify an
252+ # existing dashboard's charts from the bundle position without
253+ # going through the same check. Newly created dashboards (the
254+ # caller is their editor) and dashboards the caller may edit are
255+ # unaffected.
256+ if _can_write_dashboard (dashboard ):
257+ # Use ORM-level reassignment instead of Core
258+ # delete()/insert() so SQLAlchemy-Continuum's M2M tracker
259+ # sees per-row changes through the ORM. Bulk DML via Core
260+ # would emit a malformed INSERT into
261+ # ``dashboard_slices_version`` (missing the composite-PK
262+ # columns) — see the parallel rewrite in
263+ # ``DatasetDAO.update_columns`` and the test-factory's
264+ # ``delete_dashboard_slices_associations`` for the same
265+ # reason.
266+ slice_ids : list [int ] = []
267+ for uuid in find_chart_uuids (config ["position" ]):
268+ # Skip charts that weren't part of this import;
269+ # ``continue`` (not ``break``) so a single
270+ # missing/unresolved chart uuid doesn't truncate the
271+ # rest. ``find_chart_uuids`` returns a set, so a
272+ # ``break`` here would drop a non-deterministic subset of
273+ # the dashboard's charts.
274+ if uuid not in chart_ids :
275+ continue
276+ slice_ids .append (chart_ids [uuid ])
277+
278+ dashboard .slices = (
279+ db .session .query (Slice ).filter (Slice .id .in_ (slice_ids )).all ()
280+ if slice_ids
281+ else []
282+ )
283+ # Flush eagerly so the M2M rows land in
284+ # ``dashboard_slices`` before any subsequent autoflush
285+ # fires an inner-flush event handler that would reset
286+ # the relationship change (cf. the SAWarning at
287+ # ``superset/models/helpers.py`` re. "attribute history
288+ # events accumulated ... have been reset").
289+ db .session .flush ()
256290
257291 # Handle tags using import_tag function
258292 if feature_flag_manager .is_feature_enabled ("TAGGING_SYSTEM" ):
0 commit comments