LibraryPanels: Prevents deletion of connected library panels (#32277)

* LibraryPanels: Prevents deletion of connected library panels

* Refactor: adds the delete library panel modal

* Chore: updates after PR comments
This commit is contained in:
Hugo Häggmark
2021-03-24 11:43:27 +01:00
committed by GitHub
parent 26823ee438
commit 376ed8a381
13 changed files with 264 additions and 54 deletions

View File

@@ -128,5 +128,8 @@ func toLibraryPanelError(err error, message string) response.Response {
if errors.Is(err, models.ErrFolderAccessDenied) {
return response.Error(403, models.ErrFolderAccessDenied.Error(), err)
}
if errors.Is(err, errLibraryPanelHasConnectedDashboards) {
return response.Error(403, errLibraryPanelHasConnectedDashboards.Error(), err)
}
return response.Error(500, message, err)
}

View File

@@ -173,8 +173,14 @@ func (lps *LibraryPanelService) deleteLibraryPanel(c *models.ReqContext, uid str
if err := lps.requirePermissionsOnFolder(c.SignedInUser, panel.FolderID); err != nil {
return err
}
if _, err := session.Exec("DELETE FROM library_panel_dashboard WHERE librarypanel_id=?", panel.ID); err != nil {
var dashIDs []struct {
DashboardID int64 `xorm:"dashboard_id"`
}
sql := "SELECT dashboard_id FROM library_panel_dashboard WHERE librarypanel_id=?"
if err := session.SQL(sql, panel.ID).Find(&dashIDs); err != nil {
return err
} else if len(dashIDs) > 0 {
return errLibraryPanelHasConnectedDashboards
}
result, err := session.Exec("DELETE FROM library_panel WHERE id=?", panel.ID)

View File

@@ -30,4 +30,15 @@ func TestDeleteLibraryPanel(t *testing.T) {
resp := sc.service.deleteHandler(sc.reqContext)
require.Equal(t, 404, resp.Status())
})
scenarioWithLibraryPanel(t, "When an admin tries to delete a library panel that is connected, it should fail",
func(t *testing.T, sc scenarioContext) {
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID, ":dashboardId": "1"})
resp := sc.service.connectHandler(sc.reqContext)
require.Equal(t, 200, resp.Status())
sc.reqContext.ReplaceAllParams(map[string]string{":uid": sc.initialResult.Result.UID})
resp = sc.service.deleteHandler(sc.reqContext)
require.Equal(t, 403, resp.Status())
})
}

View File

@@ -111,6 +111,8 @@ var (
ErrFolderHasConnectedLibraryPanels = errors.New("folder contains library panels that are linked to dashboards")
// errLibraryPanelVersionMismatch is an error for when a library panel has been changed by someone else.
errLibraryPanelVersionMismatch = errors.New("the library panel has been changed by someone else")
// errLibraryPanelHasConnectedDashboards is an error for when an user deletes a library panel that is connected to library panels.
errLibraryPanelHasConnectedDashboards = errors.New("the library panel is linked to dashboards")
)
// Commands