diff --git a/pkg/api/index.go b/pkg/api/index.go index eb25cad293e..5c375da5f73 100644 --- a/pkg/api/index.go +++ b/pkg/api/index.go @@ -13,6 +13,7 @@ import ( "github.com/grafana/grafana/pkg/api/dtos" "github.com/grafana/grafana/pkg/middleware" 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" "github.com/grafana/grafana/pkg/services/dashboards" "github.com/grafana/grafana/pkg/services/featuremgmt" @@ -169,10 +170,22 @@ func (hs *HTTPServer) setIndexViewData(c *contextmodel.ReqContext) (*dtos.IndexV return &data, nil } -func (hs *HTTPServer) buildUserAnalyticsSettings(ctx context.Context, signedInUser *user.SignedInUser) dtos.AnalyticsSettings { - identifier := signedInUser.Email + "@" + setting.AppUrl +func (hs *HTTPServer) buildUserAnalyticsSettings(ctx context.Context, signedInUser identity.Requester) dtos.AnalyticsSettings { + 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) { hs.log.Error("Failed to get auth info for analytics", "error", err) } diff --git a/pkg/services/libraryelements/database.go b/pkg/services/libraryelements/database.go index e75ac79f90c..2932bfa3e17 100644 --- a/pkg/services/libraryelements/database.go +++ b/pkg/services/libraryelements/database.go @@ -18,7 +18,6 @@ import ( "github.com/grafana/grafana/pkg/services/org" "github.com/grafana/grafana/pkg/services/search" "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/util" ) @@ -206,10 +205,10 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn } // 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 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 { return err } @@ -490,7 +489,8 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI 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 if toFolderID == -1 { toFolderID = fromFolderID @@ -514,13 +514,13 @@ func (l *LibraryElementService) handleFolderIDPatches(ctx context.Context, eleme } // 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 if err := l.requireSupportedElementKind(cmd.Kind); err != nil { return model.LibraryElementDTO{}, err } 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 { return err } @@ -537,15 +537,26 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU 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) { 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{ ID: elementInDB.ID, - OrgID: signedInUser.OrgID, + OrgID: signedInUser.GetOrgID(), FolderID: cmd.FolderID, UID: updateUID, Name: cmd.Name, @@ -557,7 +568,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU Created: elementInDB.Created, CreatedBy: elementInDB.CreatedBy, Updated: time.Now(), - UpdatedBy: signedInUser.UserID, + UpdatedBy: userID, } if cmd.Name == "" { @@ -603,8 +614,8 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU }, UpdatedBy: librarypanel.LibraryElementDTOMetaUser{ Id: libraryElement.UpdatedBy, - Name: signedInUser.Login, - AvatarUrl: dtos.GetGravatarUrl(signedInUser.Email), + Name: signedInUser.GetLogin(), + 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. -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) recursiveQueriesAreSupported, err := l.SQLStore.RecursiveQueriesAreSupported() 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 { - element, err := getLibraryElement(l.SQLStore.GetDialect(), session, uid, signedInUser.OrgID) + element, err := getLibraryElement(l.SQLStore.GetDialect(), session, uid, signedInUser.GetOrgID()) if err != nil { 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(" INNER JOIN dashboard AS dashboard on lec.connection_id = dashboard.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, "") } if err := session.SQL(builder.GetSQLString(), builder.GetParams()...).Find(&libraryElementConnections); err != nil {