diff --git a/pkg/services/org/orgimpl/store_test.go b/pkg/services/org/orgimpl/store_test.go index ecaf5b34297..60461b8698c 100644 --- a/pkg/services/org/orgimpl/store_test.go +++ b/pkg/services/org/orgimpl/store_test.go @@ -401,9 +401,6 @@ func TestIntegrationOrgUserDataAccess(t *testing.T) { err = orgUserStore.RemoveOrgUser(context.Background(), &remCmd) require.NoError(t, err) require.True(t, remCmd.UserWasDeleted) - - err = ss.GetSignedInUser(context.Background(), &models.GetSignedInUserQuery{UserId: ac2.ID}) - require.Equal(t, err, user.ErrUserNotFound) }) t.Run("Cannot delete last admin org user", func(t *testing.T) { diff --git a/pkg/services/sqlstore/mockstore/mockstore.go b/pkg/services/sqlstore/mockstore/mockstore.go index 9ce5d0990ea..6f6de30c37a 100644 --- a/pkg/services/sqlstore/mockstore/mockstore.go +++ b/pkg/services/sqlstore/mockstore/mockstore.go @@ -75,11 +75,6 @@ func (m *SQLStoreMock) GetUserProfile(ctx context.Context, query *models.GetUser return m.ExpectedError } -func (m *SQLStoreMock) GetSignedInUser(ctx context.Context, query *models.GetSignedInUserQuery) error { - query.Result = m.ExpectedSignedInUser - return m.ExpectedError -} - func (m *SQLStoreMock) CreateTeam(name string, email string, orgID int64) (models.Team, error) { return models.Team{ Name: name, diff --git a/pkg/services/sqlstore/org_test.go b/pkg/services/sqlstore/org_test.go index 16099f7a33d..2f79f4e4548 100644 --- a/pkg/services/sqlstore/org_test.go +++ b/pkg/services/sqlstore/org_test.go @@ -78,20 +78,6 @@ func TestIntegrationAccountDataAccess(t *testing.T) { require.NoError(t, err) }) - t.Run("Can get logged in user projection", func(t *testing.T) { - query := models.GetSignedInUserQuery{UserId: ac2.ID} - err := sqlStore.GetSignedInUser(context.Background(), &query) - - require.NoError(t, err) - require.Equal(t, query.Result.Email, "ac2@test.com") - require.Equal(t, query.Result.OrgID, ac2.OrgID) - require.Equal(t, query.Result.Name, "ac2 name") - require.Equal(t, query.Result.Login, "ac2") - require.EqualValues(t, query.Result.OrgRole, "Admin") - require.Equal(t, query.Result.OrgName, "ac2@test.com") - require.Equal(t, query.Result.IsGrafanaAdmin, true) - }) - t.Run("Can get user organizations", func(t *testing.T) { query := models.GetUserOrgListQuery{UserId: ac2.ID} err := sqlStore.GetUserOrgList(context.Background(), &query) diff --git a/pkg/services/sqlstore/store.go b/pkg/services/sqlstore/store.go index 16ca3e885aa..0b903c29290 100644 --- a/pkg/services/sqlstore/store.go +++ b/pkg/services/sqlstore/store.go @@ -20,7 +20,6 @@ type Store interface { GetDBType() core.DbType GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) error CreateUser(ctx context.Context, cmd user.CreateUserCommand) (*user.User, error) - GetSignedInUser(ctx context.Context, query *models.GetSignedInUserQuery) error WithDbSession(ctx context.Context, callback DBTransactionFunc) error WithNewDbSession(ctx context.Context, callback DBTransactionFunc) error WithTransactionalDbSession(ctx context.Context, callback DBTransactionFunc) error diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index 4064ffc696b..a587950f935 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -7,7 +7,6 @@ import ( "sort" "strconv" "strings" - "time" "github.com/grafana/grafana/pkg/events" "github.com/grafana/grafana/pkg/models" @@ -207,121 +206,6 @@ func (ss *SQLStore) GetUserOrgList(ctx context.Context, query *models.GetUserOrg }) } -func newSignedInUserCacheKey(orgID, userID int64) string { - return fmt.Sprintf("signed-in-user-%d-%d", userID, orgID) -} - -// deprecated method, use only for tests -func (ss *SQLStore) GetSignedInUserWithCacheCtx(ctx context.Context, query *models.GetSignedInUserQuery) error { - cacheKey := newSignedInUserCacheKey(query.OrgId, query.UserId) - if cached, found := ss.CacheService.Get(cacheKey); found { - cachedUser := cached.(user.SignedInUser) - query.Result = &cachedUser - return nil - } - - err := ss.GetSignedInUser(ctx, query) - if err != nil { - return err - } - - cacheKey = newSignedInUserCacheKey(query.Result.OrgID, query.UserId) - ss.CacheService.Set(cacheKey, *query.Result, time.Second*5) - return nil -} - -func (ss *SQLStore) GetSignedInUser(ctx context.Context, query *models.GetSignedInUserQuery) error { - return ss.WithDbSession(ctx, func(dbSess *DBSession) error { - orgId := "u.org_id" - if query.OrgId > 0 { - orgId = strconv.FormatInt(query.OrgId, 10) - } - - var rawSQL = `SELECT - u.id as user_id, - u.is_admin as is_grafana_admin, - u.email as email, - u.login as login, - u.name as name, - u.is_disabled as is_disabled, - u.help_flags1 as help_flags1, - u.last_seen_at as last_seen_at, - (SELECT COUNT(*) FROM org_user where org_user.user_id = u.id) as org_count, - user_auth.auth_module as external_auth_module, - user_auth.auth_id as external_auth_id, - org.name as org_name, - org_user.role as org_role, - org.id as org_id - FROM ` + dialect.Quote("user") + ` as u - LEFT OUTER JOIN user_auth on user_auth.user_id = u.id - LEFT OUTER JOIN org_user on org_user.org_id = ` + orgId + ` and org_user.user_id = u.id - LEFT OUTER JOIN org on org.id = org_user.org_id ` - - sess := dbSess.Table("user") - sess = sess.Context(ctx) - switch { - case query.UserId > 0: - sess.SQL(rawSQL+"WHERE u.id=?", query.UserId) - case query.Login != "": - if ss.Cfg.CaseInsensitiveLogin { - sess.SQL(rawSQL+"WHERE LOWER(u.login)=LOWER(?)", query.Login) - } else { - sess.SQL(rawSQL+"WHERE u.login=?", query.Login) - } - case query.Email != "": - if ss.Cfg.CaseInsensitiveLogin { - sess.SQL(rawSQL+"WHERE LOWER(u.email)=LOWER(?)", query.Email) - } else { - sess.SQL(rawSQL+"WHERE u.email=?", query.Email) - } - } - - var usr user.SignedInUser - has, err := sess.Get(&usr) - if err != nil { - return err - } else if !has { - return user.ErrUserNotFound - } - - if usr.OrgRole == "" { - usr.OrgID = -1 - usr.OrgName = "Org missing" - } - - if usr.ExternalAuthModule != "oauth_grafana_com" { - usr.ExternalAuthID = "" - } - - // tempUser is used to retrieve the teams for the signed in user for internal use. - tempUser := &user.SignedInUser{ - OrgID: usr.OrgID, - Permissions: map[int64]map[string][]string{ - usr.OrgID: { - ac.ActionTeamsRead: {ac.ScopeTeamsAll}, - }, - }, - } - getTeamsByUserQuery := &models.GetTeamsByUserQuery{ - OrgId: usr.OrgID, - UserId: usr.UserID, - SignedInUser: tempUser, - } - err = ss.GetTeamsByUser(ctx, getTeamsByUserQuery) - if err != nil { - return err - } - - usr.Teams = make([]int64, len(getTeamsByUserQuery.Result)) - for i, t := range getTeamsByUserQuery.Result { - usr.Teams[i] = t.Id - } - - query.Result = &usr - return err - }) -} - // GetTeamsByUser is used by the Guardian when checking a users' permissions // TODO: use team.Service after user service is split func (ss *SQLStore) GetTeamsByUser(ctx context.Context, query *models.GetTeamsByUserQuery) error { diff --git a/pkg/services/user/userimpl/store_test.go b/pkg/services/user/userimpl/store_test.go index 90c1d1a2aa7..48e2d2de23b 100644 --- a/pkg/services/user/userimpl/store_test.go +++ b/pkg/services/user/userimpl/store_test.go @@ -454,11 +454,11 @@ func TestIntegrationUserDataAccess(t *testing.T) { ss.CacheService.Flush() - query3 := &models.GetSignedInUserQuery{OrgId: users[1].OrgID, UserId: users[1].ID} - err = ss.GetSignedInUserWithCacheCtx(context.Background(), query3) + query3 := &user.GetSignedInUserQuery{OrgID: users[1].OrgID, UserID: users[1].ID} + query3Result, err := userStore.GetSignedInUser(context.Background(), query3) require.Nil(t, err) - require.NotNil(t, query3.Result) - require.Equal(t, query3.OrgId, users[1].OrgID) + require.NotNil(t, query3Result) + require.Equal(t, query3.OrgID, users[1].OrgID) disableCmd := user.BatchDisableUsersCommand{ UserIDs: []int64{users[0].ID, users[1].ID, users[2].ID, users[3].ID, users[4].ID}, @@ -706,6 +706,20 @@ func TestIntegrationUserDataAccess(t *testing.T) { require.Len(t, queryResult.Users, 1) require.EqualValues(t, queryResult.TotalCount, 1) }) + + t.Run("Can get logged in user projection", func(t *testing.T) { + query := user.GetSignedInUserQuery{UserID: 2} + queryResult, err := userStore.GetSignedInUser(context.Background(), &query) + + require.NoError(t, err) + assert.Equal(t, queryResult.Email, "user1@test.com") + assert.EqualValues(t, queryResult.OrgID, 2) + assert.Equal(t, queryResult.Name, "user1") + assert.Equal(t, queryResult.Login, "loginuser1") + assert.EqualValues(t, queryResult.OrgRole, "Admin") + assert.Equal(t, queryResult.OrgName, "user1@test.com") + assert.Equal(t, queryResult.IsGrafanaAdmin, false) + }) } func TestIntegrationUserUpdate(t *testing.T) {