Remove GetSignedInUserWithCacheCtx and GetSignedInUser from sqlstore (#59551)

* Remove GetSignedInUserWithCacheCtx and GetSignedInUser from sqlstore

* Delete removed method from test
This commit is contained in:
idafurjes 2022-11-30 14:28:51 +01:00 committed by GitHub
parent 0fca3cf9dd
commit d6275c58dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 18 additions and 143 deletions

View File

@ -401,9 +401,6 @@ func TestIntegrationOrgUserDataAccess(t *testing.T) {
err = orgUserStore.RemoveOrgUser(context.Background(), &remCmd) err = orgUserStore.RemoveOrgUser(context.Background(), &remCmd)
require.NoError(t, err) require.NoError(t, err)
require.True(t, remCmd.UserWasDeleted) 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) { t.Run("Cannot delete last admin org user", func(t *testing.T) {

View File

@ -75,11 +75,6 @@ func (m *SQLStoreMock) GetUserProfile(ctx context.Context, query *models.GetUser
return m.ExpectedError 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) { func (m *SQLStoreMock) CreateTeam(name string, email string, orgID int64) (models.Team, error) {
return models.Team{ return models.Team{
Name: name, Name: name,

View File

@ -78,20 +78,6 @@ func TestIntegrationAccountDataAccess(t *testing.T) {
require.NoError(t, err) 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) { t.Run("Can get user organizations", func(t *testing.T) {
query := models.GetUserOrgListQuery{UserId: ac2.ID} query := models.GetUserOrgListQuery{UserId: ac2.ID}
err := sqlStore.GetUserOrgList(context.Background(), &query) err := sqlStore.GetUserOrgList(context.Background(), &query)

View File

@ -20,7 +20,6 @@ type Store interface {
GetDBType() core.DbType GetDBType() core.DbType
GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) error GetSystemStats(ctx context.Context, query *models.GetSystemStatsQuery) error
CreateUser(ctx context.Context, cmd user.CreateUserCommand) (*user.User, 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 WithDbSession(ctx context.Context, callback DBTransactionFunc) error
WithNewDbSession(ctx context.Context, callback DBTransactionFunc) error WithNewDbSession(ctx context.Context, callback DBTransactionFunc) error
WithTransactionalDbSession(ctx context.Context, callback DBTransactionFunc) error WithTransactionalDbSession(ctx context.Context, callback DBTransactionFunc) error

View File

@ -7,7 +7,6 @@ import (
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/grafana/grafana/pkg/events" "github.com/grafana/grafana/pkg/events"
"github.com/grafana/grafana/pkg/models" "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 // GetTeamsByUser is used by the Guardian when checking a users' permissions
// TODO: use team.Service after user service is split // TODO: use team.Service after user service is split
func (ss *SQLStore) GetTeamsByUser(ctx context.Context, query *models.GetTeamsByUserQuery) error { func (ss *SQLStore) GetTeamsByUser(ctx context.Context, query *models.GetTeamsByUserQuery) error {

View File

@ -454,11 +454,11 @@ func TestIntegrationUserDataAccess(t *testing.T) {
ss.CacheService.Flush() ss.CacheService.Flush()
query3 := &models.GetSignedInUserQuery{OrgId: users[1].OrgID, UserId: users[1].ID} query3 := &user.GetSignedInUserQuery{OrgID: users[1].OrgID, UserID: users[1].ID}
err = ss.GetSignedInUserWithCacheCtx(context.Background(), query3) query3Result, err := userStore.GetSignedInUser(context.Background(), query3)
require.Nil(t, err) require.Nil(t, err)
require.NotNil(t, query3.Result) require.NotNil(t, query3Result)
require.Equal(t, query3.OrgId, users[1].OrgID) require.Equal(t, query3.OrgID, users[1].OrgID)
disableCmd := user.BatchDisableUsersCommand{ disableCmd := user.BatchDisableUsersCommand{
UserIDs: []int64{users[0].ID, users[1].ID, users[2].ID, users[3].ID, users[4].ID}, 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.Len(t, queryResult.Users, 1)
require.EqualValues(t, queryResult.TotalCount, 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) { func TestIntegrationUserUpdate(t *testing.T) {