]> source.dussan.org Git - gitea.git/commitdiff
Enhance USER_DISABLED_FEATURES to allow disabling change username or full name (...
authorZisu Zhang <thezzisu@gmail.com>
Sat, 5 Oct 2024 20:41:38 +0000 (04:41 +0800)
committerGitHub <noreply@github.com>
Sat, 5 Oct 2024 20:41:38 +0000 (20:41 +0000)
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

custom/conf/app.example.ini
modules/setting/admin.go
options/locale/locale_en-US.ini
routers/web/user/setting/profile.go
templates/user/settings/profile.tmpl

index 69d541ff8d407ff1cca5cc95bea966c40e99aef7..7c7a43944f099f561cd9154df81a87e78c202bb4 100644 (file)
@@ -1507,6 +1507,8 @@ LEVEL = Info
 ;; - manage_gpg_keys: a user cannot configure gpg keys
 ;; - manage_mfa: a user cannot configure mfa devices
 ;; - manage_credentials: a user cannot configure emails, passwords, or openid
+;; - change_username: a user cannot change their username
+;; - change_full_name: a user cannot change their full name
 ;;EXTERNAL_USER_DISABLE_FEATURES =
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
index ca4e9b1d5835b242e5d6e2a82e633b1234340be8..fde291ade94ae4a9c8b520ac6520ccb3f58bc6b2 100644 (file)
@@ -29,4 +29,6 @@ const (
        UserFeatureManageGPGKeys     = "manage_gpg_keys"
        UserFeatureManageMFA         = "manage_mfa"
        UserFeatureManageCredentials = "manage_credentials"
+       UserFeatureChangeUsername    = "change_username"
+       UserFeatureChangeFullName    = "change_full_name"
 )
index e3b17f9a04f2d24c86cbf0bbd4d3cc519f786ada..a02d939b79eda81a7a18a988a7126e89406e47ec 100644 (file)
@@ -580,6 +580,8 @@ lang_select_error = Select a language from the list.
 
 username_been_taken = The username is already taken.
 username_change_not_local_user = Non-local users are not allowed to change their username.
+change_username_disabled = Changing username is disabled.
+change_full_name_disabled = Changing full name is disabled.
 username_has_not_been_changed = Username has not been changed
 repo_name_been_taken = The repository name is already used.
 repository_force_private = Force Private is enabled: private repositories cannot be made public.
@@ -705,7 +707,8 @@ public_profile = Public Profile
 biography_placeholder = Tell us a little bit about yourself! (You can use Markdown)
 location_placeholder = Share your approximate location with others
 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.
-password_username_disabled = Non-local users are not allowed to change their username. Please contact your site administrator for more details.
+password_username_disabled = You are not allowed to change their username. Please contact your site administrator for more details.
+password_full_name_disabled = You are not allowed to change their full name. Please contact your site administrator for more details.
 full_name = Full Name
 website = Website
 location = Location
index 554f6cd6ce3f92eeab643a8ae2553e799268c9bd..3b051c9b5f4b7fb5e4ba58aecc26272af99df248 100644 (file)
@@ -69,6 +69,11 @@ func ProfilePost(ctx *context.Context) {
        form := web.GetForm(ctx).(*forms.UpdateProfileForm)
 
        if form.Name != "" {
+               if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureChangeUsername) {
+                       ctx.Flash.Error(ctx.Tr("user.form.change_username_disabled"))
+                       ctx.Redirect(setting.AppSubURL + "/user/settings")
+                       return
+               }
                if err := user_service.RenameUser(ctx, ctx.Doer, form.Name); err != nil {
                        switch {
                        case user_model.IsErrUserIsNotLocal(err):
@@ -91,7 +96,6 @@ func ProfilePost(ctx *context.Context) {
        }
 
        opts := &user_service.UpdateOptions{
-               FullName:            optional.Some(form.FullName),
                KeepEmailPrivate:    optional.Some(form.KeepEmailPrivate),
                Description:         optional.Some(form.Description),
                Website:             optional.Some(form.Website),
@@ -99,6 +103,16 @@ func ProfilePost(ctx *context.Context) {
                Visibility:          optional.Some(form.Visibility),
                KeepActivityPrivate: optional.Some(form.KeepActivityPrivate),
        }
+
+       if form.FullName != "" {
+               if user_model.IsFeatureDisabledWithLoginType(ctx.Doer, setting.UserFeatureChangeFullName) {
+                       ctx.Flash.Error(ctx.Tr("user.form.change_full_name_disabled"))
+                       ctx.Redirect(setting.AppSubURL + "/user/settings")
+                       return
+               }
+               opts.FullName = optional.Some(form.FullName)
+       }
+
        if err := user_service.UpdateUser(ctx, ctx.Doer, opts); err != nil {
                ctx.ServerError("UpdateUser", err)
                return
index aaaf8f30db6d6fbd54fc9cd0161b3e40a040a635..9c7e2de2183243d9db1a9ab6cdd11e3d259f8bb6 100644 (file)
                                                <span class="text red tw-hidden" id="name-change-prompt"> {{ctx.Locale.Tr "settings.change_username_prompt"}}</span>
                                                <span class="text red tw-hidden" id="name-change-redirect-prompt"> {{ctx.Locale.Tr "settings.change_username_redirect_prompt"}}</span>
                                        </label>
-                                       <input id="username" name="name" value="{{.SignedUser.Name}}" data-name="{{.SignedUser.Name}}" autofocus required {{if or (not .SignedUser.IsLocal) .IsReverseProxy}}disabled{{end}} maxlength="40">
-                                       {{if or (not .SignedUser.IsLocal) .IsReverseProxy}}
+                                       <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">
+                                       {{if or (not .SignedUser.IsLocal) ($.UserDisabledFeatures.Contains "change_username") .IsReverseProxy}}
                                        <p class="help text blue">{{ctx.Locale.Tr "settings.password_username_disabled"}}</p>
                                        {{end}}
                                </div>
                                <div class="field {{if .Err_FullName}}error{{end}}">
                                        <label for="full_name">{{ctx.Locale.Tr "settings.full_name"}}</label>
-                                       <input id="full_name" name="full_name" value="{{.SignedUser.FullName}}" maxlength="100">
+                                       <input id="full_name" name="full_name" value="{{.SignedUser.FullName}}" {{if ($.UserDisabledFeatures.Contains "change_full_name")}}disabled{{end}} maxlength="100">
+                                       {{if ($.UserDisabledFeatures.Contains "change_full_name")}}
+                                       <p class="help text blue">{{ctx.Locale.Tr "settings.password_full_name_disabled"}}</p>
+                                       {{end}}
                                </div>
                                <div class="field {{if .Err_Email}}error{{end}}">
                                        <label>{{ctx.Locale.Tr "email"}}</label>