From 698b9e2acc4daafe7d2b314e2d8c96545dde9c40 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Thu, 26 Mar 2015 17:11:47 -0400 Subject: #1070 Clearer error message for illegal characters --- models/user.go | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'models/user.go') diff --git a/models/user.go b/models/user.go index dcfd0dc5..bf69f97a 100644 --- a/models/user.go +++ b/models/user.go @@ -36,10 +36,8 @@ const ( ) var ( - ErrUserAlreadyExist = errors.New("User already exist") ErrUserNotExist = errors.New("User does not exist") ErrUserNotKeyOwner = errors.New("User does not the owner of public key") - ErrEmailAlreadyUsed = errors.New("E-mail already used") ErrEmailNotExist = errors.New("E-mail does not exist") ErrEmailNotActivated = errors.New("E-mail address has not been activated") ErrUserNameIllegal = errors.New("User name contains illegal characters") @@ -273,23 +271,23 @@ func GetUserSalt() string { } // CreateUser creates record of a new user. -func CreateUser(u *User) error { - if !IsLegalName(u.Name) { - return ErrUserNameIllegal +func CreateUser(u *User) (err error) { + if err = IsUsableName(u.Name); err != nil { + return err } isExist, err := IsUserExist(0, u.Name) if err != nil { return err } else if isExist { - return ErrUserAlreadyExist + return ErrUserAlreadyExist{u.Name} } isExist, err = IsEmailUsed(u.Email) if err != nil { return err } else if isExist { - return ErrEmailAlreadyUsed + return ErrEmailAlreadyUsed{u.Email} } u.LowerName = strings.ToLower(u.Name) @@ -392,8 +390,15 @@ func VerifyActiveEmailCode(code, email string) *EmailAddress { // ChangeUserName changes all corresponding setting from old user name to new one. func ChangeUserName(u *User, newUserName string) (err error) { - if !IsLegalName(newUserName) { - return ErrUserNameIllegal + if err = IsUsableName(newUserName); err != nil { + return err + } + + isExist, err := IsUserExist(0, newUserName) + if err != nil { + return err + } else if isExist { + return ErrUserAlreadyExist{newUserName} } return os.Rename(UserPath(u.LowerName), UserPath(newUserName)) @@ -405,7 +410,7 @@ func UpdateUser(u *User) error { if err != nil { return err } else if has { - return ErrEmailAlreadyUsed + return ErrEmailAlreadyUsed{u.Email} } u.LowerName = strings.ToLower(u.Name) @@ -641,7 +646,7 @@ func AddEmailAddress(email *EmailAddress) error { if err != nil { return err } else if used { - return ErrEmailAlreadyUsed + return ErrEmailAlreadyUsed{email.Email} } _, err = x.Insert(email) -- cgit v1.2.3 From f78046fc3be8db80f8ac44512237c92825540e5d Mon Sep 17 00:00:00 2001 From: Dustin Willis Webber Date: Thu, 16 Apr 2015 14:36:32 -0400 Subject: typo fix --- models/login.go | 2 +- models/user.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'models/user.go') diff --git a/models/login.go b/models/login.go index 916e2731..73d11256 100644 --- a/models/login.go +++ b/models/login.go @@ -169,7 +169,7 @@ func UserSignIn(uname, passwd string) (*User, error) { // For plain login, user must exist to reach this line. // Now verify password. if u.LoginType == PLAIN { - if !u.ValidtePassword(passwd) { + if !u.ValidatePassword(passwd) { return nil, ErrUserNotExist } return u, nil diff --git a/models/user.go b/models/user.go index dcfd0dc5..8651464e 100644 --- a/models/user.go +++ b/models/user.go @@ -146,7 +146,7 @@ func (u *User) EncodePasswd() { } // ValidtePassword checks if given password matches the one belongs to the user. -func (u *User) ValidtePassword(passwd string) bool { +func (u *User) ValidatePassword(passwd string) bool { newUser := &User{Passwd: passwd, Salt: u.Salt} newUser.EncodePasswd() return u.Passwd == newUser.Passwd -- cgit v1.2.3 From e57594dc31fc42c1bb7ba0df77d1d4f249f8f079 Mon Sep 17 00:00:00 2001 From: Dustin Willis Webber Date: Thu, 16 Apr 2015 14:40:39 -0400 Subject: typo fix for comment --- models/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'models/user.go') diff --git a/models/user.go b/models/user.go index 8651464e..e0ba4be3 100644 --- a/models/user.go +++ b/models/user.go @@ -145,7 +145,7 @@ func (u *User) EncodePasswd() { u.Passwd = fmt.Sprintf("%x", newPasswd) } -// ValidtePassword checks if given password matches the one belongs to the user. +// ValidatePassword checks if given password matches the one belongs to the user. func (u *User) ValidatePassword(passwd string) bool { newUser := &User{Passwd: passwd, Salt: u.Salt} newUser.EncodePasswd() -- cgit v1.2.3