Swap order of login fields (#511)
* 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:
committed by
Sofia Papagiannaki
parent
b571acc1dc
commit
85f581105a
@@ -226,28 +226,33 @@ func (ss *SQLStore) GetUserByLogin(ctx context.Context, query *models.GetUserByL
|
|||||||
return user.ErrUserNotFound
|
return user.ErrUserNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try and find the user by login first.
|
var where string
|
||||||
// It's not sufficient to assume that a LoginOrEmail with an "@" is an email.
|
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{}
|
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=?"
|
where = "email=?"
|
||||||
if ss.Cfg.CaseInsensitiveLogin {
|
if ss.Cfg.CaseInsensitiveLogin {
|
||||||
where = "LOWER(email)=LOWER(?)"
|
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{}
|
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)
|
has, err = sess.Where(notServiceAccountFilter(ss)).Where(where, query.LoginOrEmail).Get(usr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -165,6 +165,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) {
|
t.Run("Testing DB - creates and loads disabled user", func(t *testing.T) {
|
||||||
ss = InitTestDB(t)
|
ss = InitTestDB(t)
|
||||||
cmd := user.CreateUserCommand{
|
cmd := user.CreateUserCommand{
|
||||||
|
|||||||
Reference in New Issue
Block a user