From a666829a37be6f9fd98f9e7dd1767c420f7f3b32 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 24 Nov 2021 17:49:20 +0800 Subject: Move user related model into models/user (#17781) * Move user related model into models/user * Fix lint for windows * Fix windows lint * Fix windows lint * Move some tests in models * Merge --- services/auth/signin.go | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) (limited to 'services/auth/signin.go') diff --git a/services/auth/signin.go b/services/auth/signin.go index 504214f9f1..5f75000d98 100644 --- a/services/auth/signin.go +++ b/services/auth/signin.go @@ -7,25 +7,24 @@ package auth import ( "strings" - "code.gitea.io/gitea/models" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/login" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" - _ "code.gitea.io/gitea/services/auth/source/db" // register the sources (and below) - _ "code.gitea.io/gitea/services/auth/source/ldap" - _ "code.gitea.io/gitea/services/auth/source/oauth2" - _ "code.gitea.io/gitea/services/auth/source/pam" - _ "code.gitea.io/gitea/services/auth/source/smtp" - _ "code.gitea.io/gitea/services/auth/source/sspi" + _ "code.gitea.io/gitea/services/auth/source/db" // register the sources (and below) + _ "code.gitea.io/gitea/services/auth/source/ldap" // register the ldap source + "code.gitea.io/gitea/services/auth/source/oauth2" + _ "code.gitea.io/gitea/services/auth/source/pam" // register the pam source + "code.gitea.io/gitea/services/auth/source/smtp" + _ "code.gitea.io/gitea/services/auth/source/sspi" // register the sspi source ) // UserSignIn validates user name and password. -func UserSignIn(username, password string) (*models.User, *login.Source, error) { - var user *models.User +func UserSignIn(username, password string) (*user_model.User, *login.Source, error) { + var user *user_model.User if strings.Contains(username, "@") { - user = &models.User{Email: strings.ToLower(strings.TrimSpace(username))} + user = &user_model.User{Email: strings.ToLower(strings.TrimSpace(username))} // check same email cnt, err := db.Count(user) if err != nil { @@ -39,13 +38,13 @@ func UserSignIn(username, password string) (*models.User, *login.Source, error) } else { trimmedUsername := strings.TrimSpace(username) if len(trimmedUsername) == 0 { - return nil, nil, models.ErrUserNotExist{Name: username} + return nil, nil, user_model.ErrUserNotExist{Name: username} } - user = &models.User{LowerName: strings.ToLower(trimmedUsername)} + user = &user_model.User{LowerName: strings.ToLower(trimmedUsername)} } - hasUser, err := models.GetUser(user) + hasUser, err := user_model.GetUser(user) if err != nil { return nil, nil, err } @@ -57,12 +56,12 @@ func UserSignIn(username, password string) (*models.User, *login.Source, error) } if !source.IsActive { - return nil, nil, models.ErrLoginSourceNotActived + return nil, nil, oauth2.ErrLoginSourceNotActived } authenticator, ok := source.Cfg.(PasswordAuthenticator) if !ok { - return nil, nil, models.ErrUnsupportedLoginType + return nil, nil, smtp.ErrUnsupportedLoginType } user, err := authenticator.Authenticate(user, username, password) @@ -73,7 +72,7 @@ func UserSignIn(username, password string) (*models.User, *login.Source, error) // 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, models.ErrUserProhibitLogin{UID: user.ID, Name: user.Name} + return nil, nil, user_model.ErrUserProhibitLogin{UID: user.ID, Name: user.Name} } return user, source, nil @@ -101,15 +100,15 @@ func UserSignIn(username, password string) (*models.User, *login.Source, error) if !authUser.ProhibitLogin { return authUser, source, nil } - err = models.ErrUserProhibitLogin{UID: authUser.ID, Name: authUser.Name} + err = user_model.ErrUserProhibitLogin{UID: authUser.ID, Name: authUser.Name} } - if models.IsErrUserNotExist(err) { + if user_model.IsErrUserNotExist(err) { log.Debug("Failed to login '%s' via '%s': %v", username, source.Name, err) } else { log.Warn("Failed to login '%s' via '%s': %v", username, source.Name, err) } } - return nil, nil, models.ErrUserNotExist{Name: username} + return nil, nil, user_model.ErrUserNotExist{Name: username} } -- cgit v1.2.3