@@ -38,15 +38,16 @@ func Profile(ctx *context.Context) {
38
38
ctx .HTML (200 , tplSettingsProfile )
39
39
}
40
40
41
- func handleUsernameChange (ctx * context.Context , newName string ) {
41
+ // HandleUsernameChange handle username changes from user settings and admin interface
42
+ func HandleUsernameChange (ctx * context.Context , user * models.User , newName string ) {
42
43
// Non-local users are not allowed to change their username.
43
- if len (newName ) == 0 || ! ctx . User .IsLocal () {
44
+ if len (newName ) == 0 || ! user .IsLocal () {
44
45
return
45
46
}
46
47
47
48
// Check if user name has been changed
48
- if ctx . User .LowerName != strings .ToLower (newName ) {
49
- if err := models .ChangeUserName (ctx . User , newName ); err != nil {
49
+ if user .LowerName != strings .ToLower (newName ) {
50
+ if err := models .ChangeUserName (user , newName ); err != nil {
50
51
switch {
51
52
case models .IsErrUserAlreadyExist (err ):
52
53
ctx .Flash .Error (ctx .Tr ("form.username_been_taken" ))
@@ -68,12 +69,8 @@ func handleUsernameChange(ctx *context.Context, newName string) {
68
69
}
69
70
return
70
71
}
71
- log .Trace ("User name changed: %s -> %s" , ctx . User .Name , newName )
72
+ log .Trace ("User name changed: %s -> %s" , user .Name , newName )
72
73
}
73
-
74
- // In case it's just a case change
75
- ctx .User .Name = newName
76
- ctx .User .LowerName = strings .ToLower (newName )
77
74
}
78
75
79
76
// ProfilePost response for change user's profile
@@ -86,9 +83,13 @@ func ProfilePost(ctx *context.Context, form auth.UpdateProfileForm) {
86
83
return
87
84
}
88
85
89
- handleUsernameChange (ctx , form .Name )
90
- if ctx .Written () {
91
- return
86
+ if len (form .Name ) != 0 && ctx .User .Name != form .Name {
87
+ HandleUsernameChange (ctx , ctx .User , form .Name )
88
+ if ctx .Written () {
89
+ return
90
+ }
91
+ ctx .User .Name = form .Name
92
+ ctx .User .LowerName = strings .ToLower (form .Name )
92
93
}
93
94
94
95
ctx .User .FullName = form .FullName
0 commit comments