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

Backport #19261

Unfortunately #18789 disabled autoregistration using email addresses as they would
be shortcut to email address does not exist.

This PR attempts to restore autoregistration by allowing an unknown email address
to percolate through to the autoregistration path of UserSignin.

Fix #19256

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
zeripath 2022-04-03 01:36:47 +01:00 committed by GitHub
parent 471a1e8111
commit 14a6aafb50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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 {
if has {
if !emailAddress.IsActivated {
return nil, nil, user_model.ErrEmailAddressNotExist{
Email: username,
}
}
user = &user_model.User{ID: emailAddress.UID}
}
} else {
trimmedUsername := strings.TrimSpace(username)
if len(trimmedUsername) == 0 {
@ -45,6 +49,7 @@ func UserSignIn(username, password string) (*user_model.User, *auth.Source, erro
user = &user_model.User{LowerName: strings.ToLower(trimmedUsername)}
}
if user != nil {
hasUser, err := user_model.GetUser(user)
if err != nil {
return nil, nil, err
@ -78,6 +83,7 @@ func UserSignIn(username, password string) (*user_model.User, *auth.Source, erro
return user, source, nil
}
}
sources, err := auth.AllActiveSources()
if err != nil {
@ -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}
}