2022-03-03 08:05:47 -06:00
|
|
|
package guardian
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-10-19 08:02:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2022-03-03 08:05:47 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2022-05-17 13:52:22 -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-09-20 11:58:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/team"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2023-03-16 04:54:01 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2022-03-03 08:05:47 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type Provider struct{}
|
|
|
|
|
2022-05-10 08:48:47 -05:00
|
|
|
func ProvideService(
|
2023-03-16 04:54:01 -05:00
|
|
|
cfg *setting.Cfg, store db.DB, ac accesscontrol.AccessControl,
|
2022-05-10 08:48:47 -05:00
|
|
|
folderPermissionsService accesscontrol.FolderPermissionsService, dashboardPermissionsService accesscontrol.DashboardPermissionsService,
|
2022-09-20 11:58:04 -05:00
|
|
|
dashboardService dashboards.DashboardService, teamService team.Service,
|
2022-05-10 08:48:47 -05:00
|
|
|
) *Provider {
|
2022-04-25 03:42:09 -05:00
|
|
|
if !ac.IsDisabled() {
|
2022-03-03 08:05:47 -06:00
|
|
|
// TODO: Fix this hack, see https://github.com/grafana/grafana-enterprise/issues/2935
|
2023-03-16 04:54:01 -05:00
|
|
|
InitAccessControlGuardian(cfg, store, ac, folderPermissionsService, dashboardPermissionsService, dashboardService)
|
2022-03-21 04:49:49 -05:00
|
|
|
} else {
|
2023-03-16 04:54:01 -05:00
|
|
|
InitLegacyGuardian(cfg, store, dashboardService, teamService)
|
2022-03-03 08:05:47 -06:00
|
|
|
}
|
|
|
|
return &Provider{}
|
|
|
|
}
|
2022-03-21 04:49:49 -05:00
|
|
|
|
2023-03-16 04:54:01 -05:00
|
|
|
func InitLegacyGuardian(cfg *setting.Cfg, store db.DB, dashSvc dashboards.DashboardService, teamSvc team.Service) {
|
2022-12-15 08:34:17 -06:00
|
|
|
New = func(ctx context.Context, dashId int64, orgId int64, user *user.SignedInUser) (DashboardGuardian, error) {
|
2023-03-16 04:54:01 -05:00
|
|
|
return newDashboardGuardian(ctx, cfg, dashId, orgId, user, store, dashSvc, teamSvc)
|
2022-03-21 04:49:49 -05:00
|
|
|
}
|
2022-12-15 08:34:17 -06:00
|
|
|
|
|
|
|
NewByUID = func(ctx context.Context, dashUID string, orgId int64, user *user.SignedInUser) (DashboardGuardian, error) {
|
2023-03-16 04:54:01 -05:00
|
|
|
return newDashboardGuardianByUID(ctx, cfg, dashUID, orgId, user, store, dashSvc, teamSvc)
|
2022-12-15 08:34:17 -06:00
|
|
|
}
|
|
|
|
|
2023-01-16 09:33:55 -06:00
|
|
|
NewByDashboard = func(ctx context.Context, dash *dashboards.Dashboard, orgId int64, user *user.SignedInUser) (DashboardGuardian, error) {
|
2023-03-16 04:54:01 -05:00
|
|
|
return newDashboardGuardianByDashboard(ctx, cfg, dash, orgId, user, store, dashSvc, teamSvc)
|
2022-12-15 08:34:17 -06:00
|
|
|
}
|
2023-07-25 07:31:12 -05:00
|
|
|
|
|
|
|
NewByFolder = func(ctx context.Context, f *folder.Folder, orgId int64, user *user.SignedInUser) (DashboardGuardian, error) {
|
|
|
|
return newDashboardGuardianByFolder(ctx, cfg, f, orgId, user, store, dashSvc, teamSvc)
|
|
|
|
}
|
2022-03-21 04:49:49 -05:00
|
|
|
}
|
|
|
|
|
2022-05-10 08:48:47 -05:00
|
|
|
func InitAccessControlGuardian(
|
2023-03-16 04:54:01 -05:00
|
|
|
cfg *setting.Cfg, store db.DB, ac accesscontrol.AccessControl, folderPermissionsService accesscontrol.FolderPermissionsService,
|
2022-05-17 13:52:22 -05:00
|
|
|
dashboardPermissionsService accesscontrol.DashboardPermissionsService, dashboardService dashboards.DashboardService,
|
2022-05-10 08:48:47 -05:00
|
|
|
) {
|
2022-12-15 08:34:17 -06:00
|
|
|
New = func(ctx context.Context, dashId int64, orgId int64, user *user.SignedInUser) (DashboardGuardian, error) {
|
2023-03-16 04:54:01 -05:00
|
|
|
return NewAccessControlDashboardGuardian(ctx, cfg, dashId, user, store, ac, folderPermissionsService, dashboardPermissionsService, dashboardService)
|
2022-03-21 04:49:49 -05:00
|
|
|
}
|
2022-12-15 08:34:17 -06:00
|
|
|
|
|
|
|
NewByUID = func(ctx context.Context, dashUID string, orgId int64, user *user.SignedInUser) (DashboardGuardian, error) {
|
2023-03-16 04:54:01 -05:00
|
|
|
return NewAccessControlDashboardGuardianByUID(ctx, cfg, dashUID, user, store, ac, folderPermissionsService, dashboardPermissionsService, dashboardService)
|
2022-12-15 08:34:17 -06:00
|
|
|
}
|
|
|
|
|
2023-01-16 09:33:55 -06:00
|
|
|
NewByDashboard = func(ctx context.Context, dash *dashboards.Dashboard, orgId int64, user *user.SignedInUser) (DashboardGuardian, error) {
|
2023-03-16 04:54:01 -05:00
|
|
|
return NewAccessControlDashboardGuardianByDashboard(ctx, cfg, dash, user, store, ac, folderPermissionsService, dashboardPermissionsService, dashboardService)
|
2022-12-15 08:34:17 -06:00
|
|
|
}
|
2023-07-25 07:31:12 -05:00
|
|
|
|
|
|
|
NewByFolder = func(ctx context.Context, f *folder.Folder, orgId int64, user *user.SignedInUser) (DashboardGuardian, error) {
|
|
|
|
return NewAccessControlFolderGuardian(ctx, cfg, f, user, store, ac, folderPermissionsService, dashboardPermissionsService, dashboardService)
|
|
|
|
}
|
2022-03-21 04:49:49 -05:00
|
|
|
}
|