Skip to content

Commit 3be156f

Browse files
qwerty287Gustedzeripath
authored
Allow admins to change user avatars (#17661)
Adds the avatar change panel to the edit user page (bottom) and allows admins to change it this way Co-authored-by: Gusted <williamzijl7@hotmail.com> Co-authored-by: zeripath <art27@cantab.net>
1 parent bbffcc3 commit 3be156f

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ delete_current_avatar = Delete Current Avatar
545545
uploaded_avatar_not_a_image = The uploaded file is not an image.
546546
uploaded_avatar_is_too_big = The uploaded file has exceeded the maximum size.
547547
update_avatar_success = Your avatar has been updated.
548+
update_user_avatar_success = The user's avatar has been updated.
548549

549550
change_password = Update Password
550551
old_password = Current Password

routers/web/admin/users.go

+31
Original file line numberDiff line numberDiff line change
@@ -401,3 +401,34 @@ func DeleteUser(ctx *context.Context) {
401401
"redirect": setting.AppSubURL + "/admin/users",
402402
})
403403
}
404+
405+
// AvatarPost response for change user's avatar request
406+
func AvatarPost(ctx *context.Context) {
407+
u := prepareUserInfo(ctx)
408+
if ctx.Written() {
409+
return
410+
}
411+
412+
form := web.GetForm(ctx).(*forms.AvatarForm)
413+
if err := router_user_setting.UpdateAvatarSetting(ctx, form, u); err != nil {
414+
ctx.Flash.Error(err.Error())
415+
} else {
416+
ctx.Flash.Success(ctx.Tr("settings.update_user_avatar_success"))
417+
}
418+
419+
ctx.Redirect(setting.AppSubURL + "/admin/users/" + strconv.FormatInt(u.ID, 10))
420+
}
421+
422+
// DeleteAvatar render delete avatar page
423+
func DeleteAvatar(ctx *context.Context) {
424+
u := prepareUserInfo(ctx)
425+
if ctx.Written() {
426+
return
427+
}
428+
429+
if err := u.DeleteAvatar(); err != nil {
430+
ctx.Flash.Error(err.Error())
431+
}
432+
433+
ctx.Redirect(setting.AppSubURL + "/admin/users/" + strconv.FormatInt(u.ID, 10))
434+
}

routers/web/web.go

+2
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,8 @@ func RegisterRoutes(m *web.Route) {
408408
m.Combo("/new").Get(admin.NewUser).Post(bindIgnErr(forms.AdminCreateUserForm{}), admin.NewUserPost)
409409
m.Combo("/{userid}").Get(admin.EditUser).Post(bindIgnErr(forms.AdminEditUserForm{}), admin.EditUserPost)
410410
m.Post("/{userid}/delete", admin.DeleteUser)
411+
m.Post("/{userid}/avatar", bindIgnErr(forms.AvatarForm{}), admin.AvatarPost)
412+
m.Post("/{userid}/avatar/delete", admin.DeleteAvatar)
411413
})
412414

413415
m.Group("/emails", func() {

templates/admin/user/edit.tmpl

+38
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,44 @@
155155
</div>
156156
</form>
157157
</div>
158+
159+
<h4 class="ui top attached header">
160+
{{.i18n.Tr "settings.avatar"}}
161+
</h4>
162+
<div class="ui attached segment">
163+
<form class="ui form" action="{{.Link}}/avatar" method="post" enctype="multipart/form-data">
164+
{{.CsrfTokenHtml}}
165+
{{if not DisableGravatar}}
166+
<div class="inline field">
167+
<div class="ui radio checkbox">
168+
<input name="source" value="lookup" type="radio" {{if not .User.UseCustomAvatar}}checked{{end}}>
169+
<label>{{.i18n.Tr "settings.lookup_avatar_by_mail"}}</label>
170+
</div>
171+
</div>
172+
<div class="field {{if .Err_Gravatar}}error{{end}}">
173+
<label for="gravatar">Avatar {{.i18n.Tr "email"}}</label>
174+
<input id="gravatar" name="gravatar" value="{{.User.AvatarEmail}}" />
175+
</div>
176+
{{end}}
177+
178+
<div class="inline field">
179+
<div class="ui radio checkbox">
180+
<input name="source" value="local" type="radio" {{if .User.UseCustomAvatar}}checked{{end}}>
181+
<label>{{.i18n.Tr "settings.enable_custom_avatar"}}</label>
182+
</div>
183+
</div>
184+
185+
<div class="inline field">
186+
<label for="avatar">{{.i18n.Tr "settings.choose_new_avatar"}}</label>
187+
<input name="avatar" type="file" >
188+
</div>
189+
190+
<div class="field">
191+
<button class="ui green button">{{$.i18n.Tr "settings.update_avatar"}}</button>
192+
<a class="ui red button delete-post" data-request-url="{{.Link}}/avatar/delete" data-done-url="{{.Link}}">{{$.i18n.Tr "settings.delete_current_avatar"}}</a>
193+
</div>
194+
</form>
195+
</div>
158196
</div>
159197
</div>
160198

0 commit comments

Comments
 (0)