Swap order of login fields (#511) (#560)

* Swap order of login fields

* Add test for username/login field conflict

(cherry picked from commit bcee2c47fd11dc716b227ee759aeeac0db20454d)
(cherry picked from commit 825ab1ab1cf1c47cc4ed1890b35b8abab8a0ab14)
This commit is contained in:
linoman
2022-10-11 12:08:46 +02:00
committed by Serge Zaitsev
parent 412f05e16f
commit 1d58ef43fb
2 changed files with 59 additions and 15 deletions
+20 -15
View File
@@ -228,28 +228,33 @@ func (ss *SQLStore) GetUserByLogin(ctx context.Context, query *models.GetUserByL
return user.ErrUserNotFound
}
// Try and find the user by login first.
// It's not sufficient to assume that a LoginOrEmail with an "@" is an email.
var where string
var has bool
var err error
// Since username can be an email address, attempt login with email address
// first if the login field has the "@" symbol.
usr := &user.User{}
where := "login=?"
if ss.Cfg.CaseInsensitiveLogin {
where = "LOWER(login)=LOWER(?)"
}
has, err := sess.Where(notServiceAccountFilter(ss)).Where(where, query.LoginOrEmail).Get(usr)
if err != nil {
return err
}
if !has && strings.Contains(query.LoginOrEmail, "@") {
// If the user wasn't found, and it contains an "@" fallback to finding the
// user by email.
if strings.Contains(query.LoginOrEmail, "@") {
where = "email=?"
if ss.Cfg.CaseInsensitiveLogin {
where = "LOWER(email)=LOWER(?)"
}
has, err = sess.Where(notServiceAccountFilter(ss)).Where(where, query.LoginOrEmail).Get(usr)
if err != nil {
return err
}
}
// Look for the login field instead of email
if !has {
usr = &user.User{}
where = "login=?"
if ss.Cfg.CaseInsensitiveLogin {
where = "LOWER(login)=LOWER(?)"
}
has, err = sess.Where(notServiceAccountFilter(ss)).Where(where, query.LoginOrEmail).Get(usr)
}
+39
View File
@@ -166,6 +166,45 @@ func TestIntegrationUserDataAccess(t *testing.T) {
})
})
t.Run("Get User by login - user_2 uses user_1.email as login", func(t *testing.T) {
ss = InitTestDB(t)
// create user_1
cmd := user.CreateUserCommand{
Email: "user_1@mail.com",
Name: "user_1",
Login: "user_1",
Password: "user_1_password",
IsDisabled: true,
}
user_1, err := ss.CreateUser(context.Background(), cmd)
require.Nil(t, err)
// create user_2
cmd = user.CreateUserCommand{
Email: "user_2@mail.com",
Name: "user_2",
Login: "user_1@mail.com",
Password: "user_2_password",
IsDisabled: true,
}
user_2, err := ss.CreateUser(context.Background(), cmd)
require.Nil(t, err)
// query user database for user_1 email
query := models.GetUserByLoginQuery{LoginOrEmail: "user_1@mail.com"}
err = ss.GetUserByLogin(context.Background(), &query)
require.Nil(t, err)
// expect user_1 as result
require.Equal(t, user_1.Email, query.Result.Email)
require.Equal(t, user_1.Login, query.Result.Login)
require.Equal(t, user_1.Name, query.Result.Name)
require.NotEqual(t, user_2.Email, query.Result.Email)
require.NotEqual(t, user_2.Login, query.Result.Login)
require.NotEqual(t, user_2.Name, query.Result.Name)
})
t.Run("Testing DB - creates and loads disabled user", func(t *testing.T) {
ss = InitTestDB(t)
cmd := user.CreateUserCommand{