mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Replace signed in user for identity.requester (#74048)
* Make identity.Requester available at Context * Clean pkg/services/guardian/guardian.go * Clean guardian provider and guardian AC * Clean pkg/api/team.go * Clean ctxhandler, datasources, plugin and live * Clean dashboards and guardian * Implement NewUserDisplayDTOFromRequester * Change status code numbers for http constants * Upgrade signature of ngalert services * log parsing errors instead of throwing error
This commit is contained in:
@@ -6,9 +6,9 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/auth/identity"
|
||||
"github.com/grafana/grafana/pkg/services/dashboards"
|
||||
"github.com/grafana/grafana/pkg/services/folder"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
@@ -16,14 +16,14 @@ var _ DashboardGuardian = new(accessControlDashboardGuardian)
|
||||
|
||||
// NewAccessControlDashboardGuardianByDashboard creates a dashboard guardian by the provided dashboardId.
|
||||
func NewAccessControlDashboardGuardian(
|
||||
ctx context.Context, cfg *setting.Cfg, dashboardId int64, user *user.SignedInUser,
|
||||
ctx context.Context, cfg *setting.Cfg, dashboardId int64, user identity.Requester,
|
||||
ac accesscontrol.AccessControl, dashboardService dashboards.DashboardService,
|
||||
) (DashboardGuardian, error) {
|
||||
var dashboard *dashboards.Dashboard
|
||||
if dashboardId != 0 {
|
||||
q := &dashboards.GetDashboardQuery{
|
||||
ID: dashboardId,
|
||||
OrgID: user.OrgID,
|
||||
OrgID: user.GetOrgID(),
|
||||
}
|
||||
|
||||
qResult, err := dashboardService.GetDashboard(ctx, q)
|
||||
@@ -65,14 +65,14 @@ func NewAccessControlDashboardGuardian(
|
||||
|
||||
// NewAccessControlDashboardGuardianByDashboard creates a dashboard guardian by the provided dashboardUID.
|
||||
func NewAccessControlDashboardGuardianByUID(
|
||||
ctx context.Context, cfg *setting.Cfg, dashboardUID string, user *user.SignedInUser,
|
||||
ctx context.Context, cfg *setting.Cfg, dashboardUID string, user identity.Requester,
|
||||
ac accesscontrol.AccessControl, dashboardService dashboards.DashboardService,
|
||||
) (DashboardGuardian, error) {
|
||||
var dashboard *dashboards.Dashboard
|
||||
if dashboardUID != "" {
|
||||
q := &dashboards.GetDashboardQuery{
|
||||
UID: dashboardUID,
|
||||
OrgID: user.OrgID,
|
||||
OrgID: user.GetOrgID(),
|
||||
}
|
||||
|
||||
qResult, err := dashboardService.GetDashboard(ctx, q)
|
||||
@@ -116,7 +116,7 @@ func NewAccessControlDashboardGuardianByUID(
|
||||
// This constructor should be preferred over the other two if the dashboard in available
|
||||
// since it avoids querying the database for fetching the dashboard.
|
||||
func NewAccessControlDashboardGuardianByDashboard(
|
||||
ctx context.Context, cfg *setting.Cfg, dashboard *dashboards.Dashboard, user *user.SignedInUser,
|
||||
ctx context.Context, cfg *setting.Cfg, dashboard *dashboards.Dashboard, user identity.Requester,
|
||||
ac accesscontrol.AccessControl, dashboardService dashboards.DashboardService,
|
||||
) (DashboardGuardian, error) {
|
||||
if dashboard != nil && dashboard.IsFolder {
|
||||
@@ -148,7 +148,7 @@ func NewAccessControlDashboardGuardianByDashboard(
|
||||
|
||||
// NewAccessControlFolderGuardian creates a folder guardian by the provided folder.
|
||||
func NewAccessControlFolderGuardian(
|
||||
ctx context.Context, cfg *setting.Cfg, f *folder.Folder, user *user.SignedInUser,
|
||||
ctx context.Context, cfg *setting.Cfg, f *folder.Folder, user identity.Requester,
|
||||
ac accesscontrol.AccessControl, dashboardService dashboards.DashboardService,
|
||||
) (DashboardGuardian, error) {
|
||||
return &accessControlFolderGuardian{
|
||||
@@ -168,7 +168,7 @@ type accessControlBaseGuardian struct {
|
||||
cfg *setting.Cfg
|
||||
ctx context.Context
|
||||
log log.Logger
|
||||
user *user.SignedInUser
|
||||
user identity.Requester
|
||||
ac accesscontrol.AccessControl
|
||||
dashboardService dashboards.DashboardService
|
||||
}
|
||||
@@ -309,12 +309,13 @@ func (a *accessControlFolderGuardian) CanCreate(folderID int64, isFolder bool) (
|
||||
|
||||
func (a *accessControlDashboardGuardian) evaluate(evaluator accesscontrol.Evaluator) (bool, error) {
|
||||
ok, err := a.ac.Evaluate(a.ctx, a.user, evaluator)
|
||||
namespaceID, userID := a.user.GetNamespacedID()
|
||||
if err != nil {
|
||||
id := 0
|
||||
if a.dashboard != nil {
|
||||
id = int(a.dashboard.ID)
|
||||
}
|
||||
a.log.Debug("Failed to evaluate access control to dashboard", "error", err, "userId", a.user.UserID, "id", id)
|
||||
a.log.Debug("Failed to evaluate access control to dashboard", "error", err, "namespaceID", namespaceID, "userId", userID, "id", id)
|
||||
}
|
||||
|
||||
if !ok && err == nil {
|
||||
@@ -322,7 +323,7 @@ func (a *accessControlDashboardGuardian) evaluate(evaluator accesscontrol.Evalua
|
||||
if a.dashboard != nil {
|
||||
id = int(a.dashboard.ID)
|
||||
}
|
||||
a.log.Debug("Access denied to dashboard", "userId", a.user.UserID, "id", id, "permissions", evaluator.GoString())
|
||||
a.log.Debug("Access denied to dashboard", "namespaceID", namespaceID, "userId", userID, "id", id, "permissions", evaluator.GoString())
|
||||
}
|
||||
|
||||
return ok, err
|
||||
@@ -330,6 +331,7 @@ func (a *accessControlDashboardGuardian) evaluate(evaluator accesscontrol.Evalua
|
||||
|
||||
func (a *accessControlFolderGuardian) evaluate(evaluator accesscontrol.Evaluator) (bool, error) {
|
||||
ok, err := a.ac.Evaluate(a.ctx, a.user, evaluator)
|
||||
namespaceID, userID := a.user.GetNamespacedID()
|
||||
if err != nil {
|
||||
uid := ""
|
||||
orgID := 0
|
||||
@@ -337,7 +339,7 @@ func (a *accessControlFolderGuardian) evaluate(evaluator accesscontrol.Evaluator
|
||||
uid = a.folder.UID
|
||||
orgID = int(a.folder.OrgID)
|
||||
}
|
||||
a.log.Debug("Failed to evaluate access control to folder", "error", err, "userId", a.user.UserID, "orgID", orgID, "uid", uid)
|
||||
a.log.Debug("Failed to evaluate access control to folder", "error", err, "namespaceID", namespaceID, "userId", userID, "orgID", orgID, "uid", uid)
|
||||
}
|
||||
|
||||
if !ok && err == nil {
|
||||
@@ -347,7 +349,7 @@ func (a *accessControlFolderGuardian) evaluate(evaluator accesscontrol.Evaluator
|
||||
uid = a.folder.UID
|
||||
orgID = int(a.folder.OrgID)
|
||||
}
|
||||
a.log.Debug("Access denied to folder", "userId", a.user.UserID, "orgID", orgID, "uid", uid, "permissions", evaluator.GoString())
|
||||
a.log.Debug("Access denied to folder", "namespaceID", namespaceID, "userId", userID, "orgID", orgID, "uid", uid, "permissions", evaluator.GoString())
|
||||
}
|
||||
|
||||
return ok, err
|
||||
@@ -357,7 +359,7 @@ func (a *accessControlDashboardGuardian) loadParentFolder(folderID int64) (*dash
|
||||
if folderID == 0 {
|
||||
return &dashboards.Dashboard{UID: accesscontrol.GeneralFolderUID}, nil
|
||||
}
|
||||
folderQuery := &dashboards.GetDashboardQuery{ID: folderID, OrgID: a.user.OrgID}
|
||||
folderQuery := &dashboards.GetDashboardQuery{ID: folderID, OrgID: a.user.GetOrgID()}
|
||||
folderQueryResult, err := a.dashboardService.GetDashboard(a.ctx, folderQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -369,7 +371,7 @@ func (a *accessControlFolderGuardian) loadParentFolder(folderID int64) (*dashboa
|
||||
if folderID == 0 {
|
||||
return &dashboards.Dashboard{UID: accesscontrol.GeneralFolderUID}, nil
|
||||
}
|
||||
folderQuery := &dashboards.GetDashboardQuery{ID: folderID, OrgID: a.user.OrgID}
|
||||
folderQuery := &dashboards.GetDashboardQuery{ID: folderID, OrgID: a.user.GetOrgID()}
|
||||
folderQueryResult, err := a.dashboardService.GetDashboard(a.ctx, folderQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user