diff --git a/pkg/api/index.go b/pkg/api/index.go index cce70a15503..7f484ecf555 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -415,7 +415,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(&hasEditPermissionInFoldersQuery); err != nil { + if err := bus.DispatchCtx(c.Req.Context(), &hasEditPermissionInFoldersQuery); err != nil { return nil, err } hasEditPerm := hasEditPermissionInFoldersQuery.Result diff --git a/pkg/services/sqlstore/dashboard.go b/pkg/services/sqlstore/dashboard.go index 7e48d3bdcbb..80206bf4269 100644 --- a/pkg/services/sqlstore/dashboard.go +++ b/pkg/services/sqlstore/dashboard.go @@ -799,29 +799,32 @@ func (ss *SQLStore) ValidateDashboardBeforeSave(dashboard *models.Dashboard, ove return isParentFolderChanged, nil } +// HasEditPermissionInFolders validates that an user have access to a certain folder func HasEditPermissionInFolders(ctx context.Context, query *models.HasEditPermissionInFoldersQuery) error { - if query.SignedInUser.HasRole(models.ROLE_EDITOR) { - query.Result = true + return withDbSession(ctx, x, func(dbSession *DBSession) error { + if query.SignedInUser.HasRole(models.ROLE_EDITOR) { + query.Result = true + return nil + } + + builder := &SQLBuilder{} + builder.Write("SELECT COUNT(dashboard.id) AS count FROM dashboard WHERE dashboard.org_id = ? AND dashboard.is_folder = ?", + query.SignedInUser.OrgId, dialect.BooleanStr(true)) + builder.WriteDashboardPermissionFilter(query.SignedInUser, models.PERMISSION_EDIT) + + type folderCount struct { + Count int64 + } + + resp := make([]*folderCount, 0) + if err := dbSession.SQL(builder.GetSQLString(), builder.params...).Find(&resp); err != nil { + return err + } + + query.Result = len(resp) > 0 && resp[0].Count > 0 + return nil - } - - builder := &SQLBuilder{} - builder.Write("SELECT COUNT(dashboard.id) AS count FROM dashboard WHERE dashboard.org_id = ? AND dashboard.is_folder = ?", - query.SignedInUser.OrgId, dialect.BooleanStr(true)) - builder.WriteDashboardPermissionFilter(query.SignedInUser, models.PERMISSION_EDIT) - - type folderCount struct { - Count int64 - } - - resp := make([]*folderCount, 0) - if err := x.SQL(builder.GetSQLString(), builder.params...).Find(&resp); err != nil { - return err - } - - query.Result = len(resp) > 0 && resp[0].Count > 0 - - return nil + }) } func HasAdminPermissionInFolders(ctx context.Context, query *models.HasAdminPermissionInFoldersQuery) error {