2022-02-16 09:47:41 -08:00
|
|
|
package searchV2
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
2022-10-19 09:02:15 -04:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2023-02-06 17:38:15 +00:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-04-28 22:16:23 +04:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
2023-01-26 08:46:30 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2023-02-06 17:38:15 +00:00
|
|
|
"github.com/grafana/grafana/pkg/services/folder"
|
2022-08-10 11:56:48 +02:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2022-02-16 09:47:41 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ResourceFilter checks if a given a uid (resource identifier) check if we have the requested permission
|
2023-01-27 12:12:30 +00:00
|
|
|
type ResourceFilter func(kind entityKind, uid, parentUID string) bool
|
2022-02-16 09:47:41 -08:00
|
|
|
|
|
|
|
|
// FutureAuthService eventually implemented by the security service
|
|
|
|
|
type FutureAuthService interface {
|
2023-02-06 17:38:15 +00:00
|
|
|
GetDashboardReadFilter(ctx context.Context, orgID int64, user *user.SignedInUser) (ResourceFilter, error)
|
2022-02-16 09:47:41 -08:00
|
|
|
}
|
|
|
|
|
|
2023-01-27 12:12:30 +00:00
|
|
|
var _ FutureAuthService = (*simpleAuthService)(nil)
|
2022-04-28 22:16:23 +04:00
|
|
|
|
2023-01-27 12:12:30 +00:00
|
|
|
type simpleAuthService struct {
|
2023-02-06 17:38:15 +00:00
|
|
|
sql db.DB
|
|
|
|
|
ac accesscontrol.Service
|
|
|
|
|
folderService folder.Service
|
|
|
|
|
logger log.Logger
|
2022-02-16 09:47:41 -08:00
|
|
|
}
|
|
|
|
|
|
2023-02-06 17:38:15 +00:00
|
|
|
func (a *simpleAuthService) GetDashboardReadFilter(ctx context.Context, orgID int64, user *user.SignedInUser) (ResourceFilter, error) {
|
2023-07-19 11:37:27 +02:00
|
|
|
canReadDashboard, canReadFolder := accesscontrol.Checker(user, dashboards.ActionDashboardsRead), accesscontrol.Checker(user, dashboards.ActionFoldersRead)
|
|
|
|
|
return func(kind entityKind, uid, parent string) bool {
|
|
|
|
|
if kind == entityKindFolder {
|
|
|
|
|
scopes, err := dashboards.GetInheritedScopes(ctx, orgID, uid, a.folderService)
|
|
|
|
|
if err != nil {
|
2023-09-04 18:46:08 +02:00
|
|
|
a.logger.Debug("Could not retrieve inherited folder scopes:", "err", err)
|
2023-01-27 12:12:30 +00:00
|
|
|
}
|
2023-07-19 11:37:27 +02:00
|
|
|
scopes = append(scopes, dashboards.ScopeFoldersProvider.GetResourceScopeUID(uid))
|
|
|
|
|
return canReadFolder(scopes...)
|
|
|
|
|
} else if kind == entityKindDashboard {
|
|
|
|
|
scopes, err := dashboards.GetInheritedScopes(ctx, orgID, parent, a.folderService)
|
|
|
|
|
if err != nil {
|
2023-09-04 18:46:08 +02:00
|
|
|
a.logger.Debug("Could not retrieve inherited folder scopes:", "err", err)
|
2023-07-19 11:37:27 +02:00
|
|
|
}
|
|
|
|
|
scopes = append(scopes, dashboards.ScopeDashboardsProvider.GetResourceScopeUID(uid))
|
|
|
|
|
scopes = append(scopes, dashboards.ScopeFoldersProvider.GetResourceScopeUID(parent))
|
|
|
|
|
return canReadDashboard(scopes...)
|
2022-02-16 09:47:41 -08:00
|
|
|
}
|
2023-07-19 11:37:27 +02:00
|
|
|
return false
|
|
|
|
|
}, nil
|
2022-02-16 09:47:41 -08:00
|
|
|
}
|