Skip to content

Commit

Permalink
Auth: Removal of conflicting users check upon creation (grafana#89045)
Browse files Browse the repository at this point in the history
fix: removal of check for conflicting users
  • Loading branch information
eleijonmarck authored Jun 12, 2024
1 parent f09f21b commit c85d10d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 35 deletions.
23 changes: 0 additions & 23 deletions pkg/services/user/model.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package user

import (
"fmt"
"strings"
"time"

"github.com/grafana/grafana/pkg/services/auth/identity"
Expand Down Expand Up @@ -219,27 +217,6 @@ type CompleteEmailVerifyCommand struct {
Code string
}

type ErrCaseInsensitiveLoginConflict struct {
Users []User
}

func (e *ErrCaseInsensitiveLoginConflict) Unwrap() error {
return ErrCaseInsensitive
}

func (e *ErrCaseInsensitiveLoginConflict) Error() string {
n := len(e.Users)

userStrings := make([]string, 0, n)
for _, v := range e.Users {
userStrings = append(userStrings, fmt.Sprintf("%s (email:%s, id:%d)", v.Login, v.Email, v.ID))
}

return fmt.Sprintf(
"Found a conflict in user login information. %d users already exist with either the same login or email: [%s].",
n, strings.Join(userStrings, ", "))
}

type Filter interface {
WhereCondition() *WhereCondition
InCondition() *InCondition
Expand Down
17 changes: 5 additions & 12 deletions pkg/services/user/userimpl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,14 @@ func (ss *sqlStore) GetByEmail(ctx context.Context, query *user.GetUserByEmailQu
}

// LoginConflict returns an error if the provided email or login are already
// associated with a user. If caseInsensitive is true the search is not case
// sensitive.
// associated with a user.
func (ss *sqlStore) LoginConflict(ctx context.Context, login, email string) error {
// enforcement of lowercase due to forcement of caseinsensitive login
login = strings.ToLower(login)
email = strings.ToLower(email)

err := ss.db.WithDbSession(ctx, func(sess *db.Session) error {
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 {
Expand All @@ -201,14 +201,7 @@ func (ss *sqlStore) LoginConflict(ctx context.Context, login, email string) erro
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
Expand Down

0 comments on commit c85d10d

Please sign in to comment.