diff --git a/pkg/api/index.go b/pkg/api/index.go index da0c9c45bf1..4668e58a805 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -7,7 +7,6 @@ import ( "github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/api/navlinks" - "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/plugins" ac "github.com/grafana/grafana/pkg/services/accesscontrol" @@ -540,7 +539,7 @@ func (hs *HTTPServer) buildAdminNavLinks(c *models.ReqContext) []*dtos.NavLink { func (hs *HTTPServer) setIndexViewData(c *models.ReqContext) (*dtos.IndexViewData, error) { hasEditPermissionInFoldersQuery := models.HasEditPermissionInFoldersQuery{SignedInUser: c.SignedInUser} - if err := bus.Dispatch(c.Req.Context(), &hasEditPermissionInFoldersQuery); err != nil { + if err := hs.SQLStore.HasEditPermissionInFolders(c.Req.Context(), &hasEditPermissionInFoldersQuery); err != nil { return nil, err } hasEditPerm := hasEditPermissionInFoldersQuery.Result @@ -553,7 +552,7 @@ func (hs *HTTPServer) setIndexViewData(c *models.ReqContext) (*dtos.IndexViewDat settings["dateFormats"] = hs.Cfg.DateFormats prefsQuery := models.GetPreferencesWithDefaultsQuery{User: c.SignedInUser} - if err := bus.Dispatch(c.Req.Context(), &prefsQuery); err != nil { + if err := hs.SQLStore.GetPreferencesWithDefaults(c.Req.Context(), &prefsQuery); err != nil { return nil, err } prefs := prefsQuery.Result diff --git a/pkg/services/sqlstore/dashboard.go b/pkg/services/sqlstore/dashboard.go index ba2f812a22f..9cc57173906 100644 --- a/pkg/services/sqlstore/dashboard.go +++ b/pkg/services/sqlstore/dashboard.go @@ -33,7 +33,6 @@ func init() { bus.AddHandler("sql", GetDashboardsByPluginId) bus.AddHandler("sql", GetDashboardPermissionsForUser) bus.AddHandler("sql", GetDashboardsBySlug) - bus.AddHandler("sql", HasEditPermissionInFolders) bus.AddHandler("sql", HasAdminPermissionInFolders) prometheus.MustRegister(shadowSearchCounter) @@ -44,6 +43,7 @@ func (ss *SQLStore) addDashboardQueryAndCommandHandlers() { bus.AddHandler("sql", ss.SearchDashboards) bus.AddHandler("sql", ss.DeleteDashboard) bus.AddHandler("sql", ss.GetDashboards) + bus.AddHandler("sql", ss.HasEditPermissionInFolders) } var generateNewUid func() string = util.GenerateShortUID @@ -800,7 +800,7 @@ func (ss *SQLStore) ValidateDashboardBeforeSave(dashboard *models.Dashboard, ove } // HasEditPermissionInFolders validates that an user have access to a certain folder -func HasEditPermissionInFolders(ctx context.Context, query *models.HasEditPermissionInFoldersQuery) error { +func (ss *SQLStore) HasEditPermissionInFolders(ctx context.Context, query *models.HasEditPermissionInFoldersQuery) error { return withDbSession(ctx, x, func(dbSession *DBSession) error { if query.SignedInUser.HasRole(models.ROLE_EDITOR) { query.Result = true diff --git a/pkg/services/sqlstore/dashboard_folder_test.go b/pkg/services/sqlstore/dashboard_folder_test.go index cdef363fb95..3b7007594f0 100644 --- a/pkg/services/sqlstore/dashboard_folder_test.go +++ b/pkg/services/sqlstore/dashboard_folder_test.go @@ -327,7 +327,7 @@ func TestDashboardFolderDataAccess(t *testing.T) { query := &models.HasEditPermissionInFoldersQuery{ SignedInUser: &models.SignedInUser{UserId: adminUser.Id, OrgId: 1, OrgRole: models.ROLE_ADMIN}, } - err := HasEditPermissionInFolders(context.Background(), query) + err := sqlStore.HasEditPermissionInFolders(context.Background(), query) require.NoError(t, err) require.True(t, query.Result) }) @@ -393,7 +393,7 @@ func TestDashboardFolderDataAccess(t *testing.T) { query := &models.HasEditPermissionInFoldersQuery{ SignedInUser: &models.SignedInUser{UserId: editorUser.Id, OrgId: 1, OrgRole: models.ROLE_EDITOR}, } - err := HasEditPermissionInFolders(context.Background(), query) + err := sqlStore.HasEditPermissionInFolders(context.Background(), query) require.NoError(t, err) require.True(t, query.Result) }) @@ -461,7 +461,7 @@ func TestDashboardFolderDataAccess(t *testing.T) { query := &models.HasEditPermissionInFoldersQuery{ SignedInUser: &models.SignedInUser{UserId: viewerUser.Id, OrgId: 1, OrgRole: models.ROLE_VIEWER}, } - err := HasEditPermissionInFolders(context.Background(), query) + err := sqlStore.HasEditPermissionInFolders(context.Background(), query) require.NoError(t, err) require.False(t, query.Result) }) @@ -485,7 +485,7 @@ func TestDashboardFolderDataAccess(t *testing.T) { query := &models.HasEditPermissionInFoldersQuery{ SignedInUser: &models.SignedInUser{UserId: viewerUser.Id, OrgId: 1, OrgRole: models.ROLE_VIEWER}, } - err := HasEditPermissionInFolders(context.Background(), query) + err := sqlStore.HasEditPermissionInFolders(context.Background(), query) require.NoError(t, err) require.True(t, query.Result) }) @@ -501,7 +501,7 @@ func TestDashboardFolderDataAccess(t *testing.T) { query := &models.HasEditPermissionInFoldersQuery{ SignedInUser: &models.SignedInUser{UserId: viewerUser.Id, OrgId: 1, OrgRole: models.ROLE_VIEWER}, } - err := HasEditPermissionInFolders(context.Background(), query) + err := sqlStore.HasEditPermissionInFolders(context.Background(), query) require.NoError(t, err) require.True(t, query.Result) }) diff --git a/pkg/services/sqlstore/mockstore/mockstore.go b/pkg/services/sqlstore/mockstore/mockstore.go index 4fb230bd1cd..827e94d56e2 100644 --- a/pkg/services/sqlstore/mockstore/mockstore.go +++ b/pkg/services/sqlstore/mockstore/mockstore.go @@ -34,6 +34,10 @@ func (m SQLStoreMock) GetDashboardSnapshot(query *models.GetDashboardSnapshotQue return m.ExpectedError } +func (m SQLStoreMock) HasEditPermissionInFolders(ctx context.Context, query *models.HasEditPermissionInFoldersQuery) error { + return m.ExpectedError +} + func (m SQLStoreMock) SearchDashboardSnapshots(query *models.GetDashboardSnapshotsQuery) error { return m.ExpectedError } diff --git a/pkg/services/sqlstore/store.go b/pkg/services/sqlstore/store.go index ddb8595582d..1c6c3da67fb 100644 --- a/pkg/services/sqlstore/store.go +++ b/pkg/services/sqlstore/store.go @@ -12,6 +12,7 @@ type Store interface { CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error GetDashboardSnapshot(query *models.GetDashboardSnapshotQuery) error + HasEditPermissionInFolders(ctx context.Context, query *models.HasEditPermissionInFoldersQuery) error SearchDashboardSnapshots(query *models.GetDashboardSnapshotsQuery) error GetOrgByName(name string) (*models.Org, error) CreateOrgWithMember(name string, userID int64) (models.Org, error)