aboutsummaryrefslogtreecommitdiffstats
path: root/routers
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 /routers
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 'routers')
-rw-r--r--routers/api/v1/admin/user.go58
-rw-r--r--routers/api/v1/api.go1
-rw-r--r--routers/api/v1/swagger/options.go3
-rw-r--r--routers/web/org/setting.go2
-rw-r--r--routers/web/user/setting/profile.go52
5 files changed, 78 insertions, 38 deletions
diff --git a/routers/api/v1/admin/user.go b/routers/api/v1/admin/user.go
index 4192d8654d..369d13943a 100644
--- a/routers/api/v1/admin/user.go
+++ b/routers/api/v1/admin/user.go
@@ -461,3 +461,61 @@ func GetAllUsers(ctx *context.APIContext) {
ctx.SetTotalCountHeader(maxResults)
ctx.JSON(http.StatusOK, &results)
}
+
+// RenameUser api for renaming a user
+func RenameUser(ctx *context.APIContext) {
+ // swagger:operation POST /admin/users/{username}/rename admin adminRenameUser
+ // ---
+ // summary: Rename a user
+ // produces:
+ // - application/json
+ // parameters:
+ // - name: username
+ // in: path
+ // description: existing username of user
+ // type: string
+ // required: true
+ // - name: body
+ // in: body
+ // required: true
+ // schema:
+ // "$ref": "#/definitions/RenameUserOption"
+ // responses:
+ // "204":
+ // "$ref": "#/responses/empty"
+ // "403":
+ // "$ref": "#/responses/forbidden"
+ // "422":
+ // "$ref": "#/responses/validationError"
+
+ if ctx.ContextUser.IsOrganization() {
+ ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("%s is an organization not a user", ctx.ContextUser.Name))
+ return
+ }
+
+ newName := web.GetForm(ctx).(*api.RenameUserOption).NewName
+
+ if strings.EqualFold(newName, ctx.ContextUser.Name) {
+ // Noop as username is not changed
+ ctx.Status(http.StatusNoContent)
+ return
+ }
+
+ // Check if user name has been changed
+ if err := user_service.RenameUser(ctx, ctx.ContextUser, newName); err != nil {
+ switch {
+ case user_model.IsErrUserAlreadyExist(err):
+ ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("form.username_been_taken"))
+ case db.IsErrNameReserved(err):
+ ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_reserved", newName))
+ case db.IsErrNamePatternNotAllowed(err):
+ ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_pattern_not_allowed", newName))
+ case db.IsErrNameCharsNotAllowed(err):
+ ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("user.form.name_chars_not_allowed", newName))
+ default:
+ ctx.ServerError("ChangeUserName", err)
+ }
+ return
+ }
+ ctx.Status(http.StatusNoContent)
+}
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go
index 735939a551..7001dc72ac 100644
--- a/routers/api/v1/api.go
+++ b/routers/api/v1/api.go
@@ -1257,6 +1257,7 @@ func Routes(ctx gocontext.Context) *web.Route {
m.Get("/orgs", org.ListUserOrgs)
m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
+ m.Post("/rename", bind(api.RenameUserOption{}), admin.RenameUser)
}, context_service.UserAssignmentAPI())
})
m.Group("/unadopted", func() {
diff --git a/routers/api/v1/swagger/options.go b/routers/api/v1/swagger/options.go
index 979b184075..0c8d3d353f 100644
--- a/routers/api/v1/swagger/options.go
+++ b/routers/api/v1/swagger/options.go
@@ -49,6 +49,9 @@ type swaggerParameterBodies struct {
CreateKeyOption api.CreateKeyOption
// in:body
+ RenameUserOption api.RenameUserOption
+
+ // in:body
CreateLabelOption api.CreateLabelOption
// in:body
EditLabelOption api.EditLabelOption
diff --git a/routers/web/org/setting.go b/routers/web/org/setting.go
index b57ebfbcda..654e9000fa 100644
--- a/routers/web/org/setting.go
+++ b/routers/web/org/setting.go
@@ -79,7 +79,7 @@ func SettingsPost(ctx *context.Context) {
ctx.Data["OrgName"] = true
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSettingsOptions, &form)
return
- } else if err = user_model.ChangeUserName(org.AsUser(), form.Name); err != nil {
+ } else if err = user_model.ChangeUserName(ctx, org.AsUser(), form.Name); err != nil {
switch {
case db.IsErrNameReserved(err):
ctx.Data["OrgName"] = true
diff --git a/routers/web/user/setting/profile.go b/routers/web/user/setting/profile.go
index f0f053a514..f500be7632 100644
--- a/routers/web/user/setting/profile.go
+++ b/routers/web/user/setting/profile.go
@@ -27,9 +27,7 @@ import (
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/modules/web/middleware"
- "code.gitea.io/gitea/services/agit"
"code.gitea.io/gitea/services/forms"
- container_service "code.gitea.io/gitea/services/packages/container"
user_service "code.gitea.io/gitea/services/user"
)
@@ -57,45 +55,25 @@ func HandleUsernameChange(ctx *context.Context, user *user_model.User, newName s
return fmt.Errorf(ctx.Tr("form.username_change_not_local_user"))
}
- // Check if user name has been changed
- if user.LowerName != strings.ToLower(newName) {
- if err := user_model.ChangeUserName(user, newName); err != nil {
- switch {
- case user_model.IsErrUserAlreadyExist(err):
- ctx.Flash.Error(ctx.Tr("form.username_been_taken"))
- case user_model.IsErrEmailAlreadyUsed(err):
- ctx.Flash.Error(ctx.Tr("form.email_been_used"))
- case db.IsErrNameReserved(err):
- ctx.Flash.Error(ctx.Tr("user.form.name_reserved", newName))
- case db.IsErrNamePatternNotAllowed(err):
- ctx.Flash.Error(ctx.Tr("user.form.name_pattern_not_allowed", newName))
- case db.IsErrNameCharsNotAllowed(err):
- ctx.Flash.Error(ctx.Tr("user.form.name_chars_not_allowed", newName))
- default:
- ctx.ServerError("ChangeUserName", err)
- }
- return err
- }
- } else {
- if err := repo_model.UpdateRepositoryOwnerNames(user.ID, newName); err != nil {
- ctx.ServerError("UpdateRepository", err)
- return err
+ // rename user
+ if err := user_service.RenameUser(ctx, user, newName); err != nil {
+ switch {
+ case user_model.IsErrUserAlreadyExist(err):
+ ctx.Flash.Error(ctx.Tr("form.username_been_taken"))
+ case user_model.IsErrEmailAlreadyUsed(err):
+ ctx.Flash.Error(ctx.Tr("form.email_been_used"))
+ case db.IsErrNameReserved(err):
+ ctx.Flash.Error(ctx.Tr("user.form.name_reserved", newName))
+ case db.IsErrNamePatternNotAllowed(err):
+ ctx.Flash.Error(ctx.Tr("user.form.name_pattern_not_allowed", newName))
+ case db.IsErrNameCharsNotAllowed(err):
+ ctx.Flash.Error(ctx.Tr("user.form.name_chars_not_allowed", newName))
+ default:
+ ctx.ServerError("ChangeUserName", err)
}
- }
-
- // update all agit flow pull request header
- err := agit.UserNameChanged(user, newName)
- if err != nil {
- ctx.ServerError("agit.UserNameChanged", err)
- return err
- }
-
- if err := container_service.UpdateRepositoryNames(ctx, user, newName); err != nil {
- ctx.ServerError("UpdateRepositoryNames", err)
return err
}
- log.Trace("User name changed: %s -> %s", user.Name, newName)
return nil
}