Chore: Remove x from dashboard snapshots (#48001)

pull/48011/head^2
Kat Yang 3 years ago committed by GitHub
parent d832bde270
commit eb05f6ead8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      pkg/services/dashboardsnapshots/dashboardsnapshots.go
  2. 76
      pkg/services/sqlstore/dashboard_snapshot.go
  3. 14
      pkg/services/sqlstore/dashboard_snapshot_test.go
  4. 4
      pkg/services/sqlstore/mockstore/mockstore.go
  5. 4
      pkg/services/sqlstore/store.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 {

@ -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)
sess := x.NewSession()
if query.Limit > 0 {
sess.Limit(query.Limit)
}
sess.Table("dashboard_snapshot")
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:
query.Result = snapshots
return nil
}
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 := ss.NewSession(ctx)
if query.Limit > 0 {
sess.Limit(query.Limit)
}
sess.Table("dashboard_snapshot")
if query.Name != "" {
sess.Where("name LIKE ?", query.Name)
}
err := sess.Find(&snapshots)
query.Result = snapshots
return err
// 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 err
})
}

@ -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)

@ -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
}

@ -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

Loading…
Cancel
Save