Skip to content

Commit 14a6aaf

Browse files
authored
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 <[email protected]>
1 parent 471a1e8 commit 14a6aaf

File tree

1 file changed

+39
-29
lines changed

1 file changed

+39
-29
lines changed

services/auth/signin.go

+39-29
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,23 @@ import (
2323
// UserSignIn validates user name and password.
2424
func UserSignIn(username, password string) (*user_model.User, *auth.Source, error) {
2525
var user *user_model.User
26+
isEmail := false
2627
if strings.Contains(username, "@") {
28+
isEmail = true
2729
emailAddress := user_model.EmailAddress{LowerEmail: strings.ToLower(strings.TrimSpace(username))}
2830
// check same email
29-
has, err := db.GetEngine(db.DefaultContext).Where("is_activated=?", true).Get(&emailAddress)
31+
has, err := db.GetEngine(db.DefaultContext).Get(&emailAddress)
3032
if err != nil {
3133
return nil, nil, err
3234
}
33-
if !has {
34-
return nil, nil, user_model.ErrEmailAddressNotExist{
35-
Email: username,
35+
if has {
36+
if !emailAddress.IsActivated {
37+
return nil, nil, user_model.ErrEmailAddressNotExist{
38+
Email: username,
39+
}
3640
}
41+
user = &user_model.User{ID: emailAddress.UID}
3742
}
38-
user = &user_model.User{ID: emailAddress.UID}
3943
} else {
4044
trimmedUsername := strings.TrimSpace(username)
4145
if len(trimmedUsername) == 0 {
@@ -45,38 +49,40 @@ func UserSignIn(username, password string) (*user_model.User, *auth.Source, erro
4549
user = &user_model.User{LowerName: strings.ToLower(trimmedUsername)}
4650
}
4751

48-
hasUser, err := user_model.GetUser(user)
49-
if err != nil {
50-
return nil, nil, err
51-
}
52-
53-
if hasUser {
54-
source, err := auth.GetSourceByID(user.LoginSource)
52+
if user != nil {
53+
hasUser, err := user_model.GetUser(user)
5554
if err != nil {
5655
return nil, nil, err
5756
}
5857

59-
if !source.IsActive {
60-
return nil, nil, oauth2.ErrAuthSourceNotActived
61-
}
58+
if hasUser {
59+
source, err := auth.GetSourceByID(user.LoginSource)
60+
if err != nil {
61+
return nil, nil, err
62+
}
6263

63-
authenticator, ok := source.Cfg.(PasswordAuthenticator)
64-
if !ok {
65-
return nil, nil, smtp.ErrUnsupportedLoginType
66-
}
64+
if !source.IsActive {
65+
return nil, nil, oauth2.ErrAuthSourceNotActived
66+
}
6767

68-
user, err := authenticator.Authenticate(user, user.LoginName, password)
69-
if err != nil {
70-
return nil, nil, err
71-
}
68+
authenticator, ok := source.Cfg.(PasswordAuthenticator)
69+
if !ok {
70+
return nil, nil, smtp.ErrUnsupportedLoginType
71+
}
7272

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

79-
return user, source, nil
78+
// WARN: DON'T check user.IsActive, that will be checked on reqSign so that
79+
// user could be hint to resend confirm email.
80+
if user.ProhibitLogin {
81+
return nil, nil, user_model.ErrUserProhibitLogin{UID: user.ID, Name: user.Name}
82+
}
83+
84+
return user, source, nil
85+
}
8086
}
8187

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

120+
if isEmail {
121+
return nil, nil, user_model.ErrEmailAddressNotExist{Email: username}
122+
}
123+
114124
return nil, nil, user_model.ErrUserNotExist{Name: username}
115125
}

0 commit comments

Comments
 (0)