|
|
@ -796,18 +796,48 @@ func validateUser(u *User) error { |
|
|
|
return ValidateEmail(u.Email) |
|
|
|
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 { |
|
|
|
if err := validateUser(u); err != nil { |
|
|
|
return err |
|
|
|
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) |
|
|
|
_, err := e.ID(u.ID).AllCols().Update(u) |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// UpdateUser updates user's information.
|
|
|
|
// UpdateUser updates user's information.
|
|
|
|
func UpdateUser(u *User) error { |
|
|
|
func UpdateUser(u *User, emailChanged bool) error { |
|
|
|
return updateUser(db.GetEngine(db.DefaultContext), u) |
|
|
|
return updateUser(db.DefaultContext, u, emailChanged) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// UpdateUserCols update user according special columns
|
|
|
|
// UpdateUserCols update user according special columns
|
|
|
@ -836,14 +866,13 @@ func UpdateUserSetting(u *User) (err error) { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
defer committer.Close() |
|
|
|
defer committer.Close() |
|
|
|
sess := db.GetEngine(ctx) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !u.IsOrganization() { |
|
|
|
if !u.IsOrganization() { |
|
|
|
if err = checkDupEmail(sess, u); err != nil { |
|
|
|
if err = checkDupEmail(db.GetEngine(ctx), u); err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if err = updateUser(sess, u); err != nil { |
|
|
|
if err = updateUser(ctx, u, false); err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
return committer.Commit() |
|
|
|
return committer.Commit() |
|
|
|