1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-12-22 12:54:53 -05:00

fixed bug #151 caused Find should be Get (#153)

This commit is contained in:
Lunny Xiao 2016-11-12 07:01:09 +08:00 committed by Thibault Meyer
parent 900f233b3c
commit 555d8b16cb
4 changed files with 29 additions and 21 deletions

View file

@ -55,8 +55,9 @@ func (err ErrUserAlreadyExist) Error() string {
} }
type ErrUserNotExist struct { type ErrUserNotExist struct {
UID int64 UID int64
Name string Name string
KeyID int64
} }
func IsErrUserNotExist(err error) bool { func IsErrUserNotExist(err error) bool {
@ -65,7 +66,7 @@ func IsErrUserNotExist(err error) bool {
} }
func (err ErrUserNotExist) Error() string { func (err ErrUserNotExist) Error() string {
return fmt.Sprintf("user does not exist [uid: %d, name: %s]", err.UID, err.Name) return fmt.Sprintf("user does not exist [uid: %d, name: %s, keyid: %d]", err.UID, err.Name, err.KeyID)
} }
type ErrEmailAlreadyUsed struct { type ErrEmailAlreadyUsed struct {

View file

@ -296,7 +296,7 @@ func LoginViaLDAP(user *User, login, passowrd string, source *LoginSource, autoR
username, fn, sn, mail, isAdmin, succeed := source.Cfg.(*LDAPConfig).SearchEntry(login, passowrd, source.Type == LoginDLDAP) username, fn, sn, mail, isAdmin, succeed := source.Cfg.(*LDAPConfig).SearchEntry(login, passowrd, source.Type == LoginDLDAP)
if !succeed { if !succeed {
// User not in LDAP, do nothing // User not in LDAP, do nothing
return nil, ErrUserNotExist{0, login} return nil, ErrUserNotExist{0, login, 0}
} }
if !autoRegister { if !autoRegister {
@ -404,9 +404,9 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC
if len(cfg.AllowedDomains) > 0 { if len(cfg.AllowedDomains) > 0 {
idx := strings.Index(login, "@") idx := strings.Index(login, "@")
if idx == -1 { if idx == -1 {
return nil, ErrUserNotExist{0, login} return nil, ErrUserNotExist{0, login, 0}
} else if !com.IsSliceContainsStr(strings.Split(cfg.AllowedDomains, ","), login[idx+1:]) { } else if !com.IsSliceContainsStr(strings.Split(cfg.AllowedDomains, ","), login[idx+1:]) {
return nil, ErrUserNotExist{0, login} return nil, ErrUserNotExist{0, login, 0}
} }
} }
@ -425,7 +425,7 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC
tperr, ok := err.(*textproto.Error) tperr, ok := err.(*textproto.Error)
if (ok && tperr.Code == 535) || if (ok && tperr.Code == 535) ||
strings.Contains(err.Error(), "Username and Password not accepted") { strings.Contains(err.Error(), "Username and Password not accepted") {
return nil, ErrUserNotExist{0, login} return nil, ErrUserNotExist{0, login, 0}
} }
return nil, err return nil, err
} }
@ -465,7 +465,7 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC
func LoginViaPAM(user *User, login, password string, sourceID int64, cfg *PAMConfig, autoRegister bool) (*User, error) { func LoginViaPAM(user *User, login, password string, sourceID int64, cfg *PAMConfig, autoRegister bool) (*User, error) {
if err := pam.PAMAuth(cfg.ServiceName, login, password); err != nil { if err := pam.PAMAuth(cfg.ServiceName, login, password); err != nil {
if strings.Contains(err.Error(), "Authentication failure") { if strings.Contains(err.Error(), "Authentication failure") {
return nil, ErrUserNotExist{0, login} return nil, ErrUserNotExist{0, login, 0}
} }
return nil, err return nil, err
} }
@ -525,7 +525,7 @@ func UserSignIn(username, passowrd string) (*User, error) {
return user, nil return user, nil
} }
return nil, ErrUserNotExist{user.ID, user.Name} return nil, ErrUserNotExist{user.ID, user.Name, 0}
default: default:
var source LoginSource var source LoginSource
@ -554,5 +554,5 @@ func UserSignIn(username, passowrd string) (*User, error) {
log.Warn("Failed to login '%s' via '%s': %v", username, source.Name, err) log.Warn("Failed to login '%s' via '%s': %v", username, source.Name, err)
} }
return nil, ErrUserNotExist{user.ID, user.Name} return nil, ErrUserNotExist{user.ID, user.Name, 0}
} }

View file

@ -890,12 +890,19 @@ func UserPath(userName string) string {
return filepath.Join(setting.RepoRootPath, strings.ToLower(userName)) return filepath.Join(setting.RepoRootPath, strings.ToLower(userName))
} }
// GetUserByKeyID get user information by user's public key id
func GetUserByKeyID(keyID int64) (*User, error) { func GetUserByKeyID(keyID int64) (*User, error) {
user := new(User) var user User
return user, x. has, err := x.Join("INNER", "public_key", "`public_key`.owner_id = `user`.id").
Join("INNER", "public_key", "`public_key`.owner_id = `user`.id").
Where("`public_key`.id=?", keyID). Where("`public_key`.id=?", keyID).
Find(user) Get(user)
if err != nil {
return nil, err
}
if !has {
return nil, ErrUserNotExist{0, "", keyID}
}
return &user, nil
} }
func getUserByID(e Engine, id int64) (*User, error) { func getUserByID(e Engine, id int64) (*User, error) {
@ -904,7 +911,7 @@ func getUserByID(e Engine, id int64) (*User, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, ErrUserNotExist{id, ""} return nil, ErrUserNotExist{id, "", 0}
} }
return u, nil return u, nil
} }
@ -920,7 +927,7 @@ func GetAssigneeByID(repo *Repository, userID int64) (*User, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, ErrUserNotExist{userID, ""} return nil, ErrUserNotExist{userID, "", 0}
} }
return GetUserByID(userID) return GetUserByID(userID)
} }
@ -928,14 +935,14 @@ func GetAssigneeByID(repo *Repository, userID int64) (*User, error) {
// GetUserByName returns user by given name. // GetUserByName returns user by given name.
func GetUserByName(name string) (*User, error) { func GetUserByName(name string) (*User, error) {
if len(name) == 0 { if len(name) == 0 {
return nil, ErrUserNotExist{0, name} return nil, ErrUserNotExist{0, name, 0}
} }
u := &User{LowerName: strings.ToLower(name)} u := &User{LowerName: strings.ToLower(name)}
has, err := x.Get(u) has, err := x.Get(u)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, ErrUserNotExist{0, name} return nil, ErrUserNotExist{0, name, 0}
} }
return u, nil return u, nil
} }
@ -1021,7 +1028,7 @@ func ValidateCommitsWithEmails(oldCommits *list.List) *list.List {
// GetUserByEmail returns the user object by given e-mail if exists. // GetUserByEmail returns the user object by given e-mail if exists.
func GetUserByEmail(email string) (*User, error) { func GetUserByEmail(email string) (*User, error) {
if len(email) == 0 { if len(email) == 0 {
return nil, ErrUserNotExist{0, "email"} return nil, ErrUserNotExist{0, email, 0}
} }
email = strings.ToLower(email) email = strings.ToLower(email)
@ -1045,7 +1052,7 @@ func GetUserByEmail(email string) (*User, error) {
return GetUserByID(emailAddress.UID) return GetUserByID(emailAddress.UID)
} }
return nil, ErrUserNotExist{0, email} return nil, ErrUserNotExist{0, email, 0}
} }
type SearchUserOptions struct { type SearchUserOptions struct {

View file

@ -172,7 +172,7 @@ func MakeEmailPrimary(email *EmailAddress) error {
if err != nil { if err != nil {
return err return err
} else if !has { } else if !has {
return ErrUserNotExist{email.UID, ""} return ErrUserNotExist{email.UID, "", 0}
} }
// Make sure the former primary email doesn't disappear. // Make sure the former primary email doesn't disappear.