From 51870086bc611ec4f535dc08697922063a5847a2 Mon Sep 17 00:00:00 2001 From: forgejo-backport-action Date: Tue, 29 Jul 2025 10:48:51 +0200 Subject: [PATCH] [v12.0/forgejo] fix: allow admins to always rename users (#8719) **Backport:** https://codeberg.org/forgejo/forgejo/pulls/8715 Do not apply the rename restriction of non-local users if the doer is an admin (changes via the admin interface). This is a conscious choice and the admin knows better if they make such changes. Regression of c59a057297 Resolves forgejo/forgejo#3657 ## Release notes - Bug fixes - [PR](https://codeberg.org/forgejo/forgejo/pulls/8715): allow admins to always rename users Co-authored-by: Gusted Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8719 Reviewed-by: Earl Warren Co-authored-by: forgejo-backport-action Co-committed-by: forgejo-backport-action --- services/user/user.go | 3 ++- services/user/user_test.go | 8 +++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/services/user/user.go b/services/user/user.go index d90fbac978..d682d5a434 100644 --- a/services/user/user.go +++ b/services/user/user.go @@ -47,7 +47,8 @@ func renameUser(ctx context.Context, u *user_model.User, newUserName string, doe } // Non-local users are not allowed to change their username. - if !u.IsOrganization() && !u.IsLocal() { + // If the doer is an admin, then allow the rename - they know better. + if !doerIsAdmin && !u.IsOrganization() && !u.IsLocal() { return user_model.ErrUserIsNotLocal{ UID: u.ID, Name: u.Name, diff --git a/services/user/user_test.go b/services/user/user_test.go index 36f2776ad8..f1cab60a6d 100644 --- a/services/user/user_test.go +++ b/services/user/user_test.go @@ -145,10 +145,16 @@ func TestRenameUser(t *testing.T) { t.Run("Non-Local", func(t *testing.T) { u := &user_model.User{ + ID: 2, + Name: "old-name", Type: user_model.UserTypeIndividual, LoginType: auth.OAuth2, } - require.ErrorIs(t, RenameUser(db.DefaultContext, u, "user_rename"), user_model.ErrUserIsNotLocal{}) + require.ErrorIs(t, RenameUser(db.DefaultContext, u, "user_rename2"), user_model.ErrUserIsNotLocal{UID: 2, Name: "old-name"}) + + t.Run("Admin", func(t *testing.T) { + require.NoError(t, AdminRenameUser(t.Context(), u, "user_rename2")) + }) }) t.Run("Same username", func(t *testing.T) {