diff options
author | Unknwon <u@gogs.io> | 2015-12-15 22:57:18 -0500 |
---|---|---|
committer | Unknwon <u@gogs.io> | 2015-12-15 22:57:18 -0500 |
commit | b117befc2b4f5ec1e864e2eeaf99355861be01d7 (patch) | |
tree | a6f3b95e28e606f89d672e5079070b1cd9119c69 /models/user.go | |
parent | 7786cb76f3bfc87f92aaae5cc70cbfa974b76868 (diff) | |
download | gitea-b117befc2b4f5ec1e864e2eeaf99355861be01d7.tar.gz gitea-b117befc2b4f5ec1e864e2eeaf99355861be01d7.zip |
#1692 add user email APIs
Diffstat (limited to 'models/user.go')
-rw-r--r-- | models/user.go | 46 |
1 files changed, 36 insertions, 10 deletions
diff --git a/models/user.go b/models/user.go index 36927730d5..71729a4e3e 100644 --- a/models/user.go +++ b/models/user.go @@ -888,7 +888,7 @@ func GetEmailAddresses(uid int64) ([]*EmailAddress, error) { } func AddEmailAddress(email *EmailAddress) error { - email.Email = strings.ToLower(email.Email) + email.Email = strings.ToLower(strings.TrimSpace(email.Email)) used, err := IsEmailUsed(email.Email) if err != nil { return err @@ -900,6 +900,29 @@ func AddEmailAddress(email *EmailAddress) error { return err } +func AddEmailAddresses(emails []*EmailAddress) error { + if len(emails) == 0 { + return nil + } + + // Check if any of them has been used + for i := range emails { + emails[i].Email = strings.ToLower(strings.TrimSpace(emails[i].Email)) + used, err := IsEmailUsed(emails[i].Email) + if err != nil { + return err + } else if used { + return ErrEmailAlreadyUsed{emails[i].Email} + } + } + + if _, err := x.Insert(emails); err != nil { + return fmt.Errorf("Insert: %v", err) + } + + return nil +} + func (email *EmailAddress) Activate() error { email.IsActivated = true if _, err := x.Id(email.ID).AllCols().Update(email); err != nil { @@ -914,20 +937,23 @@ func (email *EmailAddress) Activate() error { } } -func DeleteEmailAddress(email *EmailAddress) error { - has, err := x.Get(email) - if err != nil { - return err - } else if !has { - return ErrEmailNotExist +func DeleteEmailAddress(email *EmailAddress) (err error) { + if email.ID > 0 { + _, err = x.Id(email.ID).Delete(new(EmailAddress)) + } else { + _, err = x.Where("email=?", email.Email).Delete(new(EmailAddress)) } + return err +} - if _, err = x.Id(email.ID).Delete(email); err != nil { - return err +func DeleteEmailAddresses(emails []*EmailAddress) (err error) { + for i := range emails { + if err = DeleteEmailAddress(emails[i]); err != nil { + return err + } } return nil - } func MakeEmailPrimary(email *EmailAddress) error { |