From 8a1f43a65d131f4f172f5c722429563e722c7887 Mon Sep 17 00:00:00 2001 From: Eric Leijonmarck Date: Mon, 29 Apr 2024 12:24:14 +0100 Subject: [PATCH] User: Remove the lowercasing in the query for login conflict (#87032) * refactor: remove the lowercasing in the query for login conflict * refactor: move function into the closure gs --- pkg/services/user/userimpl/store.go | 46 +++++++++++++---------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/pkg/services/user/userimpl/store.go b/pkg/services/user/userimpl/store.go index ea6aa5b69d3..597b649d3aa 100644 --- a/pkg/services/user/userimpl/store.go +++ b/pkg/services/user/userimpl/store.go @@ -211,35 +211,31 @@ func (ss *sqlStore) GetByEmail(ctx context.Context, query *user.GetUserByEmailQu // sensitive. func (ss *sqlStore) LoginConflict(ctx context.Context, login, email string) error { err := ss.db.WithDbSession(ctx, func(sess *db.Session) error { - return ss.loginConflict(sess, login, email) + users := make([]user.User, 0) + where := "email=? OR login=?" + login = strings.ToLower(login) + email = strings.ToLower(email) + + exists, err := sess.Where(where, email, login).Get(&user.User{}) + if err != nil { + return err + } + if exists { + return user.ErrUserAlreadyExists + } + if err := sess.Where("LOWER(email)=LOWER(?) OR LOWER(login)=LOWER(?)", + email, login).Find(&users); err != nil { + return err + } + + if len(users) > 1 { + return &user.ErrCaseInsensitiveLoginConflict{Users: users} + } + return nil }) return err } -func (ss *sqlStore) loginConflict(sess *db.Session, login, email string) error { - users := make([]user.User, 0) - where := "LOWER(email)=LOWER(?) OR LOWER(login)=LOWER(?)" - login = strings.ToLower(login) - email = strings.ToLower(email) - - exists, err := sess.Where(where, email, login).Get(&user.User{}) - if err != nil { - return err - } - if exists { - return user.ErrUserAlreadyExists - } - if err := sess.Where("LOWER(email)=LOWER(?) OR LOWER(login)=LOWER(?)", - email, login).Find(&users); err != nil { - return err - } - - if len(users) > 1 { - return &user.ErrCaseInsensitiveLoginConflict{Users: users} - } - return nil -} - func (ss *sqlStore) Update(ctx context.Context, cmd *user.UpdateUserCommand) error { // enforcement of lowercase due to forcement of caseinsensitive login cmd.Login = strings.ToLower(cmd.Login)