* only update needed columns when update user * fix missing update_unix columntags/v1.2.0-rc1
return fmt.Errorf("%v", err) | return fmt.Errorf("%v", err) | ||||
} | } | ||||
user.EncodePasswd() | user.EncodePasswd() | ||||
if err := models.UpdateUser(user); err != nil { | |||||
if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil { | |||||
return fmt.Errorf("%v", err) | return fmt.Errorf("%v", err) | ||||
} | } | ||||
// UpdateDiffViewStyle updates the users diff view style | // UpdateDiffViewStyle updates the users diff view style | ||||
func (u *User) UpdateDiffViewStyle(style string) error { | func (u *User) UpdateDiffViewStyle(style string) error { | ||||
u.DiffViewStyle = style | u.DiffViewStyle = style | ||||
return UpdateUser(u) | |||||
return UpdateUserCols(u, "diff_view_style") | |||||
} | } | ||||
// AfterSet is invoked from XORM after setting the value of a field of this object. | // AfterSet is invoked from XORM after setting the value of a field of this object. | ||||
if len(u.AvatarEmail) == 0 { | if len(u.AvatarEmail) == 0 { | ||||
u.AvatarEmail = u.Email | u.AvatarEmail = u.Email | ||||
} | } | ||||
u.Avatar = base.HashEmail(u.AvatarEmail) | |||||
if len(u.AvatarEmail) > 0 { | |||||
u.Avatar = base.HashEmail(u.AvatarEmail) | |||||
} | |||||
} | } | ||||
u.LowerName = strings.ToLower(u.Name) | u.LowerName = strings.ToLower(u.Name) | ||||
return updateUser(x, u) | return updateUser(x, u) | ||||
} | } | ||||
// UpdateUserCols update user according special columns | |||||
func UpdateUserCols(u *User, cols ...string) error { | |||||
// Organization does not need email | |||||
u.Email = strings.ToLower(u.Email) | |||||
if !u.IsOrganization() { | |||||
if len(u.AvatarEmail) == 0 { | |||||
u.AvatarEmail = u.Email | |||||
} | |||||
if len(u.AvatarEmail) > 0 { | |||||
u.Avatar = base.HashEmail(u.AvatarEmail) | |||||
} | |||||
} | |||||
u.LowerName = strings.ToLower(u.Name) | |||||
u.Location = base.TruncateString(u.Location, 255) | |||||
u.Website = base.TruncateString(u.Website, 255) | |||||
u.Description = base.TruncateString(u.Description, 255) | |||||
cols = append(cols, "updated_unix") | |||||
_, err := x.Id(u.ID).Cols(cols...).Update(u) | |||||
return err | |||||
} | |||||
// UpdateUserSetting updates user's settings. | // UpdateUserSetting updates user's settings. | ||||
func UpdateUserSetting(u *User) error { | func UpdateUserSetting(u *User) error { | ||||
if !u.IsOrganization() { | if !u.IsOrganization() { | ||||
} | } | ||||
usr.IsActive = true | usr.IsActive = true | ||||
err = UpdateUser(usr) | |||||
err = UpdateUserCols(usr, "full_name", "email", "is_admin", "is_active") | |||||
if err != nil { | if err != nil { | ||||
log.Error(4, "SyncExternalUsers[%s]: Error updating user %s: %v", s.Name, usr.Name, err) | log.Error(4, "SyncExternalUsers[%s]: Error updating user %s: %v", s.Name, usr.Name, err) | ||||
} | } | ||||
log.Trace("SyncExternalUsers[%s]: Deactivating user %s", s.Name, usr.Name) | log.Trace("SyncExternalUsers[%s]: Deactivating user %s", s.Name, usr.Name) | ||||
usr.IsActive = false | usr.IsActive = false | ||||
err = UpdateUser(&usr) | |||||
err = UpdateUserCols(&usr, "is_active") | |||||
if err != nil { | if err != nil { | ||||
log.Error(4, "SyncExternalUsers[%s]: Error deactivating user %s: %v", s.Name, usr.Name, err) | log.Error(4, "SyncExternalUsers[%s]: Error deactivating user %s: %v", s.Name, usr.Name, err) | ||||
} | } |
org.Description = form.Description | org.Description = form.Description | ||||
org.Website = form.Website | org.Website = form.Website | ||||
org.Location = form.Location | org.Location = form.Location | ||||
if err := models.UpdateUser(org); err != nil { | |||||
if err := models.UpdateUserCols(org, "full_name", "description", "website", "location"); err != nil { | |||||
ctx.Error(500, "UpdateUser", err) | ctx.Error(500, "UpdateUser", err) | ||||
return | return | ||||
} | } |
// Register last login | // Register last login | ||||
u.SetLastLogin() | u.SetLastLogin() | ||||
if err := models.UpdateUser(u); err != nil { | |||||
ctx.Handle(500, "UpdateUser", err) | |||||
if err := models.UpdateUserCols(u, "last_login_unix"); err != nil { | |||||
ctx.Handle(500, "UpdateUserCols", err) | |||||
return | return | ||||
} | } | ||||
// Register last login | // Register last login | ||||
u.SetLastLogin() | u.SetLastLogin() | ||||
if err := models.UpdateUser(u); err != nil { | |||||
ctx.Handle(500, "UpdateUser", err) | |||||
if err := models.UpdateUserCols(u, "last_login_unix"); err != nil { | |||||
ctx.Handle(500, "UpdateUserCols", err) | |||||
return | return | ||||
} | } | ||||
if models.CountUsers() == 1 { | if models.CountUsers() == 1 { | ||||
u.IsAdmin = true | u.IsAdmin = true | ||||
u.IsActive = true | u.IsActive = true | ||||
if err := models.UpdateUser(u); err != nil { | |||||
u.SetLastLogin() | |||||
if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil { | |||||
ctx.Handle(500, "UpdateUser", err) | ctx.Handle(500, "UpdateUser", err) | ||||
return | return | ||||
} | } | ||||
if models.CountUsers() == 1 { | if models.CountUsers() == 1 { | ||||
u.IsAdmin = true | u.IsAdmin = true | ||||
u.IsActive = true | u.IsActive = true | ||||
if err := models.UpdateUser(u); err != nil { | |||||
u.SetLastLogin() | |||||
if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil { | |||||
ctx.Handle(500, "UpdateUser", err) | ctx.Handle(500, "UpdateUser", err) | ||||
return | return | ||||
} | } | ||||
ctx.Handle(500, "UpdateUser", err) | ctx.Handle(500, "UpdateUser", err) | ||||
return | return | ||||
} | } | ||||
if err := models.UpdateUser(user); err != nil { | |||||
if err := models.UpdateUserCols(user, "is_active", "rands"); err != nil { | |||||
if models.IsErrUserNotExist(err) { | if models.IsErrUserNotExist(err) { | ||||
ctx.Error(404) | ctx.Error(404) | ||||
} else { | } else { | ||||
return | return | ||||
} | } | ||||
u.EncodePasswd() | u.EncodePasswd() | ||||
if err := models.UpdateUser(u); err != nil { | |||||
if err := models.UpdateUserCols(u, "passwd", "rands", "salt"); err != nil { | |||||
ctx.Handle(500, "UpdateUser", err) | ctx.Handle(500, "UpdateUser", err) | ||||
return | return | ||||
} | } |
if models.CountUsers() == 1 { | if models.CountUsers() == 1 { | ||||
u.IsAdmin = true | u.IsAdmin = true | ||||
u.IsActive = true | u.IsActive = true | ||||
if err := models.UpdateUser(u); err != nil { | |||||
u.SetLastLogin() | |||||
if err := models.UpdateUserCols(u, "is_admin", "is_active", "last_login_unix"); err != nil { | |||||
ctx.Handle(500, "UpdateUser", err) | ctx.Handle(500, "UpdateUser", err) | ||||
return | return | ||||
} | } |
} | } | ||||
} | } | ||||
if err := models.UpdateUser(ctxUser); err != nil { | |||||
if err := models.UpdateUserCols(ctxUser, "avatar", "avatar_email", "use_custom_avatar"); err != nil { | |||||
return fmt.Errorf("UpdateUser: %v", err) | return fmt.Errorf("UpdateUser: %v", err) | ||||
} | } | ||||
return | return | ||||
} | } | ||||
ctx.User.EncodePasswd() | ctx.User.EncodePasswd() | ||||
if err := models.UpdateUser(ctx.User); err != nil { | |||||
if err := models.UpdateUserCols(ctx.User, "salt", "passwd"); err != nil { | |||||
ctx.Handle(500, "UpdateUser", err) | ctx.Handle(500, "UpdateUser", err) | ||||
return | return | ||||
} | } |