summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authortechknowlogick <techknowlogick@gitea.io>2023-03-14 03:45:21 -0400
committerGitHub <noreply@github.com>2023-03-14 03:45:21 -0400
commit03591f0f95823a0b1dcca969d2a3ed505c7e6d73 (patch)
tree0117b3cf1f27b4f0f7a0346fcf724fa3af0e3f8f /services
parentaac07d010f261c00fb3bd9644c71dc108c668c11 (diff)
downloadgitea-03591f0f95823a0b1dcca969d2a3ed505c7e6d73.tar.gz
gitea-03591f0f95823a0b1dcca969d2a3ed505c7e6d73.zip
add user rename endpoint to admin api (#22789)
this is a simple endpoint that adds the ability to rename users to the admin API. Note: this is not in a mergeable state. It would be better if this was handled by a PATCH/POST to the /api/v1/admin/users/{username} endpoint and the username is modified. --------- Co-authored-by: Jason Song <i@wolfogre.com>
Diffstat (limited to 'services')
-rw-r--r--services/agit/agit.go4
-rw-r--r--services/user/rename.go41
-rw-r--r--services/user/user.go16
3 files changed, 59 insertions, 2 deletions
diff --git a/services/agit/agit.go b/services/agit/agit.go
index b61cb6f3f5..32fc3cba4a 100644
--- a/services/agit/agit.go
+++ b/services/agit/agit.go
@@ -226,8 +226,8 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
}
// UserNameChanged handle user name change for agit flow pull
-func UserNameChanged(user *user_model.User, newName string) error {
- pulls, err := issues_model.GetAllUnmergedAgitPullRequestByPoster(user.ID)
+func UserNameChanged(ctx context.Context, user *user_model.User, newName string) error {
+ pulls, err := issues_model.GetAllUnmergedAgitPullRequestByPoster(ctx, user.ID)
if err != nil {
return err
}
diff --git a/services/user/rename.go b/services/user/rename.go
new file mode 100644
index 0000000000..af195d7d76
--- /dev/null
+++ b/services/user/rename.go
@@ -0,0 +1,41 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package user
+
+import (
+ "context"
+ "fmt"
+ "strings"
+
+ user_model "code.gitea.io/gitea/models/user"
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/services/agit"
+ container_service "code.gitea.io/gitea/services/packages/container"
+)
+
+func renameUser(ctx context.Context, u *user_model.User, newUserName string) error {
+ if u.IsOrganization() {
+ return fmt.Errorf("cannot rename organization")
+ }
+
+ if err := user_model.ChangeUserName(ctx, u, newUserName); err != nil {
+ return err
+ }
+
+ if err := agit.UserNameChanged(ctx, u, newUserName); err != nil {
+ return err
+ }
+ if err := container_service.UpdateRepositoryNames(ctx, u, newUserName); err != nil {
+ return err
+ }
+
+ u.Name = newUserName
+ u.LowerName = strings.ToLower(newUserName)
+ if err := user_model.UpdateUser(ctx, u, false); err != nil {
+ return err
+ }
+
+ log.Trace("User name changed: %s -> %s", u.Name, newUserName)
+ return nil
+}
diff --git a/services/user/user.go b/services/user/user.go
index f0b8fe1c31..d52a2f404b 100644
--- a/services/user/user.go
+++ b/services/user/user.go
@@ -27,6 +27,22 @@ import (
"code.gitea.io/gitea/services/packages"
)
+// RenameUser renames a user
+func RenameUser(ctx context.Context, u *user_model.User, newUserName string) error {
+ ctx, committer, err := db.TxContext(ctx)
+ if err != nil {
+ return err
+ }
+ defer committer.Close()
+ if err := renameUser(ctx, u, newUserName); err != nil {
+ return err
+ }
+ if err := committer.Commit(); err != nil {
+ return err
+ }
+ return err
+}
+
// DeleteUser completely and permanently deletes everything of a user,
// but issues/comments/pulls will be kept and shown as someone has been deleted,
// unless the user is younger than USER_DELETE_WITH_COMMENTS_MAX_DAYS.