diff options
Diffstat (limited to 'models/user/user.go')
-rw-r--r-- | models/user/user.go | 41 |
1 files changed, 35 insertions, 6 deletions
diff --git a/models/user/user.go b/models/user/user.go index f8ccee0b38..62f2aa472d 100644 --- a/models/user/user.go +++ b/models/user/user.go @@ -796,18 +796,48 @@ func validateUser(u *User) error { return ValidateEmail(u.Email) } -func updateUser(e db.Engine, u *User) error { +func updateUser(ctx context.Context, u *User, changePrimaryEmail bool) error { if err := validateUser(u); err != nil { return err } + e := db.GetEngine(ctx) + + if changePrimaryEmail { + var emailAddress EmailAddress + has, err := e.Where("lower_email=?", strings.ToLower(u.Email)).Get(&emailAddress) + if err != nil { + return err + } + if !has { + // 1. Update old primary email + if _, err = e.Where("uid=? AND is_primary=?", u.ID, true).Cols("is_primary").Update(&EmailAddress{ + IsPrimary: false, + }); err != nil { + return err + } + + emailAddress.Email = u.Email + emailAddress.UID = u.ID + emailAddress.IsActivated = true + emailAddress.IsPrimary = true + if _, err := e.Insert(&emailAddress); err != nil { + return err + } + } else if _, err := e.ID(emailAddress).Cols("is_primary").Update(&EmailAddress{ + IsPrimary: true, + }); err != nil { + return err + } + } + _, err := e.ID(u.ID).AllCols().Update(u) return err } // UpdateUser updates user's information. -func UpdateUser(u *User) error { - return updateUser(db.GetEngine(db.DefaultContext), u) +func UpdateUser(u *User, emailChanged bool) error { + return updateUser(db.DefaultContext, u, emailChanged) } // UpdateUserCols update user according special columns @@ -836,14 +866,13 @@ func UpdateUserSetting(u *User) (err error) { return err } defer committer.Close() - sess := db.GetEngine(ctx) if !u.IsOrganization() { - if err = checkDupEmail(sess, u); err != nil { + if err = checkDupEmail(db.GetEngine(ctx), u); err != nil { return err } } - if err = updateUser(sess, u); err != nil { + if err = updateUser(ctx, u, false); err != nil { return err } return committer.Commit() |