Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore user autoregistration with email addresses (#19261) #19312

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 39 additions & 29 deletions services/auth/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ import (
// UserSignIn validates user name and password.
func UserSignIn(username, password string) (*user_model.User, *auth.Source, error) {
var user *user_model.User
isEmail := false
if strings.Contains(username, "@") {
isEmail = true
emailAddress := user_model.EmailAddress{LowerEmail: strings.ToLower(strings.TrimSpace(username))}
// check same email
has, err := db.GetEngine(db.DefaultContext).Where("is_activated=?", true).Get(&emailAddress)
has, err := db.GetEngine(db.DefaultContext).Get(&emailAddress)
if err != nil {
return nil, nil, err
}
if !has {
return nil, nil, user_model.ErrEmailAddressNotExist{
Email: username,
if has {
if !emailAddress.IsActivated {
return nil, nil, user_model.ErrEmailAddressNotExist{
Email: username,
}
}
user = &user_model.User{ID: emailAddress.UID}
}
user = &user_model.User{ID: emailAddress.UID}
} else {
trimmedUsername := strings.TrimSpace(username)
if len(trimmedUsername) == 0 {
Expand All @@ -45,38 +49,40 @@ func UserSignIn(username, password string) (*user_model.User, *auth.Source, erro
user = &user_model.User{LowerName: strings.ToLower(trimmedUsername)}
}

hasUser, err := user_model.GetUser(user)
if err != nil {
return nil, nil, err
}

if hasUser {
source, err := auth.GetSourceByID(user.LoginSource)
if user != nil {
hasUser, err := user_model.GetUser(user)
if err != nil {
return nil, nil, err
}

if !source.IsActive {
return nil, nil, oauth2.ErrAuthSourceNotActived
}
if hasUser {
source, err := auth.GetSourceByID(user.LoginSource)
if err != nil {
return nil, nil, err
}

authenticator, ok := source.Cfg.(PasswordAuthenticator)
if !ok {
return nil, nil, smtp.ErrUnsupportedLoginType
}
if !source.IsActive {
return nil, nil, oauth2.ErrAuthSourceNotActived
}

user, err := authenticator.Authenticate(user, user.LoginName, password)
if err != nil {
return nil, nil, err
}
authenticator, ok := source.Cfg.(PasswordAuthenticator)
if !ok {
return nil, nil, smtp.ErrUnsupportedLoginType
}

// WARN: DON'T check user.IsActive, that will be checked on reqSign so that
// user could be hint to resend confirm email.
if user.ProhibitLogin {
return nil, nil, user_model.ErrUserProhibitLogin{UID: user.ID, Name: user.Name}
}
user, err := authenticator.Authenticate(user, user.LoginName, password)
if err != nil {
return nil, nil, err
}

return user, source, nil
// WARN: DON'T check user.IsActive, that will be checked on reqSign so that
// user could be hint to resend confirm email.
if user.ProhibitLogin {
return nil, nil, user_model.ErrUserProhibitLogin{UID: user.ID, Name: user.Name}
}

return user, source, nil
}
}

sources, err := auth.AllActiveSources()
Expand Down Expand Up @@ -111,5 +117,9 @@ func UserSignIn(username, password string) (*user_model.User, *auth.Source, erro
}
}

if isEmail {
return nil, nil, user_model.ErrEmailAddressNotExist{Email: username}
}

return nil, nil, user_model.ErrUserNotExist{Name: username}
}