diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-05-21 23:13:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-21 23:13:47 +0800 |
commit | c59a057297c782f44a81a3e630b5094a58099edb (patch) | |
tree | 58c3020b0da9755fa769619bb3ad80e656f25cd9 /routers/web/user/setting | |
parent | 64f6a5d113da0d5d187752c9398d6e8d22d24b79 (diff) | |
download | gitea-c59a057297c782f44a81a3e630b5094a58099edb.tar.gz gitea-c59a057297c782f44a81a3e630b5094a58099edb.zip |
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`
Diffstat (limited to 'routers/web/user/setting')
-rw-r--r-- | routers/web/user/setting/profile.go | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/routers/web/user/setting/profile.go b/routers/web/user/setting/profile.go index 0a8a5e6280..47066d5e38 100644 --- a/routers/web/user/setting/profile.go +++ b/routers/web/user/setting/profile.go @@ -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 } |