[main] Login email before username (#57400)
* Swap order of login fields * Validate email field before validating the username field. Co-authored-by: linoman <2051016+linoman@users.noreply.github.com>
This commit is contained in:
@@ -170,27 +170,30 @@ func (ss *sqlStore) GetByLogin(ctx context.Context, query *user.GetUserByLoginQu
|
||||
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.
|
||||
where := "login=?"
|
||||
if ss.cfg.CaseInsensitiveLogin {
|
||||
where = "LOWER(login)=LOWER(?)"
|
||||
}
|
||||
|
||||
has, err := sess.Where(ss.notServiceAccountFilter()).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.
|
||||
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.
|
||||
if strings.Contains(query.LoginOrEmail, "@") {
|
||||
where = "email=?"
|
||||
if ss.cfg.CaseInsensitiveLogin {
|
||||
where = "LOWER(email)=LOWER(?)"
|
||||
}
|
||||
usr = &user.User{}
|
||||
has, err = sess.Where(ss.notServiceAccountFilter()).Where(where, query.LoginOrEmail).Get(usr)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Look for the login field instead of email
|
||||
if !has {
|
||||
where = "login=?"
|
||||
if ss.cfg.CaseInsensitiveLogin {
|
||||
where = "LOWER(login)=LOWER(?)"
|
||||
}
|
||||
has, err = sess.Where(ss.notServiceAccountFilter()).Where(where, query.LoginOrEmail).Get(usr)
|
||||
}
|
||||
|
||||
@@ -199,7 +202,6 @@ func (ss *sqlStore) GetByLogin(ctx context.Context, query *user.GetUserByLoginQu
|
||||
} else if !has {
|
||||
return user.ErrUserNotFound
|
||||
}
|
||||
|
||||
if ss.cfg.CaseInsensitiveLogin {
|
||||
if err := ss.userCaseInsensitiveLoginConflict(ctx, sess, usr.Login, usr.Email); err != nil {
|
||||
return err
|
||||
@@ -207,10 +209,8 @@ func (ss *sqlStore) GetByLogin(ctx context.Context, query *user.GetUserByLoginQu
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return usr, nil
|
||||
|
||||
return usr, err
|
||||
}
|
||||
|
||||
func (ss *sqlStore) GetByEmail(ctx context.Context, query *user.GetUserByEmailQuery) (*user.User, error) {
|
||||
|
||||
Reference in New Issue
Block a user