Skip to content

Commit 19bc306

Browse files
fix(reports): keep recipients set by admins on chart and dashboard reports (#46527)
1 parent ffda167 commit 19bc306

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎superset/commands/report/update.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,14 +403,16 @@ def validate(self) -> None: # noqa: C901
403403
self._properties["last_state"] = ReportState.NOOP
404404

405405
# For reports created from charts or dashboards the recipient must always
406-
# be the requesting user's own email address.
406+
# be the requesting user's own email address. Admins can manage these
407+
# reports from the Alerts & Reports list, so keep the recipients they set.
407408
if (
408409
self._model.creation_method
409410
in (
410411
ReportCreationMethod.CHARTS,
411412
ReportCreationMethod.DASHBOARDS,
412413
)
413414
and "recipients" in self._properties
415+
and not security_manager.is_admin()
414416
):
415417
if user_email := get_user_email():
416418
self._properties["recipients"] = [

‎tests/unit_tests/commands/report/update_test.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ def test_report_to_alert_with_db_accepted(mocker: MockerFixture) -> None:
317317
# --- Recipient enforcement for chart/dashboard reports ---
318318

319319
_PATCH_GET_USER_EMAIL = "superset.commands.report.update.get_user_email"
320+
_PATCH_IS_ADMIN = "superset.commands.report.update.security_manager.is_admin"
320321

321322

322323
def test_chart_report_update_recipient_overridden_with_owner_email(
@@ -330,6 +331,7 @@ def test_chart_report_update_recipient_overridden_with_owner_email(
330331
creation_method=ReportCreationMethod.CHARTS,
331332
)
332333
_setup_mocks(mocker, model)
334+
mocker.patch(_PATCH_IS_ADMIN, return_value=False)
333335

334336
data = {
335337
"recipients": [
@@ -359,6 +361,7 @@ def test_dashboard_report_update_recipient_overridden_with_owner_email(
359361
creation_method=ReportCreationMethod.DASHBOARDS,
360362
)
361363
_setup_mocks(mocker, model)
364+
mocker.patch(_PATCH_IS_ADMIN, return_value=False)
362365

363366
data = {
364367
"recipients": [
@@ -377,6 +380,41 @@ def test_dashboard_report_update_recipient_overridden_with_owner_email(
377380
assert recipients[0]["recipient_config_json"]["target"] == "owner@example.com"
378381

379382

383+
@pytest.mark.parametrize(
384+
"creation_method",
385+
[ReportCreationMethod.CHARTS, ReportCreationMethod.DASHBOARDS],
386+
)
387+
def test_chart_dashboard_report_update_recipients_kept_for_admin(
388+
mocker: MockerFixture,
389+
creation_method: ReportCreationMethod,
390+
) -> None:
391+
"""Admins editing a chart/dashboard report keep the recipients they set."""
392+
model = _make_model(
393+
mocker,
394+
model_type=ReportScheduleType.REPORT,
395+
database_id=None,
396+
creation_method=creation_method,
397+
)
398+
_setup_mocks(mocker, model)
399+
mocker.patch(_PATCH_IS_ADMIN, return_value=True)
400+
401+
recipients = [
402+
{
403+
"type": "Email",
404+
"recipient_config_json": {"target": "owner@example.com"},
405+
},
406+
{
407+
"type": "Email",
408+
"recipient_config_json": {"target": "team@example.com"},
409+
},
410+
]
411+
cmd = UpdateReportScheduleCommand(model_id=1, data={"recipients": recipients})
412+
with patch(_PATCH_GET_USER_EMAIL, return_value="admin@example.com"):
413+
cmd.validate()
414+
415+
assert cmd._properties["recipients"] == recipients
416+
417+
380418
def test_alerts_reports_update_recipient_not_overridden(
381419
mocker: MockerFixture,
382420
) -> None:
@@ -433,6 +471,7 @@ def test_chart_report_update_no_user_email_raises(mocker: MockerFixture) -> None
433471
creation_method=ReportCreationMethod.CHARTS,
434472
)
435473
_setup_mocks(mocker, model)
474+
mocker.patch(_PATCH_IS_ADMIN, return_value=False)
436475

437476
cmd = UpdateReportScheduleCommand(
438477
model_id=1,

0 commit comments

Comments
 (0)