Automatic Merge
Этот коммит содержится в:
Mattermost Build
2026-01-07 14:17:32 +02:00
коммит произвёл GitHub
родитель a07b1d7a8c
Коммит fc8b22242d
2 изменённых файлов: 100 добавлений и 13 удалений

Просмотреть файл

@@ -124,23 +124,25 @@ func (a *App) GetUserForLogin(c request.CTX, id, loginId string) (*model.User, *
enableUsername := *a.Config().EmailSettings.EnableSignInWithUsername
enableEmail := *a.Config().EmailSettings.EnableSignInWithEmail
// If we are given a userID then fail if we can't find a user with that ID
if id != "" {
user, err := a.GetUser(id)
if err != nil {
if err.Id != MissingAccountError {
err.StatusCode = http.StatusInternalServerError
if enableEmail || enableUsername {
// If we are given a userID then fail if we can't find a user with that ID
if id != "" {
user, err := a.GetUser(id)
if err != nil {
if err.Id != MissingAccountError {
err.StatusCode = http.StatusInternalServerError
return nil, err
}
err.StatusCode = http.StatusBadRequest
return nil, err
}
err.StatusCode = http.StatusBadRequest
return nil, err
return user, nil
}
return user, nil
}
// Try to get the user by username/email
if user, err := a.Srv().Store().User().GetForLogin(loginId, enableUsername, enableEmail); err == nil {
return user, nil
// Try to get the user by username/email
if user, err := a.Srv().Store().User().GetForLogin(loginId, enableUsername, enableEmail); err == nil {
return user, nil
}
}
// Try to get the user with LDAP if enabled

Просмотреть файл

@@ -80,3 +80,88 @@ func TestCWSLogin(t *testing.T) {
require.Nil(t, user)
})
}
func TestGetUserForLogin(t *testing.T) {
mainHelper.Parallel(t)
th := Setup(t).InitBasic()
t.Run("Should get user with username when sign in with username is enabled", func(t *testing.T) {
th.UpdateConfig(func(config *model.Config) {
config.EmailSettings.EnableSignInWithUsername = model.NewPointer(true)
})
user, appErr := th.App.GetUserForLogin(th.Context, "", th.BasicUser.Username)
require.Nil(t, appErr)
require.NotNil(t, user)
require.Equal(t, th.BasicUser.Username, user.Username)
})
t.Run("Should not get user with username when sign in with username is disabled", func(t *testing.T) {
th.UpdateConfig(func(config *model.Config) {
config.EmailSettings.EnableSignInWithUsername = model.NewPointer(false)
})
user, appErr := th.App.GetUserForLogin(th.Context, "", th.BasicUser.Username)
require.NotNil(t, appErr)
require.Equal(t, http.StatusBadRequest, appErr.StatusCode)
require.Nil(t, user)
})
t.Run("Should get user with email when sign in with email is enabled", func(t *testing.T) {
th.UpdateConfig(func(config *model.Config) {
config.EmailSettings.EnableSignInWithEmail = model.NewPointer(true)
})
user, appErr := th.App.GetUserForLogin(th.Context, "", th.BasicUser.Email)
require.Nil(t, appErr)
require.NotNil(t, user)
require.Equal(t, th.BasicUser.Username, user.Username)
})
t.Run("Should not user with email when sign in with email is disabled", func(t *testing.T) {
th.UpdateConfig(func(config *model.Config) {
config.EmailSettings.EnableSignInWithEmail = model.NewPointer(false)
})
user, appErr := th.App.GetUserForLogin(th.Context, "", th.BasicUser.Email)
require.NotNil(t, appErr)
require.Equal(t, http.StatusBadRequest, appErr.StatusCode)
require.Nil(t, user)
})
t.Run("Should get user with user ID when sign in with email is enabled", func(t *testing.T) {
th.UpdateConfig(func(config *model.Config) {
config.EmailSettings.EnableSignInWithEmail = model.NewPointer(true)
config.EmailSettings.EnableSignInWithUsername = model.NewPointer(false)
})
user, appErr := th.App.GetUserForLogin(th.Context, th.BasicUser.Id, "")
require.Nil(t, appErr)
require.NotNil(t, user)
require.Equal(t, th.BasicUser.Username, user.Username)
})
t.Run("Should get user with user ID when sign in with username is enabled", func(t *testing.T) {
th.UpdateConfig(func(config *model.Config) {
config.EmailSettings.EnableSignInWithEmail = model.NewPointer(false)
config.EmailSettings.EnableSignInWithUsername = model.NewPointer(true)
})
user, appErr := th.App.GetUserForLogin(th.Context, th.BasicUser.Id, "")
require.Nil(t, appErr)
require.NotNil(t, user)
require.Equal(t, th.BasicUser.Username, user.Username)
})
t.Run("Should not get user with user ID when both sign in with email and username are disabled", func(t *testing.T) {
th.UpdateConfig(func(config *model.Config) {
config.EmailSettings.EnableSignInWithEmail = model.NewPointer(false)
config.EmailSettings.EnableSignInWithUsername = model.NewPointer(false)
})
user, appErr := th.App.GetUserForLogin(th.Context, th.BasicUser.Id, "")
require.NotNil(t, appErr)
require.Equal(t, http.StatusBadRequest, appErr.StatusCode)
require.Nil(t, user)
})
}