summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
author6543 <6543@obermui.de>2021-01-10 13:14:02 +0100
committerGitHub <noreply@github.com>2021-01-10 14:14:02 +0200
commit6b3b6f1833d07383d24d68ec220a18315ac36809 (patch)
treeaeceb687845f71a493b64c223297c0567d7f6bf5 /routers
parentd989247bb08d2b8eb144e7a0edeaedfc26d08175 (diff)
downloadgitea-6b3b6f1833d07383d24d68ec220a18315ac36809.tar.gz
gitea-6b3b6f1833d07383d24d68ec220a18315ac36809.zip
Add option to change username to the admin panel (#14229)
Co-authored-by: Bwko <bouwko@gmail.com> Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'routers')
-rw-r--r--routers/admin/users.go10
-rw-r--r--routers/user/setting/profile.go36
2 files changed, 27 insertions, 19 deletions
diff --git a/routers/admin/users.go b/routers/admin/users.go
index 1dc6d5bbe2..8a848b1f9d 100644
--- a/routers/admin/users.go
+++ b/routers/admin/users.go
@@ -18,6 +18,7 @@ import (
"code.gitea.io/gitea/modules/password"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/routers"
+ router_user_setting "code.gitea.io/gitea/routers/user/setting"
"code.gitea.io/gitea/services/mailer"
)
@@ -269,6 +270,15 @@ func EditUserPost(ctx *context.Context, form auth.AdminEditUserForm) {
u.HashPassword(form.Password)
}
+ if len(form.UserName) != 0 && u.Name != form.UserName {
+ if err := router_user_setting.HandleUsernameChange(ctx, u, form.UserName); err != nil {
+ ctx.Redirect(setting.AppSubURL + "/admin/users")
+ return
+ }
+ u.Name = form.UserName
+ u.LowerName = strings.ToLower(form.UserName)
+ }
+
if form.Reset2FA {
tf, err := models.GetTwoFactorByUID(u.ID)
if err != nil && !models.IsErrTwoFactorNotEnrolled(err) {
diff --git a/routers/user/setting/profile.go b/routers/user/setting/profile.go
index fe9ce098fe..c935b56230 100644
--- a/routers/user/setting/profile.go
+++ b/routers/user/setting/profile.go
@@ -38,42 +38,36 @@ func Profile(ctx *context.Context) {
ctx.HTML(200, tplSettingsProfile)
}
-func handleUsernameChange(ctx *context.Context, newName string) {
+// HandleUsernameChange handle username changes from user settings and admin interface
+func HandleUsernameChange(ctx *context.Context, user *models.User, newName string) error {
// Non-local users are not allowed to change their username.
- if len(newName) == 0 || !ctx.User.IsLocal() {
- return
+ 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"))
}
// Check if user name has been changed
- if ctx.User.LowerName != strings.ToLower(newName) {
- if err := models.ChangeUserName(ctx.User, newName); err != nil {
+ if user.LowerName != strings.ToLower(newName) {
+ if err := models.ChangeUserName(user, newName); err != nil {
switch {
case models.IsErrUserAlreadyExist(err):
ctx.Flash.Error(ctx.Tr("form.username_been_taken"))
- ctx.Redirect(setting.AppSubURL + "/user/settings")
case models.IsErrEmailAlreadyUsed(err):
ctx.Flash.Error(ctx.Tr("form.email_been_used"))
- ctx.Redirect(setting.AppSubURL + "/user/settings")
case models.IsErrNameReserved(err):
ctx.Flash.Error(ctx.Tr("user.form.name_reserved", newName))
- ctx.Redirect(setting.AppSubURL + "/user/settings")
case models.IsErrNamePatternNotAllowed(err):
ctx.Flash.Error(ctx.Tr("user.form.name_pattern_not_allowed", newName))
- ctx.Redirect(setting.AppSubURL + "/user/settings")
case models.IsErrNameCharsNotAllowed(err):
ctx.Flash.Error(ctx.Tr("user.form.name_chars_not_allowed", newName))
- ctx.Redirect(setting.AppSubURL + "/user/settings")
default:
ctx.ServerError("ChangeUserName", err)
}
- return
+ return err
}
- log.Trace("User name changed: %s -> %s", ctx.User.Name, newName)
+ log.Trace("User name changed: %s -> %s", user.Name, newName)
}
-
- // In case it's just a case change
- ctx.User.Name = newName
- ctx.User.LowerName = strings.ToLower(newName)
+ return nil
}
// ProfilePost response for change user's profile
@@ -86,9 +80,13 @@ func ProfilePost(ctx *context.Context, form auth.UpdateProfileForm) {
return
}
- handleUsernameChange(ctx, form.Name)
- if ctx.Written() {
- return
+ if len(form.Name) != 0 && ctx.User.Name != form.Name {
+ if err := HandleUsernameChange(ctx, ctx.User, form.Name); err != nil {
+ ctx.Redirect(setting.AppSubURL + "/user/settings")
+ return
+ }
+ ctx.User.Name = form.Name
+ ctx.User.LowerName = strings.ToLower(form.Name)
}
ctx.User.FullName = form.FullName