Skip to content

Commit 66923e0

Browse files
authored
Enhance USER_DISABLED_FEATURES to allow disabling change username or full name (#31959)
Fix #31958 Enhanced `USER_DISABLED_FEATURES`(also `EXTERNAL_USER_DISABLE_FEATURES`) option in `[admin]` section. Added following values: - `change_username`: Disable change username - `change_full_name`: Disable change full name --- Progress: - [x] Update code - [x] Update translations
1 parent 6a4eb12 commit 66923e0

File tree

5 files changed

+29
-5
lines changed

5 files changed

+29
-5
lines changed

Diff for: custom/conf/app.example.ini

+2
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,8 @@ LEVEL = Info
15071507
;; - manage_gpg_keys: a user cannot configure gpg keys
15081508
;; - manage_mfa: a user cannot configure mfa devices
15091509
;; - manage_credentials: a user cannot configure emails, passwords, or openid
1510+
;; - change_username: a user cannot change their username
1511+
;; - change_full_name: a user cannot change their full name
15101512
;;EXTERNAL_USER_DISABLE_FEATURES =
15111513

15121514
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Diff for: modules/setting/admin.go

+2
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ const (
2929
UserFeatureManageGPGKeys = "manage_gpg_keys"
3030
UserFeatureManageMFA = "manage_mfa"
3131
UserFeatureManageCredentials = "manage_credentials"
32+
UserFeatureChangeUsername = "change_username"
33+
UserFeatureChangeFullName = "change_full_name"
3234
)

Diff for: options/locale/locale_en-US.ini

+4-1
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ lang_select_error = Select a language from the list.
580580
581581
username_been_taken = The username is already taken.
582582
username_change_not_local_user = Non-local users are not allowed to change their username.
583+
change_username_disabled = Changing username is disabled.
584+
change_full_name_disabled = Changing full name is disabled.
583585
username_has_not_been_changed = Username has not been changed
584586
repo_name_been_taken = The repository name is already used.
585587
repository_force_private = Force Private is enabled: private repositories cannot be made public.
@@ -705,7 +707,8 @@ public_profile = Public Profile
705707
biography_placeholder = Tell us a little bit about yourself! (You can use Markdown)
706708
location_placeholder = Share your approximate location with others
707709
profile_desc = Control how your profile is show to other users. Your primary email address will be used for notifications, password recovery and web-based Git operations.
708-
password_username_disabled = Non-local users are not allowed to change their username. Please contact your site administrator for more details.
710+
password_username_disabled = You are not allowed to change their username. Please contact your site administrator for more details.
711+
password_full_name_disabled = You are not allowed to change their full name. Please contact your site administrator for more details.
709712
full_name = Full Name
710713
website = Website
711714
location = Location

Diff for: routers/web/user/setting/profile.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func ProfilePost(ctx *context.Context) {
6969
form := web.GetForm(ctx).(*forms.UpdateProfileForm)
7070

7171
if form.Name != "" {
72+
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureChangeUsername) {
73+
ctx.Flash.Error(ctx.Tr("user.form.change_username_disabled"))
74+
ctx.Redirect(setting.AppSubURL + "/user/settings")
75+
return
76+
}
7277
if err := user_service.RenameUser(ctx, ctx.Doer, form.Name); err != nil {
7378
switch {
7479
case user_model.IsErrUserIsNotLocal(err):
@@ -91,14 +96,23 @@ func ProfilePost(ctx *context.Context) {
9196
}
9297

9398
opts := &user_service.UpdateOptions{
94-
FullName: optional.Some(form.FullName),
9599
KeepEmailPrivate: optional.Some(form.KeepEmailPrivate),
96100
Description: optional.Some(form.Description),
97101
Website: optional.Some(form.Website),
98102
Location: optional.Some(form.Location),
99103
Visibility: optional.Some(form.Visibility),
100104
KeepActivityPrivate: optional.Some(form.KeepActivityPrivate),
101105
}
106+
107+
if form.FullName != "" {
108+
if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureChangeFullName) {
109+
ctx.Flash.Error(ctx.Tr("user.form.change_full_name_disabled"))
110+
ctx.Redirect(setting.AppSubURL + "/user/settings")
111+
return
112+
}
113+
opts.FullName = optional.Some(form.FullName)
114+
}
115+
102116
if err := user_service.UpdateUser(ctx, ctx.Doer, opts); err != nil {
103117
ctx.ServerError("UpdateUser", err)
104118
return

Diff for: templates/user/settings/profile.tmpl

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
<span class="text red tw-hidden" id="name-change-prompt"> {{ctx.Locale.Tr "settings.change_username_prompt"}}</span>
1313
<span class="text red tw-hidden" id="name-change-redirect-prompt"> {{ctx.Locale.Tr "settings.change_username_redirect_prompt"}}</span>
1414
</label>
15-
<input id="username" name="name" value="{{.SignedUser.Name}}" data-name="{{.SignedUser.Name}}" autofocus required {{if or (not .SignedUser.IsLocal) .IsReverseProxy}}disabled{{end}} maxlength="40">
16-
{{if or (not .SignedUser.IsLocal) .IsReverseProxy}}
15+
<input id="username" name="name" value="{{.SignedUser.Name}}" data-name="{{.SignedUser.Name}}" autofocus required {{if or (not .SignedUser.IsLocal) ($.UserDisabledFeatures.Contains "change_username") .IsReverseProxy}}disabled{{end}} maxlength="40">
16+
{{if or (not .SignedUser.IsLocal) ($.UserDisabledFeatures.Contains "change_username") .IsReverseProxy}}
1717
<p class="help text blue">{{ctx.Locale.Tr "settings.password_username_disabled"}}</p>
1818
{{end}}
1919
</div>
2020
<div class="field {{if .Err_FullName}}error{{end}}">
2121
<label for="full_name">{{ctx.Locale.Tr "settings.full_name"}}</label>
22-
<input id="full_name" name="full_name" value="{{.SignedUser.FullName}}" maxlength="100">
22+
<input id="full_name" name="full_name" value="{{.SignedUser.FullName}}" {{if ($.UserDisabledFeatures.Contains "change_full_name")}}disabled{{end}} maxlength="100">
23+
{{if ($.UserDisabledFeatures.Contains "change_full_name")}}
24+
<p class="help text blue">{{ctx.Locale.Tr "settings.password_full_name_disabled"}}</p>
25+
{{end}}
2326
</div>
2427
<div class="field {{if .Err_Email}}error{{end}}">
2528
<label>{{ctx.Locale.Tr "email"}}</label>

0 commit comments

Comments
 (0)