From 5ec176cada3d8adf651f844e3f707bc469495abd Mon Sep 17 00:00:00 2001 From: linoman <2051016+linoman@users.noreply.github.com> Date: Fri, 16 Sep 2022 10:46:44 +0200 Subject: [PATCH] Swap order of login fields --- pkg/services/sqlstore/user.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/services/sqlstore/user.go b/pkg/services/sqlstore/user.go index d119422ce1c..2d76e7ec35a 100644 --- a/pkg/services/sqlstore/user.go +++ b/pkg/services/sqlstore/user.go @@ -295,20 +295,25 @@ func (ss *SQLStore) GetUserByLogin(ctx context.Context, query *models.GetUserByL return models.ErrUserNotFound } - // Try and find the user by login first. - // It's not sufficient to assume that a LoginOrEmail with an "@" is an email. + var has bool + var err error user := &models.User{Login: query.LoginOrEmail} - has, err := sess.Where(notServiceAccountFilter(ss)).Get(user) - 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. + // 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, "@") { user = &models.User{Email: query.LoginOrEmail} has, err = sess.Get(user) + + if err != nil { + return err + } + } + + // Lookup the login field instead of email field + if !has { + user = &models.User{Login: query.LoginOrEmail} + has, err = sess.Where(notServiceAccountFilter(ss)).Get(user) } if err != nil {