mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 00:20:25 +08:00 
			
		
		
		
	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>
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user