|
|
|
@ -36,7 +36,6 @@ const ( |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
ErrUserNotExist = errors.New("User does not exist") |
|
|
|
|
ErrUserNotKeyOwner = errors.New("User does not the owner of public key") |
|
|
|
|
ErrEmailNotExist = errors.New("E-mail does not exist") |
|
|
|
|
ErrEmailNotActivated = errors.New("E-mail address has not been activated") |
|
|
|
@ -259,6 +258,8 @@ func IsEmailUsed(email string) (bool, error) { |
|
|
|
|
if len(email) == 0 { |
|
|
|
|
return false, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
email = strings.ToLower(email) |
|
|
|
|
if has, err := x.Get(&EmailAddress{Email: email}); has || err != nil { |
|
|
|
|
return has, err |
|
|
|
|
} |
|
|
|
@ -406,6 +407,7 @@ func ChangeUserName(u *User, newUserName string) (err error) { |
|
|
|
|
|
|
|
|
|
// UpdateUser updates user's information.
|
|
|
|
|
func UpdateUser(u *User) error { |
|
|
|
|
u.Email = strings.ToLower(u.Email) |
|
|
|
|
has, err := x.Where("id!=?", u.Id).And("type=?", u.Type).And("email=?", u.Email).Get(new(User)) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
@ -555,7 +557,7 @@ func getUserById(e Engine, id int64) (*User, error) { |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} else if !has { |
|
|
|
|
return nil, ErrUserNotExist |
|
|
|
|
return nil, ErrUserNotExist{id, ""} |
|
|
|
|
} |
|
|
|
|
return u, nil |
|
|
|
|
} |
|
|
|
@ -568,14 +570,14 @@ func GetUserById(id int64) (*User, error) { |
|
|
|
|
// GetUserByName returns user by given name.
|
|
|
|
|
func GetUserByName(name string) (*User, error) { |
|
|
|
|
if len(name) == 0 { |
|
|
|
|
return nil, ErrUserNotExist |
|
|
|
|
return nil, ErrUserNotExist{0, name} |
|
|
|
|
} |
|
|
|
|
u := &User{LowerName: strings.ToLower(name)} |
|
|
|
|
has, err := x.Get(u) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
|
} else if !has { |
|
|
|
|
return nil, ErrUserNotExist |
|
|
|
|
return nil, ErrUserNotExist{0, name} |
|
|
|
|
} |
|
|
|
|
return u, nil |
|
|
|
|
} |
|
|
|
@ -642,6 +644,7 @@ func GetEmailAddresses(uid int64) ([]*EmailAddress, error) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func AddEmailAddress(email *EmailAddress) error { |
|
|
|
|
email.Email = strings.ToLower(email.Email) |
|
|
|
|
used, err := IsEmailUsed(email.Email) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
@ -675,7 +678,7 @@ func DeleteEmailAddress(email *EmailAddress) error { |
|
|
|
|
return ErrEmailNotExist |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if _, err = x.Delete(email); err != nil { |
|
|
|
|
if _, err = x.Id(email.Id).Delete(email); err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -700,7 +703,7 @@ func MakeEmailPrimary(email *EmailAddress) error { |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} else if !has { |
|
|
|
|
return ErrUserNotExist |
|
|
|
|
return ErrUserNotExist{email.Uid, ""} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Make sure the former primary email doesn't disappear
|
|
|
|
@ -737,13 +740,15 @@ func ValidateCommitWithEmail(c *git.Commit) *User { |
|
|
|
|
|
|
|
|
|
// ValidateCommitsWithEmails checks if authors' e-mails of commits are corresponding to users.
|
|
|
|
|
func ValidateCommitsWithEmails(oldCommits *list.List) *list.List { |
|
|
|
|
emails := map[string]*User{} |
|
|
|
|
newCommits := list.New() |
|
|
|
|
e := oldCommits.Front() |
|
|
|
|
var ( |
|
|
|
|
u *User |
|
|
|
|
emails = map[string]*User{} |
|
|
|
|
newCommits = list.New() |
|
|
|
|
e = oldCommits.Front() |
|
|
|
|
) |
|
|
|
|
for e != nil { |
|
|
|
|
c := e.Value.(*git.Commit) |
|
|
|
|
|
|
|
|
|
var u *User |
|
|
|
|
if v, ok := emails[c.Author.Email]; !ok { |
|
|
|
|
u, _ = GetUserByEmail(c.Author.Email) |
|
|
|
|
emails[c.Author.Email] = u |
|
|
|
@ -763,10 +768,12 @@ func ValidateCommitsWithEmails(oldCommits *list.List) *list.List { |
|
|
|
|
// GetUserByEmail returns the user object by given e-mail if exists.
|
|
|
|
|
func GetUserByEmail(email string) (*User, error) { |
|
|
|
|
if len(email) == 0 { |
|
|
|
|
return nil, ErrUserNotExist |
|
|
|
|
return nil, ErrUserNotExist{0, "email"} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
email = strings.ToLower(email) |
|
|
|
|
// First try to find the user by primary email
|
|
|
|
|
user := &User{Email: strings.ToLower(email)} |
|
|
|
|
user := &User{Email: email} |
|
|
|
|
has, err := x.Get(user) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
@ -776,7 +783,7 @@ func GetUserByEmail(email string) (*User, error) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Otherwise, check in alternative list for activated email addresses
|
|
|
|
|
emailAddress := &EmailAddress{Email: strings.ToLower(email), IsActivated: true} |
|
|
|
|
emailAddress := &EmailAddress{Email: email, IsActivated: true} |
|
|
|
|
has, err = x.Get(emailAddress) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
@ -785,7 +792,7 @@ func GetUserByEmail(email string) (*User, error) { |
|
|
|
|
return GetUserById(emailAddress.Uid) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return nil, ErrUserNotExist |
|
|
|
|
return nil, ErrUserNotExist{0, "email"} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// SearchUserByName returns given number of users whose name contains keyword.
|
|
|
|
|