Chore: Add user service method SetUsingOrg and GetSignedInUserWithCacheCtx (#53343)

* Chore: Add user service method SetUsingOrg

* Chore: Add user service method GetSignedInUserWithCacheCtx

* Use method GetSignedInUserWithCacheCtx from user service

* Fix lint after rebase

* Fix lint

* Fix lint error

* roll back some changes

* Roll back changes in api and middleware

* Add xorm tags to SignedInUser ID fields
This commit is contained in:
idafurjes
2022-08-11 13:28:55 +02:00
committed by GitHub
parent ca72cd570e
commit a14621fff6
191 changed files with 1108 additions and 1049 deletions

View File

@@ -47,7 +47,7 @@ func (l *LibraryElementService) createHandler(c *models.ReqContext) response.Res
if *cmd.FolderUID == "" {
cmd.FolderID = 0
} else {
folder, err := l.folderService.GetFolderByUID(c.Req.Context(), c.SignedInUser, c.OrgId, *cmd.FolderUID)
folder, err := l.folderService.GetFolderByUID(c.Req.Context(), c.SignedInUser, c.OrgID, *cmd.FolderUID)
if err != nil || folder == nil {
return response.Error(http.StatusBadRequest, "failed to get folder", err)
}
@@ -61,7 +61,7 @@ func (l *LibraryElementService) createHandler(c *models.ReqContext) response.Res
}
if element.FolderID != 0 {
folder, err := l.folderService.GetFolderByID(c.Req.Context(), c.SignedInUser, element.FolderID, c.OrgId)
folder, err := l.folderService.GetFolderByID(c.Req.Context(), c.SignedInUser, element.FolderID, c.OrgID)
if err != nil {
return response.Error(http.StatusInternalServerError, "failed to get folder", err)
}
@@ -174,7 +174,7 @@ func (l *LibraryElementService) patchHandler(c *models.ReqContext) response.Resp
if *cmd.FolderUID == "" {
cmd.FolderID = 0
} else {
folder, err := l.folderService.GetFolderByUID(c.Req.Context(), c.SignedInUser, c.OrgId, *cmd.FolderUID)
folder, err := l.folderService.GetFolderByUID(c.Req.Context(), c.SignedInUser, c.OrgID, *cmd.FolderUID)
if err != nil || folder == nil {
return response.Error(http.StatusBadRequest, "failed to get folder", err)
}
@@ -188,7 +188,7 @@ func (l *LibraryElementService) patchHandler(c *models.ReqContext) response.Resp
}
if element.FolderID != 0 {
folder, err := l.folderService.GetFolderByID(c.Req.Context(), c.SignedInUser, element.FolderID, c.OrgId)
folder, err := l.folderService.GetFolderByID(c.Req.Context(), c.SignedInUser, element.FolderID, c.OrgID)
if err != nil {
return response.Error(http.StatusInternalServerError, "failed to get folder", err)
}

View File

@@ -115,7 +115,7 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn
}
}
element := LibraryElement{
OrgID: signedInUser.OrgId,
OrgID: signedInUser.OrgID,
FolderID: cmd.FolderID,
UID: createUID,
Name: cmd.Name,
@@ -126,8 +126,8 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn
Created: time.Now(),
Updated: time.Now(),
CreatedBy: signedInUser.UserId,
UpdatedBy: signedInUser.UserId,
CreatedBy: signedInUser.UserID,
UpdatedBy: signedInUser.UserID,
}
if err := syncFieldsWithModel(&element); err != nil {
@@ -182,7 +182,7 @@ func (l *LibraryElementService) createLibraryElement(c context.Context, signedIn
func (l *LibraryElementService) deleteLibraryElement(c context.Context, signedInUser *user.SignedInUser, uid string) (int64, error) {
var elementID int64
err := l.SQLStore.WithTransactionalDbSession(c, func(session *sqlstore.DBSession) error {
element, err := getLibraryElement(l.SQLStore.Dialect, session, uid, signedInUser.OrgId)
element, err := getLibraryElement(l.SQLStore.Dialect, session, uid, signedInUser.OrgID)
if err != nil {
return err
}
@@ -294,7 +294,7 @@ func getLibraryElements(c context.Context, store *sqlstore.SQLStore, signedInUse
// getLibraryElementByUid gets a Library Element by uid.
func (l *LibraryElementService) getLibraryElementByUid(c context.Context, signedInUser *user.SignedInUser, UID string) (LibraryElementDTO, error) {
libraryElements, err := getLibraryElements(c, l.SQLStore, signedInUser, []Pair{{key: "org_id", value: signedInUser.OrgId}, {key: "uid", value: UID}})
libraryElements, err := getLibraryElements(c, l.SQLStore, signedInUser, []Pair{{key: "org_id", value: signedInUser.OrgID}, {key: "uid", value: UID}})
if err != nil {
return LibraryElementDTO{}, err
}
@@ -307,7 +307,7 @@ func (l *LibraryElementService) getLibraryElementByUid(c context.Context, signed
// getLibraryElementByName gets a Library Element by name.
func (l *LibraryElementService) getLibraryElementsByName(c context.Context, signedInUser *user.SignedInUser, name string) ([]LibraryElementDTO, error) {
return getLibraryElements(c, l.SQLStore, signedInUser, []Pair{{"org_id", signedInUser.OrgId}, {"name", name}})
return getLibraryElements(c, l.SQLStore, signedInUser, []Pair{{"org_id", signedInUser.OrgID}, {"name", name}})
}
// getAllLibraryElements gets all Library Elements.
@@ -335,7 +335,7 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
builder.Write(", 'General' as folder_name ")
builder.Write(", '' as folder_uid ")
builder.Write(getFromLibraryElementDTOWithMeta(l.SQLStore.Dialect))
builder.Write(` WHERE le.org_id=? AND le.folder_id=0`, signedInUser.OrgId)
builder.Write(` WHERE le.org_id=? AND le.folder_id=0`, signedInUser.OrgID)
writeKindSQL(query, &builder)
writeSearchStringSQL(query, l.SQLStore, &builder)
writeExcludeSQL(query, &builder)
@@ -347,7 +347,7 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
builder.Write(", dashboard.uid as folder_uid ")
builder.Write(getFromLibraryElementDTOWithMeta(l.SQLStore.Dialect))
builder.Write(" INNER JOIN dashboard AS dashboard on le.folder_id = dashboard.id AND le.folder_id<>0")
builder.Write(` WHERE le.org_id=?`, signedInUser.OrgId)
builder.Write(` WHERE le.org_id=?`, signedInUser.OrgID)
writeKindSQL(query, &builder)
writeSearchStringSQL(query, l.SQLStore, &builder)
writeExcludeSQL(query, &builder)
@@ -405,7 +405,7 @@ func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedI
var libraryElements []LibraryElement
countBuilder := sqlstore.SQLBuilder{}
countBuilder.Write("SELECT * FROM library_element AS le")
countBuilder.Write(` WHERE le.org_id=?`, signedInUser.OrgId)
countBuilder.Write(` WHERE le.org_id=?`, signedInUser.OrgID)
writeKindSQL(query, &countBuilder)
writeSearchStringSQL(query, l.SQLStore, &countBuilder)
writeExcludeSQL(query, &countBuilder)
@@ -460,7 +460,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
return LibraryElementDTO{}, err
}
err := l.SQLStore.WithTransactionalDbSession(c, func(session *sqlstore.DBSession) error {
elementInDB, err := getLibraryElement(l.SQLStore.Dialect, session, uid, signedInUser.OrgId)
elementInDB, err := getLibraryElement(l.SQLStore.Dialect, session, uid, signedInUser.OrgID)
if err != nil {
return err
}
@@ -477,7 +477,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
return errLibraryElementUIDTooLong
}
_, err := getLibraryElement(l.SQLStore.Dialect, session, updateUID, signedInUser.OrgId)
_, err := getLibraryElement(l.SQLStore.Dialect, session, updateUID, signedInUser.OrgID)
if !errors.Is(err, ErrLibraryElementNotFound) {
return errLibraryElementAlreadyExists
}
@@ -485,7 +485,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
var libraryElement = LibraryElement{
ID: elementInDB.ID,
OrgID: signedInUser.OrgId,
OrgID: signedInUser.OrgID,
FolderID: cmd.FolderID,
UID: updateUID,
Name: cmd.Name,
@@ -497,7 +497,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
Created: elementInDB.Created,
CreatedBy: elementInDB.CreatedBy,
Updated: time.Now(),
UpdatedBy: signedInUser.UserId,
UpdatedBy: signedInUser.UserID,
}
if cmd.Name == "" {
@@ -558,7 +558,7 @@ func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInU
func (l *LibraryElementService) getConnections(c context.Context, signedInUser *user.SignedInUser, uid string) ([]LibraryElementConnectionDTO, error) {
connections := make([]LibraryElementConnectionDTO, 0)
err := l.SQLStore.WithDbSession(c, func(session *sqlstore.DBSession) error {
element, err := getLibraryElement(l.SQLStore.Dialect, session, uid, signedInUser.OrgId)
element, err := getLibraryElement(l.SQLStore.Dialect, session, uid, signedInUser.OrgID)
if err != nil {
return err
}
@@ -661,7 +661,7 @@ func (l *LibraryElementService) connectElementsToDashboardID(c context.Context,
return err
}
for _, elementUID := range elementUIDs {
element, err := getLibraryElement(l.SQLStore.Dialect, session, elementUID, signedInUser.OrgId)
element, err := getLibraryElement(l.SQLStore.Dialect, session, elementUID, signedInUser.OrgID)
if err != nil {
return err
}
@@ -674,7 +674,7 @@ func (l *LibraryElementService) connectElementsToDashboardID(c context.Context,
Kind: 1,
ConnectionID: dashboardID,
Created: time.Now(),
CreatedBy: signedInUser.UserId,
CreatedBy: signedInUser.UserID,
}
if _, err := session.Insert(&connection); err != nil {
if l.SQLStore.Dialect.IsUniqueConstraintViolation(err) {
@@ -706,7 +706,7 @@ func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c context.Conte
var folderUIDs []struct {
ID int64 `xorm:"id"`
}
err := session.SQL("SELECT id from dashboard WHERE uid=? AND org_id=? AND is_folder=?", folderUID, signedInUser.OrgId, l.SQLStore.Dialect.BooleanStr(true)).Find(&folderUIDs)
err := session.SQL("SELECT id from dashboard WHERE uid=? AND org_id=? AND is_folder=?", folderUID, signedInUser.OrgID, l.SQLStore.Dialect.BooleanStr(true)).Find(&folderUIDs)
if err != nil {
return err
}
@@ -730,7 +730,7 @@ func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c context.Conte
sql := "SELECT lec.connection_id FROM library_element AS le"
sql += " INNER JOIN " + models.LibraryElementConnectionTableName + " AS lec on le.id = lec.element_id"
sql += " WHERE le.folder_id=? AND le.org_id=?"
err = session.SQL(sql, folderID, signedInUser.OrgId).Find(&connectionIDs)
err = session.SQL(sql, folderID, signedInUser.OrgID).Find(&connectionIDs)
if err != nil {
return err
}
@@ -741,7 +741,7 @@ func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c context.Conte
var elementIDs []struct {
ID int64 `xorm:"id"`
}
err = session.SQL("SELECT id from library_element WHERE folder_id=? AND org_id=?", folderID, signedInUser.OrgId).Find(&elementIDs)
err = session.SQL("SELECT id from library_element WHERE folder_id=? AND org_id=?", folderID, signedInUser.OrgID).Find(&elementIDs)
if err != nil {
return err
}
@@ -751,7 +751,7 @@ func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c context.Conte
return err
}
}
if _, err := session.Exec("DELETE FROM library_element WHERE folder_id=? AND org_id=?", folderID, signedInUser.OrgId); err != nil {
if _, err := session.Exec("DELETE FROM library_element WHERE folder_id=? AND org_id=?", folderID, signedInUser.OrgID); err != nil {
return err
}

View File

@@ -34,12 +34,12 @@ func (l *LibraryElementService) requireEditPermissionsOnFolder(ctx context.Conte
if isGeneralFolder(folderID) && user.HasRole(org.RoleViewer) {
return dashboards.ErrFolderAccessDenied
}
folder, err := l.folderService.GetFolderByID(ctx, user, folderID, user.OrgId)
folder, err := l.folderService.GetFolderByID(ctx, user, folderID, user.OrgID)
if err != nil {
return err
}
g := guardian.New(ctx, folder.Id, user.OrgId, user)
g := guardian.New(ctx, folder.Id, user.OrgID, user)
canEdit, err := g.CanEdit()
if err != nil {
@@ -57,12 +57,12 @@ func (l *LibraryElementService) requireViewPermissionsOnFolder(ctx context.Conte
return nil
}
folder, err := l.folderService.GetFolderByID(ctx, user, folderID, user.OrgId)
folder, err := l.folderService.GetFolderByID(ctx, user, folderID, user.OrgID)
if err != nil {
return err
}
g := guardian.New(ctx, folder.Id, user.OrgId, user)
g := guardian.New(ctx, folder.Id, user.OrgID, user)
canView, err := g.CanView()
if err != nil {

View File

@@ -35,7 +35,7 @@ func TestDeleteLibraryElement(t *testing.T) {
scenarioWithPanel(t, "When an admin tries to delete a library panel in another org, it should fail",
func(t *testing.T, sc scenarioContext) {
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
sc.reqContext.SignedInUser.OrgId = 2
sc.reqContext.SignedInUser.OrgID = 2
sc.reqContext.SignedInUser.OrgRole = org.RoleAdmin
resp := sc.service.deleteHandler(sc.reqContext)
require.Equal(t, 404, resp.Status())

View File

@@ -1240,7 +1240,7 @@ func TestGetAllLibraryElements(t *testing.T) {
require.Equal(t, int64(1), result.Result.Elements[0].FolderID)
require.Equal(t, "Text - Library Panel", result.Result.Elements[0].Name)
sc.reqContext.SignedInUser.OrgId = 2
sc.reqContext.SignedInUser.OrgID = 2
sc.reqContext.SignedInUser.OrgRole = org.RoleAdmin
resp = sc.service.getAllHandler(sc.reqContext)
require.Equal(t, 200, resp.Status())

View File

@@ -183,7 +183,7 @@ func TestGetLibraryElement(t *testing.T) {
scenarioWithPanel(t, "When an admin tries to get a library panel that exists in an other org, it should fail",
func(t *testing.T, sc scenarioContext) {
sc.reqContext.SignedInUser.OrgId = 2
sc.reqContext.SignedInUser.OrgID = 2
sc.reqContext.SignedInUser.OrgRole = org.RoleAdmin
// by uid

View File

@@ -290,7 +290,7 @@ func TestPatchLibraryElement(t *testing.T) {
scenarioWithPanel(t, "When another admin tries to patch a library panel, it should change UpdatedBy successfully and return correct result",
func(t *testing.T, sc scenarioContext) {
cmd := PatchLibraryElementCommand{FolderID: -1, Version: 1, Kind: int64(models.PanelElement)}
sc.reqContext.UserId = 2
sc.reqContext.UserID = 2
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
sc.ctx.Req.Body = mockRequestBody(cmd)
resp := sc.service.patchHandler(sc.reqContext)
@@ -347,7 +347,7 @@ func TestPatchLibraryElement(t *testing.T) {
Version: 1,
Kind: int64(models.PanelElement),
}
sc.reqContext.OrgId = 2
sc.reqContext.OrgID = 2
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})
sc.ctx.Req.Body = mockRequestBody(cmd)
resp := sc.service.patchHandler(sc.reqContext)

View File

@@ -265,7 +265,7 @@ func createDashboard(t *testing.T, sqlStore *sqlstore.SQLStore, user user.Signed
dashItem := &dashboards.SaveDashboardDTO{
Dashboard: dash,
Message: "",
OrgId: user.OrgId,
OrgId: user.OrgID,
User: &user,
Overwrite: false,
}
@@ -309,7 +309,7 @@ func createFolderWithACL(t *testing.T, sqlStore *sqlstore.SQLStore, title string
features, folderPermissions, ac, busmock.New(),
)
t.Logf("Creating folder with title and UID %q", title)
folder, err := s.CreateFolder(context.Background(), &user, user.OrgId, title, title)
folder, err := s.CreateFolder(context.Background(), &user, user.OrgID, title, title)
require.NoError(t, err)
updateFolderACL(t, dashboardStore, folder.Id, items)
@@ -424,11 +424,11 @@ func testScenario(t *testing.T, desc string, fn func(t *testing.T, sc scenarioCo
}
usr := user.SignedInUser{
UserId: 1,
UserID: 1,
Name: "Signed In User",
Login: "signed_in_user",
Email: "signed.in.user@test.com",
OrgId: orgID,
OrgID: orgID,
OrgRole: role,
LastSeenAt: time.Now(),
}