Chore: Propagate context for dashboard guardian (#39201)
Require guardian.New to take context.Context as first argument. Migrates the GetDashboardAclInfoListQuery to be dispatched using context. Ref #36734 Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> Co-authored-by: sam boyer <sam.boyer@grafana.com>
This commit is contained in:
co-authored by
Emil Tullstedt
sam boyer
parent
914ae81026
commit
518a0d0458
@@ -8,12 +8,16 @@ import (
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetDashboardAclInfoList)
|
||||
func (ss *SQLStore) addDashboardACLQueryAndCommandHandlers() {
|
||||
bus.AddHandlerCtx("sql", ss.GetDashboardAclInfoList)
|
||||
}
|
||||
|
||||
func (ss *SQLStore) UpdateDashboardACL(dashboardID int64, items []*models.DashboardAcl) error {
|
||||
return ss.WithTransactionalDbSession(context.Background(), func(sess *DBSession) error {
|
||||
return ss.UpdateDashboardACLCtx(context.TODO(), dashboardID, items)
|
||||
}
|
||||
|
||||
func (ss *SQLStore) UpdateDashboardACLCtx(ctx context.Context, dashboardID int64, items []*models.DashboardAcl) error {
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
// delete existing items
|
||||
_, err := sess.Exec("DELETE FROM dashboard_acl WHERE dashboard_id=?", dashboardID)
|
||||
if err != nil {
|
||||
@@ -47,13 +51,13 @@ func (ss *SQLStore) UpdateDashboardACL(dashboardID int64, items []*models.Dashbo
|
||||
// 1) Permissions for the dashboard
|
||||
// 2) permissions for its parent folder
|
||||
// 3) if no specific permissions have been set for the dashboard or its parent folder then get the default permissions
|
||||
func GetDashboardAclInfoList(query *models.GetDashboardAclInfoListQuery) error {
|
||||
var err error
|
||||
func (ss *SQLStore) GetDashboardAclInfoList(ctx context.Context, query *models.GetDashboardAclInfoListQuery) error {
|
||||
outerErr := ss.WithDbSession(ctx, func(dbSession *DBSession) error {
|
||||
query.Result = make([]*models.DashboardAclInfoDTO, 0)
|
||||
falseStr := dialect.BooleanStr(false)
|
||||
|
||||
falseStr := dialect.BooleanStr(false)
|
||||
|
||||
if query.DashboardID == 0 {
|
||||
sql := `SELECT
|
||||
if query.DashboardID == 0 {
|
||||
sql := `SELECT
|
||||
da.id,
|
||||
da.org_id,
|
||||
da.dashboard_id,
|
||||
@@ -69,13 +73,13 @@ func GetDashboardAclInfoList(query *models.GetDashboardAclInfoListQuery) error {
|
||||
'' as title,
|
||||
'' as slug,
|
||||
'' as uid,` +
|
||||
falseStr + ` AS is_folder,` +
|
||||
falseStr + ` AS inherited
|
||||
falseStr + ` AS is_folder,` +
|
||||
falseStr + ` AS inherited
|
||||
FROM dashboard_acl as da
|
||||
WHERE da.dashboard_id = -1`
|
||||
query.Result = make([]*models.DashboardAclInfoDTO, 0)
|
||||
err = x.SQL(sql).Find(&query.Result)
|
||||
} else {
|
||||
return dbSession.SQL(sql).Find(&query.Result)
|
||||
}
|
||||
|
||||
rawSQL := `
|
||||
-- get permissions for the dashboard and its parent folder
|
||||
SELECT
|
||||
@@ -115,13 +119,16 @@ func GetDashboardAclInfoList(query *models.GetDashboardAclInfoListQuery) error {
|
||||
ORDER BY da.id ASC
|
||||
`
|
||||
|
||||
query.Result = make([]*models.DashboardAclInfoDTO, 0)
|
||||
err = x.SQL(rawSQL, query.OrgID, query.DashboardID).Find(&query.Result)
|
||||
return dbSession.SQL(rawSQL, query.OrgID, query.DashboardID).Find(&query.Result)
|
||||
})
|
||||
|
||||
if outerErr != nil {
|
||||
return outerErr
|
||||
}
|
||||
|
||||
for _, p := range query.Result {
|
||||
p.PermissionName = p.Permission.String()
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
@@ -32,7 +33,7 @@ func TestDashboardAclDataAccess(t *testing.T) {
|
||||
Convey("When reading folder acl should include default acl", func() {
|
||||
query := models.GetDashboardAclInfoListQuery{DashboardID: savedFolder.Id, OrgID: 1}
|
||||
|
||||
err := GetDashboardAclInfoList(&query)
|
||||
err := sqlStore.GetDashboardAclInfoList(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
@@ -48,7 +49,7 @@ func TestDashboardAclDataAccess(t *testing.T) {
|
||||
Convey("When reading dashboard acl should include acl for parent folder", func() {
|
||||
query := models.GetDashboardAclInfoListQuery{DashboardID: childDash.Id, OrgID: 1}
|
||||
|
||||
err := GetDashboardAclInfoList(&query)
|
||||
err := sqlStore.GetDashboardAclInfoList(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
@@ -69,7 +70,7 @@ func TestDashboardAclDataAccess(t *testing.T) {
|
||||
Convey("When reading dashboard acl should return no acl items", func() {
|
||||
query := models.GetDashboardAclInfoListQuery{DashboardID: childDash.Id, OrgID: 1}
|
||||
|
||||
err := GetDashboardAclInfoList(&query)
|
||||
err := sqlStore.GetDashboardAclInfoList(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 0)
|
||||
@@ -88,7 +89,7 @@ func TestDashboardAclDataAccess(t *testing.T) {
|
||||
Convey("When reading dashboard acl should include acl for parent folder", func() {
|
||||
query := models.GetDashboardAclInfoListQuery{DashboardID: childDash.Id, OrgID: 1}
|
||||
|
||||
err := GetDashboardAclInfoList(&query)
|
||||
err := sqlStore.GetDashboardAclInfoList(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 1)
|
||||
@@ -107,7 +108,7 @@ func TestDashboardAclDataAccess(t *testing.T) {
|
||||
Convey("When reading dashboard acl should include acl for parent folder and child", func() {
|
||||
query := models.GetDashboardAclInfoListQuery{OrgID: 1, DashboardID: childDash.Id}
|
||||
|
||||
err := GetDashboardAclInfoList(&query)
|
||||
err := sqlStore.GetDashboardAclInfoList(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
@@ -131,7 +132,7 @@ func TestDashboardAclDataAccess(t *testing.T) {
|
||||
Convey("When reading dashboard acl should include default acl for parent folder and the child acl", func() {
|
||||
query := models.GetDashboardAclInfoListQuery{OrgID: 1, DashboardID: childDash.Id}
|
||||
|
||||
err := GetDashboardAclInfoList(&query)
|
||||
err := sqlStore.GetDashboardAclInfoList(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
defaultPermissionsId := -1
|
||||
@@ -157,7 +158,7 @@ func TestDashboardAclDataAccess(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
q1 := &models.GetDashboardAclInfoListQuery{DashboardID: savedFolder.Id, OrgID: 1}
|
||||
err = GetDashboardAclInfoList(q1)
|
||||
err = sqlStore.GetDashboardAclInfoList(context.Background(), q1)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(q1.Result[0].DashboardId, ShouldEqual, savedFolder.Id)
|
||||
@@ -172,7 +173,7 @@ func TestDashboardAclDataAccess(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
q3 := &models.GetDashboardAclInfoListQuery{DashboardID: savedFolder.Id, OrgID: 1}
|
||||
err = GetDashboardAclInfoList(q3)
|
||||
err = sqlStore.GetDashboardAclInfoList(context.Background(), q3)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(q3.Result), ShouldEqual, 0)
|
||||
})
|
||||
@@ -192,7 +193,7 @@ func TestDashboardAclDataAccess(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
q1 := &models.GetDashboardAclInfoListQuery{DashboardID: savedFolder.Id, OrgID: 1}
|
||||
err = GetDashboardAclInfoList(q1)
|
||||
err = sqlStore.GetDashboardAclInfoList(context.Background(), q1)
|
||||
So(err, ShouldBeNil)
|
||||
So(q1.Result[0].DashboardId, ShouldEqual, savedFolder.Id)
|
||||
So(q1.Result[0].Permission, ShouldEqual, models.PERMISSION_EDIT)
|
||||
@@ -209,7 +210,7 @@ func TestDashboardAclDataAccess(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
q3 := &models.GetDashboardAclInfoListQuery{DashboardID: savedFolder.Id, OrgID: 1}
|
||||
err = GetDashboardAclInfoList(q3)
|
||||
err = sqlStore.GetDashboardAclInfoList(context.Background(), q3)
|
||||
So(err, ShouldBeNil)
|
||||
So(len(q3.Result), ShouldEqual, 1)
|
||||
So(q3.Result[0].DashboardId, ShouldEqual, savedFolder.Id)
|
||||
@@ -225,7 +226,7 @@ func TestDashboardAclDataAccess(t *testing.T) {
|
||||
Convey("When reading dashboard acl should return default permissions", func() {
|
||||
query := models.GetDashboardAclInfoListQuery{DashboardID: rootFolderId, OrgID: 1}
|
||||
|
||||
err := GetDashboardAclInfoList(&query)
|
||||
err := sqlStore.GetDashboardAclInfoList(context.Background(), &query)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(query.Result), ShouldEqual, 2)
|
||||
|
||||
@@ -338,7 +338,7 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
|
||||
Convey("Should remove dependent permissions for deleted org user", func() {
|
||||
permQuery := &models.GetDashboardAclInfoListQuery{DashboardID: dash1.Id, OrgID: ac1.OrgId}
|
||||
err = GetDashboardAclInfoList(permQuery)
|
||||
err = sqlStore.GetDashboardAclInfoList(context.Background(), permQuery)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(permQuery.Result), ShouldEqual, 0)
|
||||
@@ -346,7 +346,7 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
|
||||
Convey("Should not remove dashboard permissions for same user in another org", func() {
|
||||
permQuery := &models.GetDashboardAclInfoListQuery{DashboardID: dash2.Id, OrgID: ac3.OrgId}
|
||||
err = GetDashboardAclInfoList(permQuery)
|
||||
err = sqlStore.GetDashboardAclInfoList(context.Background(), permQuery)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(permQuery.Result), ShouldEqual, 1)
|
||||
|
||||
@@ -113,6 +113,7 @@ func newSQLStore(cfg *setting.Cfg, cacheService *localcache.CacheService, bus bu
|
||||
ss.addAlertNotificationUidByIdHandler()
|
||||
ss.addPreferencesQueryAndCommandHandlers()
|
||||
ss.addDashboardQueryAndCommandHandlers()
|
||||
ss.addDashboardACLQueryAndCommandHandlers()
|
||||
ss.addQuotaQueryAndCommandHandlers()
|
||||
|
||||
// if err := ss.Reset(); err != nil {
|
||||
|
||||
@@ -262,7 +262,7 @@ func TestTeamCommandsAndQueries(t *testing.T) {
|
||||
So(err, ShouldEqual, models.ErrTeamNotFound)
|
||||
|
||||
permQuery := &models.GetDashboardAclInfoListQuery{DashboardID: 1, OrgID: testOrgID}
|
||||
err = GetDashboardAclInfoList(permQuery)
|
||||
err = sqlStore.GetDashboardAclInfoList(context.Background(), permQuery)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(len(permQuery.Result), ShouldEqual, 0)
|
||||
|
||||
@@ -261,7 +261,7 @@ func TestUserDataAccess(t *testing.T) {
|
||||
require.Len(t, query1.Result, 1)
|
||||
|
||||
permQuery := &models.GetDashboardAclInfoListQuery{DashboardID: 1, OrgID: users[0].OrgId}
|
||||
err = GetDashboardAclInfoList(permQuery)
|
||||
err = ss.GetDashboardAclInfoList(context.Background(), permQuery)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Len(t, permQuery.Result, 0)
|
||||
@@ -347,7 +347,7 @@ func TestUserDataAccess(t *testing.T) {
|
||||
require.Len(t, query2.Result, 1)
|
||||
|
||||
permQuery = &models.GetDashboardAclInfoListQuery{DashboardID: 1, OrgID: users[0].OrgId}
|
||||
err = GetDashboardAclInfoList(permQuery)
|
||||
err = ss.GetDashboardAclInfoList(context.Background(), permQuery)
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Len(t, permQuery.Result, 0)
|
||||
|
||||
Reference in New Issue
Block a user