2017-06-16 20:25:24 -05:00
|
|
|
package guardian
|
|
|
|
|
|
|
|
import (
|
2021-09-23 10:43:32 -05:00
|
|
|
"context"
|
2018-02-26 12:12:01 -06:00
|
|
|
|
2023-08-30 09:51:18 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/auth/identity"
|
2022-06-01 13:16:26 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2023-07-25 07:31:12 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/folder"
|
2022-12-15 08:34:17 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
2017-06-16 20:25:24 -05:00
|
|
|
)
|
|
|
|
|
2018-02-26 12:12:01 -06:00
|
|
|
var (
|
2023-08-22 05:52:24 -05:00
|
|
|
ErrGuardianGetDashboardFailure = errutil.Internal("guardian.getDashboardFailure", errutil.WithPublicMessage("Failed to get dashboard"))
|
|
|
|
ErrGuardianDashboardNotFound = errutil.NotFound("guardian.dashboardNotFound")
|
|
|
|
ErrGuardianFolderNotFound = errutil.NotFound("guardian.folderNotFound")
|
2018-02-26 12:12:01 -06:00
|
|
|
)
|
|
|
|
|
2018-02-19 04:12:56 -06:00
|
|
|
// DashboardGuardian to be used for guard against operations without access on dashboard and acl
|
|
|
|
type DashboardGuardian interface {
|
|
|
|
CanSave() (bool, error)
|
|
|
|
CanEdit() (bool, error)
|
|
|
|
CanView() (bool, error)
|
|
|
|
CanAdmin() (bool, error)
|
2022-03-03 08:05:47 -06:00
|
|
|
CanDelete() (bool, error)
|
|
|
|
CanCreate(folderID int64, isFolder bool) (bool, error)
|
2018-02-19 04:12:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// New factory for creating a new dashboard guardian instance
|
2022-03-03 08:05:47 -06:00
|
|
|
// When using access control this function is replaced on startup and the AccessControlDashboardGuardian is returned
|
2023-08-30 09:51:18 -05:00
|
|
|
var New = func(ctx context.Context, dashId int64, orgId int64, user identity.Requester) (DashboardGuardian, error) {
|
2022-03-21 04:49:49 -05:00
|
|
|
panic("no guardian factory implementation provided")
|
|
|
|
}
|
|
|
|
|
2022-12-15 08:34:17 -06:00
|
|
|
// NewByUID factory for creating a new dashboard guardian instance
|
|
|
|
// When using access control this function is replaced on startup and the AccessControlDashboardGuardian is returned
|
2023-08-30 09:51:18 -05:00
|
|
|
var NewByUID = func(ctx context.Context, dashUID string, orgId int64, user identity.Requester) (DashboardGuardian, error) {
|
2022-12-15 08:34:17 -06:00
|
|
|
panic("no guardian factory implementation provided")
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewByDashboard factory for creating a new dashboard guardian instance
|
|
|
|
// When using access control this function is replaced on startup and the AccessControlDashboardGuardian is returned
|
2023-08-30 09:51:18 -05:00
|
|
|
var NewByDashboard = func(ctx context.Context, dash *dashboards.Dashboard, orgId int64, user identity.Requester) (DashboardGuardian, error) {
|
2022-12-15 08:34:17 -06:00
|
|
|
panic("no guardian factory implementation provided")
|
|
|
|
}
|
|
|
|
|
2023-07-25 07:31:12 -05:00
|
|
|
// NewByFolder factory for creating a new folder guardian instance
|
|
|
|
// When using access control this function is replaced on startup and the AccessControlDashboardGuardian is returned
|
2023-08-30 09:51:18 -05:00
|
|
|
var NewByFolder = func(ctx context.Context, f *folder.Folder, orgId int64, user identity.Requester) (DashboardGuardian, error) {
|
2023-07-25 07:31:12 -05:00
|
|
|
panic("no guardian factory implementation provided")
|
|
|
|
}
|
|
|
|
|
2020-11-17 04:51:31 -06:00
|
|
|
// nolint:unused
|
2018-02-20 11:08:19 -06:00
|
|
|
type FakeDashboardGuardian struct {
|
2023-08-30 03:14:17 -05:00
|
|
|
DashID int64
|
|
|
|
DashUID string
|
|
|
|
OrgID int64
|
2023-08-30 09:51:18 -05:00
|
|
|
User identity.Requester
|
2023-08-30 03:14:17 -05:00
|
|
|
CanSaveValue bool
|
|
|
|
CanEditValue bool
|
|
|
|
CanViewValue bool
|
|
|
|
CanAdminValue bool
|
2018-02-20 11:08:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *FakeDashboardGuardian) CanSave() (bool, error) {
|
|
|
|
return g.CanSaveValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *FakeDashboardGuardian) CanEdit() (bool, error) {
|
|
|
|
return g.CanEditValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *FakeDashboardGuardian) CanView() (bool, error) {
|
|
|
|
return g.CanViewValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *FakeDashboardGuardian) CanAdmin() (bool, error) {
|
|
|
|
return g.CanAdminValue, nil
|
|
|
|
}
|
|
|
|
|
2022-03-03 08:05:47 -06:00
|
|
|
func (g *FakeDashboardGuardian) CanDelete() (bool, error) {
|
|
|
|
return g.CanSaveValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *FakeDashboardGuardian) CanCreate(_ int64, _ bool) (bool, error) {
|
|
|
|
return g.CanSaveValue, nil
|
|
|
|
}
|
|
|
|
|
2020-11-17 04:51:31 -06:00
|
|
|
// nolint:unused
|
2018-02-20 11:08:19 -06:00
|
|
|
func MockDashboardGuardian(mock *FakeDashboardGuardian) {
|
2023-08-30 09:51:18 -05:00
|
|
|
New = func(_ context.Context, dashID int64, orgId int64, user identity.Requester) (DashboardGuardian, error) {
|
2023-01-20 07:58:47 -06:00
|
|
|
mock.OrgID = orgId
|
2022-12-15 08:34:17 -06:00
|
|
|
mock.DashID = dashID
|
|
|
|
mock.User = user
|
|
|
|
return mock, nil
|
|
|
|
}
|
|
|
|
|
2023-08-30 09:51:18 -05:00
|
|
|
NewByUID = func(_ context.Context, dashUID string, orgId int64, user identity.Requester) (DashboardGuardian, error) {
|
2023-01-20 07:58:47 -06:00
|
|
|
mock.OrgID = orgId
|
2022-12-15 08:34:17 -06:00
|
|
|
mock.DashUID = dashUID
|
|
|
|
mock.User = user
|
|
|
|
return mock, nil
|
|
|
|
}
|
|
|
|
|
2023-08-30 09:51:18 -05:00
|
|
|
NewByDashboard = func(_ context.Context, dash *dashboards.Dashboard, orgId int64, user identity.Requester) (DashboardGuardian, error) {
|
2023-01-20 07:58:47 -06:00
|
|
|
mock.OrgID = orgId
|
2023-01-16 09:33:55 -06:00
|
|
|
mock.DashUID = dash.UID
|
|
|
|
mock.DashID = dash.ID
|
2018-02-20 11:08:19 -06:00
|
|
|
mock.User = user
|
2022-12-15 08:34:17 -06:00
|
|
|
return mock, nil
|
2018-02-20 11:08:19 -06:00
|
|
|
}
|
2023-07-25 07:31:12 -05:00
|
|
|
|
2023-08-30 09:51:18 -05:00
|
|
|
NewByFolder = func(_ context.Context, f *folder.Folder, orgId int64, user identity.Requester) (DashboardGuardian, error) {
|
2023-07-25 07:31:12 -05:00
|
|
|
mock.OrgID = orgId
|
|
|
|
mock.DashUID = f.UID
|
|
|
|
mock.DashID = f.ID
|
|
|
|
mock.User = user
|
|
|
|
return mock, nil
|
|
|
|
}
|
2018-02-20 11:08:19 -06:00
|
|
|
}
|