[v9.2.x] Login: Fix panic when UpsertUser is called without ReqContext (#62574)
* Login: Fix panic when UpsertUser is called without ReqContext (#62539) (cherry picked from commitb1151dd118) (cherry picked from commit679cc18648)
This commit is contained in:
@@ -54,6 +54,11 @@ func (ls *Implementation) CreateUser(cmd user.CreateUserCommand) (*user.User, er
|
||||
|
||||
// UpsertUser updates an existing user, or if it doesn't exist, inserts a new one.
|
||||
func (ls *Implementation) UpsertUser(ctx context.Context, cmd *models.UpsertUserCommand) error {
|
||||
var logger log.Logger = logger
|
||||
if cmd.ReqContext != nil && cmd.ReqContext.Logger != nil {
|
||||
logger = cmd.ReqContext.Logger
|
||||
}
|
||||
|
||||
extUser := cmd.ExternalUser
|
||||
|
||||
usr, errAuthLookup := ls.AuthInfoService.LookupAndUpdate(ctx, &models.GetUserByAuthInfoQuery{
|
||||
@@ -67,13 +72,13 @@ func (ls *Implementation) UpsertUser(ctx context.Context, cmd *models.UpsertUser
|
||||
}
|
||||
|
||||
if !cmd.SignupAllowed {
|
||||
cmd.ReqContext.Logger.Warn("Not allowing login, user not found in internal user database and allow signup = false", "authmode", extUser.AuthModule)
|
||||
logger.Warn("Not allowing login, user not found in internal user database and allow signup = false", "authmode", extUser.AuthModule)
|
||||
return login.ErrSignupNotAllowed
|
||||
}
|
||||
|
||||
limitReached, errLimit := ls.QuotaService.QuotaReached(cmd.ReqContext, "user")
|
||||
if errLimit != nil {
|
||||
cmd.ReqContext.Logger.Warn("Error getting user quota.", "error", errLimit)
|
||||
logger.Warn("Error getting user quota.", "error", errLimit)
|
||||
return login.ErrGettingUserQuota
|
||||
}
|
||||
if limitReached {
|
||||
|
||||
@@ -8,6 +8,9 @@ import (
|
||||
|
||||
"github.com/go-kit/log"
|
||||
"github.com/go-kit/log/level"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/login"
|
||||
"github.com/grafana/grafana/pkg/services/login/logintest"
|
||||
@@ -16,8 +19,6 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/services/user/usertest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_syncOrgRoles_doesNotBreakWhenTryingToRemoveLastOrgAdmin(t *testing.T) {
|
||||
@@ -118,6 +119,28 @@ func Test_teamSync(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpsertUser_crashOnLog_issue62538(t *testing.T) {
|
||||
authInfoMock := &logintest.AuthInfoServiceFake{}
|
||||
authInfoMock.ExpectedError = user.ErrUserNotFound
|
||||
loginsvc := Implementation{
|
||||
QuotaService: "aimpl.Service{},
|
||||
AuthInfoService: authInfoMock,
|
||||
}
|
||||
|
||||
email := "test_user@example.org"
|
||||
upsertCmd := &models.UpsertUserCommand{
|
||||
ExternalUser: &models.ExternalUserInfo{Email: email},
|
||||
UserLookupParams: models.UserLookupParams{Email: &email},
|
||||
SignupAllowed: false,
|
||||
}
|
||||
|
||||
var err error
|
||||
require.NotPanics(t, func() {
|
||||
err = loginsvc.UpsertUser(context.Background(), upsertCmd)
|
||||
})
|
||||
require.ErrorIs(t, err, login.ErrSignupNotAllowed)
|
||||
}
|
||||
|
||||
func createSimpleUser() user.User {
|
||||
user := user.User{
|
||||
ID: 1,
|
||||
|
||||
Reference in New Issue
Block a user