grafana/pkg/services/libraryelements/guard.go
Marcus Efraimsson 518a0d0458
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>
2021-09-23 17:43:32 +02:00

54 lines
1.2 KiB
Go

package libraryelements
import (
"context"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/guardian"
)
func isGeneralFolder(folderID int64) bool {
return folderID == 0
}
func (l *LibraryElementService) requireSupportedElementKind(kindAsInt int64) error {
kind := models.LibraryElementKind(kindAsInt)
switch kind {
case models.PanelElement:
return nil
case models.VariableElement:
return nil
default:
return errLibraryElementUnSupportedElementKind
}
}
func (l *LibraryElementService) requirePermissionsOnFolder(ctx context.Context, user *models.SignedInUser, folderID int64) error {
if isGeneralFolder(folderID) && user.HasRole(models.ROLE_EDITOR) {
return nil
}
if isGeneralFolder(folderID) && user.HasRole(models.ROLE_VIEWER) {
return models.ErrFolderAccessDenied
}
s := dashboards.NewFolderService(user.OrgId, user, l.SQLStore)
folder, err := s.GetFolderByID(ctx, folderID)
if err != nil {
return err
}
g := guardian.New(ctx, folder.Id, user.OrgId, user)
canEdit, err := g.CanEdit()
if err != nil {
return err
}
if !canEdit {
return models.ErrFolderAccessDenied
}
return nil
}