diff options
Diffstat (limited to 'routers/user/setting.go')
-rw-r--r-- | routers/user/setting.go | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/routers/user/setting.go b/routers/user/setting.go index 56a9cab0a2..8d1b24dd8c 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "io/ioutil" + "os" "strings" "github.com/Unknwon/com" @@ -39,12 +40,13 @@ func Settings(ctx *middleware.Context) { ctx.HTML(200, SETTINGS_PROFILE) } -func handlerUsernameChange(ctx *middleware.Context, newName string) { +func handleUsernameChange(ctx *middleware.Context, newName string) { + // Non-local users are not allowed to change their username. if len(newName) == 0 || !ctx.User.IsLocal() { return } - // Check if user name has been changed. + // Check if user name has been changed if ctx.User.LowerName != strings.ToLower(newName) { if err := models.ChangeUserName(ctx.User, newName); err != nil { switch { @@ -67,7 +69,8 @@ func handlerUsernameChange(ctx *middleware.Context, newName string) { } log.Trace("User name changed: %s -> %s", ctx.User.Name, newName) } - // In case it's just a case change. + + // In case it's just a case change ctx.User.Name = newName ctx.User.LowerName = strings.ToLower(newName) } @@ -81,7 +84,7 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) { return } - handlerUsernameChange(ctx, form.Name) + handleUsernameChange(ctx, form.Name) if ctx.Written() { return } @@ -98,7 +101,8 @@ func SettingsPost(ctx *middleware.Context, form auth.UpdateProfileForm) { ctx.Handle(500, "UpdateUser", err) return } - log.Trace("User setting updated: %s", ctx.User.Name) + + log.Trace("User settings updated: %s", ctx.User.Name) ctx.Flash.Success(ctx.Tr("settings.update_profile_success")) ctx.Redirect(setting.AppSubUrl + "/user/settings") } @@ -112,10 +116,11 @@ func UpdateAvatarSetting(ctx *middleware.Context, form auth.UploadAvatarForm, ct if err != nil { return fmt.Errorf("Avatar.Open: %v", err) } + defer fr.Close() data, err := ioutil.ReadAll(fr) if err != nil { - return fmt.Errorf("ReadAll: %v", err) + return fmt.Errorf("ioutil.ReadAll: %v", err) } if _, ok := base.IsImageFile(data); !ok { return errors.New(ctx.Tr("settings.uploaded_avatar_not_a_image")) @@ -124,9 +129,12 @@ func UpdateAvatarSetting(ctx *middleware.Context, form auth.UploadAvatarForm, ct return fmt.Errorf("UploadAvatar: %v", err) } } else { - // In case no avatar at all. - if form.Enable && !com.IsFile(ctx.User.CustomAvatarPath()) { - return errors.New(ctx.Tr("settings.no_custom_avatar_available")) + // No avatar is uploaded but setting has been changed to enable, + // generate a random one when needed. + if form.Enable && !com.IsFile(ctxUser.CustomAvatarPath()) { + if err := ctxUser.GenerateRandomAvatar(); err != nil { + log.Error(4, "GenerateRandomAvatar[%d]: %v", ctxUser.Id, err) + } } } @@ -147,6 +155,16 @@ func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) { ctx.Redirect(setting.AppSubUrl + "/user/settings") } +func SettingsDeleteAvatar(ctx *middleware.Context) { + os.Remove(ctx.User.CustomAvatarPath()) + + ctx.User.UseCustomAvatar = false + if err := models.UpdateUser(ctx.User); err != nil { + ctx.Flash.Error(fmt.Sprintf("UpdateUser: %v", err)) + } + ctx.Redirect(setting.AppSubUrl + "/user/settings") +} + func SettingsPassword(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("settings") ctx.Data["PageIsSettingsPassword"] = true |