mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Refactor rename user and rename organization (#24052)
This PR is a refactor at the beginning. And now it did 4 things. - [x] Move renaming organizaiton and user logics into services layer and merged as one function - [x] Support rename a user capitalization only. For example, rename the user from `Lunny` to `lunny`. We just need to change one table `user` and others should not be touched. - [x] Before this PR, some renaming were missed like `agit` - [x] Fix bug the API reutrned from `http.StatusNoContent` to `http.StatusOK`
This commit is contained in:
@@ -502,17 +502,15 @@ func RenameUser(ctx *context.APIContext) {
|
||||
return
|
||||
}
|
||||
|
||||
oldName := ctx.ContextUser.Name
|
||||
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.IsErrUsernameNotChanged(err):
|
||||
// Noop as username is not changed
|
||||
ctx.Status(http.StatusNoContent)
|
||||
case user_model.IsErrUserAlreadyExist(err):
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", ctx.Tr("form.username_been_taken"))
|
||||
case db.IsErrNameReserved(err):
|
||||
@@ -526,5 +524,7 @@ func RenameUser(ctx *context.APIContext) {
|
||||
}
|
||||
return
|
||||
}
|
||||
ctx.Status(http.StatusNoContent)
|
||||
|
||||
log.Trace("User name changed: %s -> %s", oldName, newName)
|
||||
ctx.Status(http.StatusOK)
|
||||
}
|
||||
|
@@ -22,8 +22,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
user_setting "code.gitea.io/gitea/routers/web/user/setting"
|
||||
"code.gitea.io/gitea/services/forms"
|
||||
"code.gitea.io/gitea/services/org"
|
||||
container_service "code.gitea.io/gitea/services/packages/container"
|
||||
org_service "code.gitea.io/gitea/services/org"
|
||||
repo_service "code.gitea.io/gitea/services/repository"
|
||||
user_service "code.gitea.io/gitea/services/user"
|
||||
)
|
||||
@@ -67,31 +66,23 @@ func SettingsPost(ctx *context.Context) {
|
||||
nameChanged := org.Name != form.Name
|
||||
|
||||
// Check if organization name has been changed.
|
||||
if org.LowerName != strings.ToLower(form.Name) {
|
||||
isExist, err := user_model.IsUserExist(ctx, org.ID, form.Name)
|
||||
if err != nil {
|
||||
ctx.ServerError("IsUserExist", err)
|
||||
return
|
||||
} else if isExist {
|
||||
if nameChanged {
|
||||
err := org_service.RenameOrganization(ctx, org, form.Name)
|
||||
switch {
|
||||
case user_model.IsErrUserAlreadyExist(err):
|
||||
ctx.Data["OrgName"] = true
|
||||
ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), tplSettingsOptions, &form)
|
||||
return
|
||||
} else if err = user_model.ChangeUserName(ctx, org.AsUser(), form.Name); err != nil {
|
||||
switch {
|
||||
case db.IsErrNameReserved(err):
|
||||
ctx.Data["OrgName"] = true
|
||||
ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tplSettingsOptions, &form)
|
||||
case db.IsErrNamePatternNotAllowed(err):
|
||||
ctx.Data["OrgName"] = true
|
||||
ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplSettingsOptions, &form)
|
||||
default:
|
||||
ctx.ServerError("ChangeUserName", err)
|
||||
}
|
||||
case db.IsErrNameReserved(err):
|
||||
ctx.Data["OrgName"] = true
|
||||
ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(db.ErrNameReserved).Name), tplSettingsOptions, &form)
|
||||
return
|
||||
}
|
||||
|
||||
if err := container_service.UpdateRepositoryNames(ctx, org.AsUser(), form.Name); err != nil {
|
||||
ctx.ServerError("UpdateRepositoryNames", err)
|
||||
case db.IsErrNamePatternNotAllowed(err):
|
||||
ctx.Data["OrgName"] = true
|
||||
ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(db.ErrNamePatternNotAllowed).Pattern), tplSettingsOptions, &form)
|
||||
return
|
||||
case err != nil:
|
||||
ctx.ServerError("org_service.RenameOrganization", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -186,7 +177,7 @@ func SettingsDelete(ctx *context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := org.DeleteOrganization(ctx.Org.Organization); err != nil {
|
||||
if err := org_service.DeleteOrganization(ctx.Org.Organization); err != nil {
|
||||
if models.IsErrUserOwnRepos(err) {
|
||||
ctx.Flash.Error(ctx.Tr("form.org_still_own_repo"))
|
||||
ctx.Redirect(ctx.Org.OrgLink + "/settings/delete")
|
||||
|
@@ -49,15 +49,16 @@ func Profile(ctx *context.Context) {
|
||||
|
||||
// HandleUsernameChange handle username changes from user settings and admin interface
|
||||
func HandleUsernameChange(ctx *context.Context, user *user_model.User, newName string) error {
|
||||
// Non-local users are not allowed to change their username.
|
||||
if !user.IsLocal() {
|
||||
ctx.Flash.Error(ctx.Tr("form.username_change_not_local_user"))
|
||||
return fmt.Errorf(ctx.Tr("form.username_change_not_local_user"))
|
||||
}
|
||||
|
||||
oldName := user.Name
|
||||
// rename user
|
||||
if err := user_service.RenameUser(ctx, user, newName); err != nil {
|
||||
switch {
|
||||
// Noop as username is not changed
|
||||
case user_model.IsErrUsernameNotChanged(err):
|
||||
ctx.Flash.Error(ctx.Tr("form.username_has_not_been_changed"))
|
||||
// Non-local users are not allowed to change their username.
|
||||
case user_model.IsErrUserIsNotLocal(err):
|
||||
ctx.Flash.Error(ctx.Tr("form.username_change_not_local_user"))
|
||||
case user_model.IsErrUserAlreadyExist(err):
|
||||
ctx.Flash.Error(ctx.Tr("form.username_been_taken"))
|
||||
case user_model.IsErrEmailAlreadyUsed(err):
|
||||
@@ -73,7 +74,7 @@ func HandleUsernameChange(ctx *context.Context, user *user_model.User, newName s
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
log.Trace("User name changed: %s -> %s", oldName, newName)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user