From 9c91e279338f4d8bf09e1f7f5904d34cd525b102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20Moln=C3=A1r?= Date: Sun, 6 Mar 2016 17:36:30 +0100 Subject: [PATCH] Added: Ability to delete org avatar. --- cmd/web.go | 1 + models/user.go | 13 +++++++++++++ routers/org/setting.go | 8 ++++++++ routers/user/setting.go | 9 +++------ templates/org/settings/options.tmpl | 1 + 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 5092fc2b6..e839e37a5 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -368,6 +368,7 @@ func runWeb(ctx *cli.Context) { m.Combo("").Get(org.Settings). Post(bindIgnErr(auth.UpdateOrgSettingForm{}), org.SettingsPost) m.Post("/avatar", binding.MultipartForm(auth.UploadAvatarForm{}), org.SettingsAvatar) + m.Post("/avatar/delete", org.SettingsDeleteAvatar) m.Group("/hooks", func() { m.Get("", org.Webhooks) diff --git a/models/user.go b/models/user.go index bd4cace44..8b90687e3 100644 --- a/models/user.go +++ b/models/user.go @@ -346,6 +346,19 @@ func (u *User) UploadAvatar(data []byte) error { return sess.Commit() } +// DeleteAvatar deletes the user's custom avatar. +func (u *User) DeleteAvatar() error { + log.Info("Deleting user avatar: %s", u.CustomAvatarPath()) + os.Remove(u.CustomAvatarPath()) + + u.UseCustomAvatar = false + if err := UpdateUser(u); err != nil { + return fmt.Errorf("updateUser: %v", err) + } + + return nil +} + // IsAdminOfRepo returns true if user has admin or higher access of repository. func (u *User) IsAdminOfRepo(repo *Repository) bool { has, err := HasAccess(u, repo, ACCESS_MODE_ADMIN) diff --git a/routers/org/setting.go b/routers/org/setting.go index 0b1b512d3..29ce34f5d 100644 --- a/routers/org/setting.go +++ b/routers/org/setting.go @@ -96,6 +96,14 @@ func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) { ctx.Redirect(ctx.Org.OrgLink + "/settings") } +func SettingsDeleteAvatar(ctx *middleware.Context) { + if err := ctx.Org.Organization.DeleteAvatar(); err != nil { + ctx.Flash.Error(err.Error()) + } + + ctx.Redirect(ctx.Org.OrgLink + "/settings") +} + func SettingsDelete(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("org.settings") ctx.Data["PageIsSettingsDelete"] = true diff --git a/routers/user/setting.go b/routers/user/setting.go index 8d1b24dd8..c704b67ce 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -8,7 +8,6 @@ import ( "errors" "fmt" "io/ioutil" - "os" "strings" "github.com/Unknwon/com" @@ -156,12 +155,10 @@ func SettingsAvatar(ctx *middleware.Context, form auth.UploadAvatarForm) { } 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)) + if err := ctx.User.DeleteAvatar(); err != nil { + ctx.Flash.Error(err.Error()) } + ctx.Redirect(setting.AppSubUrl + "/user/settings") } diff --git a/templates/org/settings/options.tmpl b/templates/org/settings/options.tmpl index 64be63b7e..804136543 100644 --- a/templates/org/settings/options.tmpl +++ b/templates/org/settings/options.tmpl @@ -59,6 +59,7 @@
+ {{$.i18n.Tr "settings.delete_current_avatar"}}