Identity: Port Annotations and Analytics to Identity.Requester (#76104)

* Port analytics to requester

* Port annotations to identity.Request
This commit is contained in:
Jo
2023-10-06 11:38:20 +02:00
committed by GitHub
parent 729dafaff9
commit 342af2d078
2 changed files with 41 additions and 17 deletions
+16 -3
View File
@@ -13,6 +13,7 @@ import (
"github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/middleware"
ac "github.com/grafana/grafana/pkg/services/accesscontrol" ac "github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/auth/identity"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/dashboards" "github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/featuremgmt" "github.com/grafana/grafana/pkg/services/featuremgmt"
@@ -169,10 +170,22 @@ func (hs *HTTPServer) setIndexViewData(c *contextmodel.ReqContext) (*dtos.IndexV
return &data, nil return &data, nil
} }
func (hs *HTTPServer) buildUserAnalyticsSettings(ctx context.Context, signedInUser *user.SignedInUser) dtos.AnalyticsSettings { func (hs *HTTPServer) buildUserAnalyticsSettings(ctx context.Context, signedInUser identity.Requester) dtos.AnalyticsSettings {
identifier := signedInUser.Email + "@" + setting.AppUrl namespace, id := signedInUser.GetNamespacedID()
// Anonymous users do not have an email or auth info
if namespace != identity.NamespaceUser {
return dtos.AnalyticsSettings{Identifier: "@" + setting.AppUrl}
}
authInfo, err := hs.authInfoService.GetAuthInfo(ctx, &login.GetAuthInfoQuery{UserId: signedInUser.UserID}) userID, err := identity.IntIdentifier(namespace, id)
if err != nil {
hs.log.Error("Failed to parse user ID", "error", err)
return dtos.AnalyticsSettings{Identifier: "@" + setting.AppUrl}
}
identifier := signedInUser.GetEmail() + "@" + setting.AppUrl
authInfo, err := hs.authInfoService.GetAuthInfo(ctx, &login.GetAuthInfoQuery{UserId: userID})
if err != nil && !errors.Is(err, user.ErrUserNotFound) { if err != nil && !errors.Is(err, user.ErrUserNotFound) {
hs.log.Error("Failed to get auth info for analytics", "error", err) hs.log.Error("Failed to get auth info for analytics", "error", err)
} }
+25 -14
View File
@@ -18,7 +18,6 @@ import (
"github.com/grafana/grafana/pkg/services/org" "github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/search" "github.com/grafana/grafana/pkg/services/search"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator" "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util" "github.com/grafana/grafana/pkg/util"
) )
@@ -206,10 +205,10 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn
} }
// deleteLibraryElement deletes a library element. // deleteLibraryElement deletes a library element.
func (l *LibraryElementService) deleteLibraryElement(c context.Context, signedInUser *user.SignedInUser, uid string) (int64, error) { func (l *LibraryElementService) deleteLibraryElement(c context.Context, signedInUser identity.Requester, uid string) (int64, error) {
var elementID int64 var elementID int64
err := l.SQLStore.WithTransactionalDbSession(c, func(session *db.Session) error { err := l.SQLStore.WithTransactionalDbSession(c, func(session *db.Session) error {
element, err := getLibraryElement(l.SQLStore.GetDialect(), session, uid, signedInUser.OrgID) element, err := getLibraryElement(l.SQLStore.GetDialect(), session, uid, signedInUser.GetOrgID())
if err != nil { if err != nil {
return err return err
} }
@@ -490,7 +489,8 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
return result, err return result, err
} }
func (l *LibraryElementService) handleFolderIDPatches(ctx context.Context, elementToPatch *model.LibraryElement, fromFolderID int64, toFolderID int64, user *user.SignedInUser) error { func (l *LibraryElementService) handleFolderIDPatches(ctx context.Context, elementToPatch *model.LibraryElement,
fromFolderID int64, toFolderID int64, user identity.Requester) error {
// FolderID was not provided in the PATCH request // FolderID was not provided in the PATCH request
if toFolderID == -1 { if toFolderID == -1 {
toFolderID = fromFolderID toFolderID = fromFolderID
@@ -514,13 +514,13 @@ func (l *LibraryElementService) handleFolderIDPatches(ctx context.Context, eleme
} }
// patchLibraryElement updates a Library Element. // patchLibraryElement updates a Library Element.
func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInUser *user.SignedInUser, cmd model.PatchLibraryElementCommand, uid string) (model.LibraryElementDTO, error) { func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInUser identity.Requester, cmd model.PatchLibraryElementCommand, uid string) (model.LibraryElementDTO, error) {
var dto model.LibraryElementDTO var dto model.LibraryElementDTO
if err := l.requireSupportedElementKind(cmd.Kind); err != nil { if err := l.requireSupportedElementKind(cmd.Kind); err != nil {
return model.LibraryElementDTO{}, err return model.LibraryElementDTO{}, err
} }
err := l.SQLStore.WithTransactionalDbSession(c, func(session *db.Session) error { err := l.SQLStore.WithTransactionalDbSession(c, func(session *db.Session) error {
elementInDB, err := getLibraryElement(l.SQLStore.GetDialect(), session, uid, signedInUser.OrgID) elementInDB, err := getLibraryElement(l.SQLStore.GetDialect(), session, uid, signedInUser.GetOrgID())
if err != nil { if err != nil {
return err return err
} }
@@ -537,15 +537,26 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
return model.ErrLibraryElementUIDTooLong return model.ErrLibraryElementUIDTooLong
} }
_, err := getLibraryElement(l.SQLStore.GetDialect(), session, updateUID, signedInUser.OrgID) _, err := getLibraryElement(l.SQLStore.GetDialect(), session, updateUID, signedInUser.GetOrgID())
if !errors.Is(err, model.ErrLibraryElementNotFound) { if !errors.Is(err, model.ErrLibraryElementNotFound) {
return model.ErrLibraryElementAlreadyExists return model.ErrLibraryElementAlreadyExists
} }
} }
var userID int64
namespaceID, identifier := signedInUser.GetNamespacedID()
switch namespaceID {
case identity.NamespaceUser, identity.NamespaceServiceAccount:
var errID error
userID, errID = identity.IntIdentifier(namespaceID, identifier)
if errID != nil {
l.log.Warn("Error while parsing userID", "namespaceID", namespaceID, "userID", identifier, "err", errID)
}
}
var libraryElement = model.LibraryElement{ var libraryElement = model.LibraryElement{
ID: elementInDB.ID, ID: elementInDB.ID,
OrgID: signedInUser.OrgID, OrgID: signedInUser.GetOrgID(),
FolderID: cmd.FolderID, FolderID: cmd.FolderID,
UID: updateUID, UID: updateUID,
Name: cmd.Name, Name: cmd.Name,
@@ -557,7 +568,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
Created: elementInDB.Created, Created: elementInDB.Created,
CreatedBy: elementInDB.CreatedBy, CreatedBy: elementInDB.CreatedBy,
Updated: time.Now(), Updated: time.Now(),
UpdatedBy: signedInUser.UserID, UpdatedBy: userID,
} }
if cmd.Name == "" { if cmd.Name == "" {
@@ -603,8 +614,8 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
}, },
UpdatedBy: librarypanel.LibraryElementDTOMetaUser{ UpdatedBy: librarypanel.LibraryElementDTOMetaUser{
Id: libraryElement.UpdatedBy, Id: libraryElement.UpdatedBy,
Name: signedInUser.Login, Name: signedInUser.GetLogin(),
AvatarUrl: dtos.GetGravatarUrl(signedInUser.Email), AvatarUrl: dtos.GetGravatarUrl(signedInUser.GetEmail()),
}, },
}, },
} }
@@ -615,7 +626,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
} }
// getConnections gets all connections for a Library Element. // getConnections gets all connections for a Library Element.
func (l *LibraryElementService) getConnections(c context.Context, signedInUser *user.SignedInUser, uid string) ([]model.LibraryElementConnectionDTO, error) { func (l *LibraryElementService) getConnections(c context.Context, signedInUser identity.Requester, uid string) ([]model.LibraryElementConnectionDTO, error) {
connections := make([]model.LibraryElementConnectionDTO, 0) connections := make([]model.LibraryElementConnectionDTO, 0)
recursiveQueriesAreSupported, err := l.SQLStore.RecursiveQueriesAreSupported() recursiveQueriesAreSupported, err := l.SQLStore.RecursiveQueriesAreSupported()
if err != nil { if err != nil {
@@ -623,7 +634,7 @@ func (l *LibraryElementService) getConnections(c context.Context, signedInUser *
} }
err = l.SQLStore.WithDbSession(c, func(session *db.Session) error { err = l.SQLStore.WithDbSession(c, func(session *db.Session) error {
element, err := getLibraryElement(l.SQLStore.GetDialect(), session, uid, signedInUser.OrgID) element, err := getLibraryElement(l.SQLStore.GetDialect(), session, uid, signedInUser.GetOrgID())
if err != nil { if err != nil {
return err return err
} }
@@ -634,7 +645,7 @@ func (l *LibraryElementService) getConnections(c context.Context, signedInUser *
builder.Write(" LEFT JOIN " + l.SQLStore.GetDialect().Quote("user") + " AS u1 ON lec.created_by = u1.id") builder.Write(" LEFT JOIN " + l.SQLStore.GetDialect().Quote("user") + " AS u1 ON lec.created_by = u1.id")
builder.Write(" INNER JOIN dashboard AS dashboard on lec.connection_id = dashboard.id") builder.Write(" INNER JOIN dashboard AS dashboard on lec.connection_id = dashboard.id")
builder.Write(` WHERE lec.element_id=?`, element.ID) builder.Write(` WHERE lec.element_id=?`, element.ID)
if signedInUser.OrgRole != org.RoleAdmin { if signedInUser.GetOrgRole() != org.RoleAdmin {
builder.WriteDashboardPermissionFilter(signedInUser, dashboards.PERMISSION_VIEW, "") builder.WriteDashboardPermissionFilter(signedInUser, dashboards.PERMISSION_VIEW, "")
} }
if err := session.SQL(builder.GetSQLString(), builder.GetParams()...).Find(&libraryElementConnections); err != nil { if err := session.SQL(builder.GetSQLString(), builder.GetParams()...).Find(&libraryElementConnections); err != nil {