Dashboards: Allow updating a dashboard if the user doesn't have access to the parent folder (#78075)

* change where folder checks are done for dash creation/updates

* add test for folder not being found

* test fixes

* more test fixes

* add nlint directive to where folder IDs are used

* fix bad merge

* fix test
This commit is contained in:
Ieva
2023-11-16 11:11:35 +00:00
committed by GitHub
parent ba717454e1
commit b0448b92e5
9 changed files with 42 additions and 72 deletions
-20
View File
@@ -25,7 +25,6 @@ import (
"github.com/grafana/grafana/pkg/services/dashboards"
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/folder"
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/org"
pref "github.com/grafana/grafana/pkg/services/preference"
@@ -388,25 +387,6 @@ func (hs *HTTPServer) postDashboard(c *contextmodel.ReqContext, cmd dashboards.S
cmd.OrgID = c.SignedInUser.GetOrgID()
cmd.UserID = userID
// nolint:staticcheck
if cmd.FolderUID != "" || cmd.FolderID != 0 {
folder, err := hs.folderService.Get(ctx, &folder.GetFolderQuery{
OrgID: c.SignedInUser.GetOrgID(),
UID: &cmd.FolderUID,
// nolint:staticcheck
ID: &cmd.FolderID,
SignedInUser: c.SignedInUser,
})
if err != nil {
if errors.Is(err, dashboards.ErrFolderNotFound) {
return response.Error(http.StatusBadRequest, "Folder not found", err)
}
return response.Error(http.StatusInternalServerError, "Error while checking folder ID", err)
}
// nolint:staticcheck
cmd.FolderID = folder.ID
cmd.FolderUID = folder.UID
}
dash := cmd.GetDashboardModel()
newDashboard := dash.ID == 0
-26
View File
@@ -3,7 +3,6 @@ package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
@@ -468,31 +467,6 @@ func TestDashboardAPIEndpoint(t *testing.T) {
})
})
t.Run("Given a request with incorrect folder uid for creating a dashboard with", func(t *testing.T) {
cmd := dashboards.SaveDashboardCommand{
OrgID: 1,
UserID: 5,
Dashboard: simplejson.NewFromAny(map[string]any{
"title": "Dash",
}),
Overwrite: true,
FolderUID: "folderUID",
IsFolder: false,
Message: "msg",
}
dashboardService := dashboards.NewFakeDashboardService(t)
mockFolder := &foldertest.FakeService{
ExpectedError: errors.New("Error while searching Folder ID"),
}
postDashboardScenario(t, "When calling POST on", "/api/dashboards", "/api/dashboards", cmd, dashboardService, mockFolder, func(sc *scenarioContext) {
callPostDashboard(sc)
assert.Equal(t, http.StatusInternalServerError, sc.resp.Code)
})
})
// This tests that invalid requests returns expected error responses
t.Run("Given incorrect requests for creating a dashboard", func(t *testing.T) {
testCases := []struct {