Browse Source

Small fixes to multiple_emails feature

pull/755/head
Peter 10 years ago
parent
commit
20b5c23a19
  1. 24
      models/user.go
  2. 2
      routers/user/setting.go

24
models/user.go

@ -100,7 +100,7 @@ type User struct {
// primary email address, but is not obligatory // primary email address, but is not obligatory
type EmailAddress struct { type EmailAddress struct {
Id int64 Id int64
OwnerId int64 `xorm:"INDEX NOT NULL"` Uid int64 `xorm:"INDEX NOT NULL"`
Email string `xorm:"UNIQUE NOT NULL"` Email string `xorm:"UNIQUE NOT NULL"`
IsActivated bool IsActivated bool
IsPrimary bool `xorm:"-"` IsPrimary bool `xorm:"-"`
@ -261,8 +261,8 @@ func IsEmailUsed(email string) (bool, error) {
if len(email) == 0 { if len(email) == 0 {
return false, nil return false, nil
} }
if used, err := x.Get(&EmailAddress{Email: email}); used || err != nil { if has, err := x.Get(&EmailAddress{Email: email}); has || err != nil {
return used, err return has, err
} }
return x.Get(&User{Email: email}) return x.Get(&User{Email: email})
} }
@ -524,7 +524,7 @@ func DeleteUser(u *User) error {
return err return err
} }
// Delete all alternative email addresses // Delete all alternative email addresses
if _, err = x.Delete(&EmailAddress{OwnerId: u.Id}); err != nil { if _, err = x.Delete(&EmailAddress{Uid: u.Id}); err != nil {
return err return err
} }
// Delete all SSH keys. // Delete all SSH keys.
@ -551,7 +551,7 @@ func DeleteUser(u *User) error {
func DeleteInactivateUsers() error { func DeleteInactivateUsers() error {
_, err := x.Where("is_active=?", false).Delete(new(User)) _, err := x.Where("is_active=?", false).Delete(new(User))
if err == nil { if err == nil {
_, err = x.Delete(&EmailAddress{IsActivated: false}) _, err = x.Where("is_activated=?", false).Delete(new(EmailAddress))
} }
return err return err
} }
@ -639,11 +639,11 @@ func GetEmailAddresses(uid int64) ([]*EmailAddress, error) {
return nil, err return nil, err
} }
primary_email_found := false isPrimaryFound := false
for _, email := range emails { for _, email := range emails {
if email.Email == u.Email { if email.Email == u.Email {
primary_email_found = true isPrimaryFound = true
email.IsPrimary = true email.IsPrimary = true
} else { } else {
email.IsPrimary = false email.IsPrimary = false
@ -652,7 +652,7 @@ func GetEmailAddresses(uid int64) ([]*EmailAddress, error) {
// We alway want the primary email address displayed, even if it's not in // We alway want the primary email address displayed, even if it's not in
// the emailaddress table (yet) // the emailaddress table (yet)
if !primary_email_found { if !isPrimaryFound {
emails = append(emails, &EmailAddress{Email: u.Email, IsActivated: true, IsPrimary: true}) emails = append(emails, &EmailAddress{Email: u.Email, IsActivated: true, IsPrimary: true})
} }
return emails, nil return emails, nil
@ -676,7 +676,7 @@ func (email *EmailAddress) Activate() error {
return err return err
} }
if user, err := GetUserById(email.OwnerId); err != nil { if user, err := GetUserById(email.Uid); err != nil {
return err return err
} else { } else {
user.Rands = GetUserSalt() user.Rands = GetUserSalt()
@ -712,7 +712,7 @@ func MakeEmailPrimary(email *EmailAddress) error {
return ErrEmailNotActivated return ErrEmailNotActivated
} }
user := &User{Id: email.OwnerId} user := &User{Id: email.Uid}
has, err = x.Get(user) has, err = x.Get(user)
if err != nil { if err != nil {
return err return err
@ -726,7 +726,7 @@ func MakeEmailPrimary(email *EmailAddress) error {
if err != nil { if err != nil {
return err return err
} else if !has { } else if !has {
former_primary_email.OwnerId = user.Id former_primary_email.Uid = user.Id
former_primary_email.IsActivated = user.IsActive former_primary_email.IsActivated = user.IsActive
x.Insert(former_primary_email) x.Insert(former_primary_email)
} }
@ -799,7 +799,7 @@ func GetUserByEmail(email string) (*User, error) {
return nil, err return nil, err
} }
if has { if has {
return GetUserById(emailAddress.OwnerId) return GetUserById(emailAddress.Uid)
} }
return nil, ErrUserNotExist return nil, ErrUserNotExist

2
routers/user/setting.go

@ -197,7 +197,7 @@ func SettingsEmailPost(ctx *middleware.Context, form auth.AddEmailForm) {
cleanEmail := strings.Replace(form.Email, "\n", "", -1) cleanEmail := strings.Replace(form.Email, "\n", "", -1)
e := &models.EmailAddress{ e := &models.EmailAddress{
OwnerId: ctx.User.Id, Uid: ctx.User.Id,
Email: cleanEmail, Email: cleanEmail,
IsActivated: !setting.Service.RegisterEmailConfirm, IsActivated: !setting.Service.RegisterEmailConfirm,
} }

Loading…
Cancel
Save