diff --git a/pkg/services/dashboardsnapshots/dashboardsnapshots.go b/pkg/services/dashboardsnapshots/dashboardsnapshots.go index 51587cfa2dd..001bf4354cd 100644 --- a/pkg/services/dashboardsnapshots/dashboardsnapshots.go +++ b/pkg/services/dashboardsnapshots/dashboardsnapshots.go @@ -40,7 +40,7 @@ func (s *Service) CreateDashboardSnapshot(ctx context.Context, cmd *models.Creat } func (s *Service) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error { - err := s.SQLStore.GetDashboardSnapshot(query) + err := s.SQLStore.GetDashboardSnapshot(ctx, query) if err != nil { return err } @@ -66,8 +66,8 @@ func (s *Service) DeleteDashboardSnapshot(ctx context.Context, cmd *models.Delet return s.SQLStore.DeleteDashboardSnapshot(ctx, cmd) } -func (s *Service) SearchDashboardSnapshots(_ context.Context, query *models.GetDashboardSnapshotsQuery) error { - return s.SQLStore.SearchDashboardSnapshots(query) +func (s *Service) SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error { + return s.SQLStore.SearchDashboardSnapshots(ctx, query) } func (s *Service) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error { diff --git a/pkg/services/sqlstore/dashboard_snapshot.go b/pkg/services/sqlstore/dashboard_snapshot.go index 38584e26a4e..9773b795fa1 100644 --- a/pkg/services/sqlstore/dashboard_snapshot.go +++ b/pkg/services/sqlstore/dashboard_snapshot.go @@ -68,47 +68,51 @@ func (ss *SQLStore) DeleteDashboardSnapshot(ctx context.Context, cmd *models.Del }) } -func (ss *SQLStore) GetDashboardSnapshot(query *models.GetDashboardSnapshotQuery) error { - snapshot := models.DashboardSnapshot{Key: query.Key, DeleteKey: query.DeleteKey} - has, err := x.Get(&snapshot) +func (ss *SQLStore) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error { + return ss.WithDbSession(ctx, func(dbSess *DBSession) error { + snapshot := models.DashboardSnapshot{Key: query.Key, DeleteKey: query.DeleteKey} + has, err := dbSess.Get(&snapshot) - if err != nil { - return err - } else if !has { - return models.ErrDashboardSnapshotNotFound - } + if err != nil { + return err + } else if !has { + return models.ErrDashboardSnapshotNotFound + } - query.Result = &snapshot - return nil + query.Result = &snapshot + return nil + }) } // SearchDashboardSnapshots returns a list of all snapshots for admins // for other roles, it returns snapshots created by the user -func (ss *SQLStore) SearchDashboardSnapshots(query *models.GetDashboardSnapshotsQuery) error { - var snapshots = make(models.DashboardSnapshotsList, 0) +func (ss *SQLStore) SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error { + return ss.WithDbSession(ctx, func(dbSess *DBSession) error { + var snapshots = make(models.DashboardSnapshotsList, 0) - sess := x.NewSession() - if query.Limit > 0 { - sess.Limit(query.Limit) - } - sess.Table("dashboard_snapshot") + sess := ss.NewSession(ctx) + if query.Limit > 0 { + sess.Limit(query.Limit) + } + sess.Table("dashboard_snapshot") - if query.Name != "" { - sess.Where("name LIKE ?", query.Name) - } + if query.Name != "" { + sess.Where("name LIKE ?", query.Name) + } - // admins can see all snapshots, everyone else can only see their own snapshots - switch { - case query.SignedInUser.OrgRole == models.ROLE_ADMIN: - sess.Where("org_id = ?", query.OrgId) - case !query.SignedInUser.IsAnonymous: - sess.Where("org_id = ? AND user_id = ?", query.OrgId, query.SignedInUser.UserId) - default: + // admins can see all snapshots, everyone else can only see their own snapshots + switch { + case query.SignedInUser.OrgRole == models.ROLE_ADMIN: + sess.Where("org_id = ?", query.OrgId) + case !query.SignedInUser.IsAnonymous: + sess.Where("org_id = ? AND user_id = ?", query.OrgId, query.SignedInUser.UserId) + default: + query.Result = snapshots + return nil + } + + err := sess.Find(&snapshots) query.Result = snapshots - return nil - } - - err := sess.Find(&snapshots) - query.Result = snapshots - return err + return err + }) } diff --git a/pkg/services/sqlstore/dashboard_snapshot_test.go b/pkg/services/sqlstore/dashboard_snapshot_test.go index ebaf1091ebb..97127a9978a 100644 --- a/pkg/services/sqlstore/dashboard_snapshot_test.go +++ b/pkg/services/sqlstore/dashboard_snapshot_test.go @@ -47,7 +47,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) { t.Run("Should be able to get snapshot by key", func(t *testing.T) { query := models.GetDashboardSnapshotQuery{Key: "hej"} - err := sqlstore.GetDashboardSnapshot(&query) + err := sqlstore.GetDashboardSnapshot(context.Background(), &query) require.NoError(t, err) assert.NotNil(t, query.Result) @@ -69,7 +69,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) { OrgId: 1, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN}, } - err := sqlstore.SearchDashboardSnapshots(&query) + err := sqlstore.SearchDashboardSnapshots(context.Background(), &query) require.NoError(t, err) t.Run("Should return all the snapshots", func(t *testing.T) { @@ -83,7 +83,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) { OrgId: 1, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 1000}, } - err := sqlstore.SearchDashboardSnapshots(&query) + err := sqlstore.SearchDashboardSnapshots(context.Background(), &query) require.NoError(t, err) t.Run("Should return all the snapshots", func(t *testing.T) { @@ -97,7 +97,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) { OrgId: 1, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 2}, } - err := sqlstore.SearchDashboardSnapshots(&query) + err := sqlstore.SearchDashboardSnapshots(context.Background(), &query) require.NoError(t, err) t.Run("Should not return any snapshots", func(t *testing.T) { @@ -124,7 +124,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) { OrgId: 1, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, IsAnonymous: true, UserId: 0}, } - err := sqlstore.SearchDashboardSnapshots(&query) + err := sqlstore.SearchDashboardSnapshots(context.Background(), &query) require.NoError(t, err) require.NotNil(t, query.Result) @@ -161,7 +161,7 @@ func TestDeleteExpiredSnapshots(t *testing.T) { OrgId: 1, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN}, } - err = sqlstore.SearchDashboardSnapshots(&query) + err = sqlstore.SearchDashboardSnapshots(context.Background(), &query) require.NoError(t, err) assert.Len(t, query.Result, 1) @@ -174,7 +174,7 @@ func TestDeleteExpiredSnapshots(t *testing.T) { OrgId: 1, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN}, } - err = sqlstore.SearchDashboardSnapshots(&query) + err = sqlstore.SearchDashboardSnapshots(context.Background(), &query) require.NoError(t, err) require.Len(t, query.Result, 1) diff --git a/pkg/services/sqlstore/mockstore/mockstore.go b/pkg/services/sqlstore/mockstore/mockstore.go index c8e323d1123..94d635aa6d3 100644 --- a/pkg/services/sqlstore/mockstore/mockstore.go +++ b/pkg/services/sqlstore/mockstore/mockstore.go @@ -88,7 +88,7 @@ func (m *SQLStoreMock) DeleteDashboardSnapshot(ctx context.Context, cmd *models. return m.ExpectedError } -func (m *SQLStoreMock) GetDashboardSnapshot(query *models.GetDashboardSnapshotQuery) error { +func (m *SQLStoreMock) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error { query.Result = m.ExpectedDashboardSnapshot return m.ExpectedError } @@ -97,7 +97,7 @@ func (m *SQLStoreMock) HasEditPermissionInFolders(ctx context.Context, query *mo return m.ExpectedError } -func (m *SQLStoreMock) SearchDashboardSnapshots(query *models.GetDashboardSnapshotsQuery) error { +func (m *SQLStoreMock) SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error { return m.ExpectedError } diff --git a/pkg/services/sqlstore/store.go b/pkg/services/sqlstore/store.go index 26451462708..d0d6741c750 100644 --- a/pkg/services/sqlstore/store.go +++ b/pkg/services/sqlstore/store.go @@ -15,9 +15,9 @@ type Store interface { DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error - GetDashboardSnapshot(query *models.GetDashboardSnapshotQuery) error + GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error HasEditPermissionInFolders(ctx context.Context, query *models.HasEditPermissionInFoldersQuery) error - SearchDashboardSnapshots(query *models.GetDashboardSnapshotsQuery) error + SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error GetOrgByName(name string) (*models.Org, error) CreateOrgWithMember(name string, userID int64) (models.Org, error) UpdateOrg(ctx context.Context, cmd *models.UpdateOrgCommand) error