LibraryPanels: Deletes library panels during folder deletion (#31572)
* Refactor: adds permissions for library panel creation * Refactor: checks folder permissions for patch requests * Chore: changes after PR comments * Refactor: adds permissions to delete * Refactor: moves get all permission tests out of get all tests * Chore: move out get all tests to a separate file * Refactor: adds permissions to get handler * Refactor: fixes a bug with getting library panels in General folder * Refactor: adds permissions for connect/disconnect * Refactor: adds permissions and tests for get connected dashboards * Tests: adds tests for connected dashboards in General Folder * LibraryPanels: Deletes library panels during folder deletion * LibraryPanels: Deletes library panels during folder deletion * Update pkg/api/folder.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> * Update pkg/services/librarypanels/librarypanels_permissions_test.go Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com> * Chore: updates after PR comments * Chore: forgot to change some function signatures Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
co-authored by
Arve Knudsen
parent
e13a48166b
commit
4bc6a7c407
@@ -231,6 +231,59 @@ func (lps *LibraryPanelService) disconnectLibraryPanelsForDashboard(c *models.Re
|
||||
})
|
||||
}
|
||||
|
||||
// deleteLibraryPanelsInFolder deletes all Library Panels for a folder.
|
||||
func (lps *LibraryPanelService) deleteLibraryPanelsInFolder(c *models.ReqContext, folderUID string) error {
|
||||
return lps.SQLStore.WithTransactionalDbSession(c.Context.Req.Context(), func(session *sqlstore.DBSession) error {
|
||||
var folderUIDs []struct {
|
||||
ID int64 `xorm:"id"`
|
||||
}
|
||||
err := session.SQL("SELECT id from dashboard WHERE uid=? AND org_id=? AND is_folder=1", folderUID, c.SignedInUser.OrgId).Find(&folderUIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(folderUIDs) != 1 {
|
||||
return fmt.Errorf("found %d folders, while expecting at most one", len(folderUIDs))
|
||||
}
|
||||
folderID := folderUIDs[0].ID
|
||||
|
||||
if err := requirePermissionsOnFolder(c.SignedInUser, folderID); err != nil {
|
||||
return err
|
||||
}
|
||||
var dashIDs []struct {
|
||||
DashboardID int64 `xorm:"dashboard_id"`
|
||||
}
|
||||
sql := "SELECT lpd.dashboard_id FROM library_panel AS lp"
|
||||
sql += " INNER JOIN library_panel_dashboard lpd on lp.id = lpd.librarypanel_id"
|
||||
sql += " WHERE lp.folder_id=? AND lp.org_id=?"
|
||||
err = session.SQL(sql, folderID, c.SignedInUser.OrgId).Find(&dashIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(dashIDs) > 0 {
|
||||
return ErrFolderHasConnectedLibraryPanels
|
||||
}
|
||||
|
||||
var panelIDs []struct {
|
||||
ID int64 `xorm:"id"`
|
||||
}
|
||||
err = session.SQL("SELECT id from library_panel WHERE folder_id=? AND org_id=?", folderID, c.SignedInUser.OrgId).Find(&panelIDs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, panelID := range panelIDs {
|
||||
_, err := session.Exec("DELETE FROM library_panel_dashboard WHERE librarypanel_id=?", panelID.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := session.Exec("DELETE FROM library_panel WHERE folder_id=? AND org_id=?", folderID, c.SignedInUser.OrgId); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func getLibraryPanel(session *sqlstore.DBSession, uid string, orgID int64) (LibraryPanelWithMeta, error) {
|
||||
libraryPanels := make([]LibraryPanelWithMeta, 0)
|
||||
sql := sqlStatmentLibrayPanelDTOWithMeta + "WHERE lp.uid=? AND lp.org_id=?"
|
||||
|
||||
Reference in New Issue
Block a user