From 1810316243c8079207493a6880372017191facb2 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 15 Nov 2021 19:57:10 +0100 Subject: [PATCH 1/6] Allow admins to change user avatars --- routers/web/admin/users.go | 32 ++++++++++++++++++++++++++++ routers/web/web.go | 2 ++ templates/admin/user/edit.tmpl | 38 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/routers/web/admin/users.go b/routers/web/admin/users.go index db7fe7b36f6ee..576a48b10753a 100644 --- a/routers/web/admin/users.go +++ b/routers/web/admin/users.go @@ -23,6 +23,7 @@ import ( "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/web/explore" + user_setting "code.gitea.io/gitea/routers/web/user/setting" router_user_setting "code.gitea.io/gitea/routers/web/user/setting" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/mailer" @@ -401,3 +402,34 @@ func DeleteUser(ctx *context.Context) { "redirect": setting.AppSubURL + "/admin/users", }) } + +// AvatarPost response for change user's avatar request +func AvatarPost(ctx *context.Context) { + u := prepareUserInfo(ctx) + if ctx.Written() { + return + } + + form := web.GetForm(ctx).(*forms.AvatarForm) + if err := user_setting.UpdateAvatarSetting(ctx, form, u); err != nil { + ctx.Flash.Error(err.Error()) + } else { + ctx.Flash.Success(ctx.Tr("settings.update_avatar_success")) + } + + ctx.Redirect(setting.AppSubURL + "/admin/users/" + fmt.Sprint(u.ID)) +} + +// DeleteAvatar render delete avatar page +func DeleteAvatar(ctx *context.Context) { + u := prepareUserInfo(ctx) + if ctx.Written() { + return + } + + if err := u.DeleteAvatar(); err != nil { + ctx.Flash.Error(err.Error()) + } + + ctx.Redirect(setting.AppSubURL + "/admin/users/" + fmt.Sprint(u.ID)) +} diff --git a/routers/web/web.go b/routers/web/web.go index 69f737c3e1f40..fe61049922168 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -408,6 +408,8 @@ func RegisterRoutes(m *web.Route) { m.Combo("/new").Get(admin.NewUser).Post(bindIgnErr(forms.AdminCreateUserForm{}), admin.NewUserPost) m.Combo("/{userid}").Get(admin.EditUser).Post(bindIgnErr(forms.AdminEditUserForm{}), admin.EditUserPost) m.Post("/{userid}/delete", admin.DeleteUser) + m.Post("/{userid}/avatar", bindIgnErr(forms.AvatarForm{}), admin.AvatarPost) + m.Post("/{userid}/avatar/delete", admin.DeleteAvatar) }) m.Group("/emails", func() { diff --git a/templates/admin/user/edit.tmpl b/templates/admin/user/edit.tmpl index fb0ccd22bb5f8..9475a0cce645c 100644 --- a/templates/admin/user/edit.tmpl +++ b/templates/admin/user/edit.tmpl @@ -155,6 +155,44 @@ + +

+ {{.i18n.Tr "settings.avatar"}} +

+
+
+ {{.CsrfTokenHtml}} + {{if not DisableGravatar}} +
+
+ + +
+
+
+ + +
+ {{end}} + +
+
+ + +
+
+ +
+ + +
+ +
+ + {{$.i18n.Tr "settings.delete_current_avatar"}} +
+
+
From 51d938307dcfdf99e68dcc00b06e06b881d2b8e2 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 15 Nov 2021 20:03:15 +0100 Subject: [PATCH 2/6] Fix lint and update success message --- options/locale/locale_en-US.ini | 1 + routers/web/admin/users.go | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 2069b20530149..3c371eb4492aa 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -539,6 +539,7 @@ delete_current_avatar = Delete Current Avatar uploaded_avatar_not_a_image = The uploaded file is not an image. uploaded_avatar_is_too_big = The uploaded file has exceeded the maximum size. update_avatar_success = Your avatar has been updated. +update_user_avatar_success = Your avatar has been updated. change_password = Update Password old_password = Current Password diff --git a/routers/web/admin/users.go b/routers/web/admin/users.go index 576a48b10753a..c8e8da6af1453 100644 --- a/routers/web/admin/users.go +++ b/routers/web/admin/users.go @@ -23,7 +23,6 @@ import ( "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/web/explore" - user_setting "code.gitea.io/gitea/routers/web/user/setting" router_user_setting "code.gitea.io/gitea/routers/web/user/setting" "code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/mailer" @@ -411,10 +410,10 @@ func AvatarPost(ctx *context.Context) { } form := web.GetForm(ctx).(*forms.AvatarForm) - if err := user_setting.UpdateAvatarSetting(ctx, form, u); err != nil { + if err := router_user_setting.UpdateAvatarSetting(ctx, form, u); err != nil { ctx.Flash.Error(err.Error()) } else { - ctx.Flash.Success(ctx.Tr("settings.update_avatar_success")) + ctx.Flash.Success(ctx.Tr("settings.update_user_avatar_success")) } ctx.Redirect(setting.AppSubURL + "/admin/users/" + fmt.Sprint(u.ID)) From 37ae588e8d28afde3d6b402b613fed4b8ceed459 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Mon, 15 Nov 2021 20:04:03 +0100 Subject: [PATCH 3/6] Fix message --- options/locale/locale_en-US.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 3c371eb4492aa..d7243c01964a9 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -539,7 +539,7 @@ delete_current_avatar = Delete Current Avatar uploaded_avatar_not_a_image = The uploaded file is not an image. uploaded_avatar_is_too_big = The uploaded file has exceeded the maximum size. update_avatar_success = Your avatar has been updated. -update_user_avatar_success = Your avatar has been updated. +update_user_avatar_success = The avatar has been updated. change_password = Update Password old_password = Current Password From ba6804ba810eca8564317651cf52dbf9a5deef1d Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Tue, 16 Nov 2021 17:50:15 +0100 Subject: [PATCH 4/6] Update options/locale/locale_en-US.ini Co-authored-by: Gusted --- options/locale/locale_en-US.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index d7243c01964a9..7a9c0adc47c7a 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -539,7 +539,7 @@ delete_current_avatar = Delete Current Avatar uploaded_avatar_not_a_image = The uploaded file is not an image. uploaded_avatar_is_too_big = The uploaded file has exceeded the maximum size. update_avatar_success = Your avatar has been updated. -update_user_avatar_success = The avatar has been updated. +update_user_avatar_success = The user's avatar has been updated. change_password = Update Password old_password = Current Password From 5e7ec138c0e029f2052fe3d0fdfc6db5bda59004 Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Tue, 16 Nov 2021 18:27:40 +0100 Subject: [PATCH 5/6] Update routers/web/admin/users.go Co-authored-by: zeripath --- routers/web/admin/users.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/web/admin/users.go b/routers/web/admin/users.go index c8e8da6af1453..dccbd330015a9 100644 --- a/routers/web/admin/users.go +++ b/routers/web/admin/users.go @@ -416,7 +416,7 @@ func AvatarPost(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("settings.update_user_avatar_success")) } - ctx.Redirect(setting.AppSubURL + "/admin/users/" + fmt.Sprint(u.ID)) + ctx.Redirect(setting.AppSubURL + "/admin/users/" + strconv.FormatInt(u.ID, 10)) } // DeleteAvatar render delete avatar page From e29abee8d17bc63657900082ee622f6fff5a4430 Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Tue, 16 Nov 2021 18:28:13 +0100 Subject: [PATCH 6/6] Update routers/web/admin/users.go Co-authored-by: zeripath --- routers/web/admin/users.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/web/admin/users.go b/routers/web/admin/users.go index dccbd330015a9..b1927c736c6b8 100644 --- a/routers/web/admin/users.go +++ b/routers/web/admin/users.go @@ -430,5 +430,5 @@ func DeleteAvatar(ctx *context.Context) { ctx.Flash.Error(err.Error()) } - ctx.Redirect(setting.AppSubURL + "/admin/users/" + fmt.Sprint(u.ID)) + ctx.Redirect(setting.AppSubURL + "/admin/users/" + strconv.FormatInt(u.ID, 10)) }