@@ -163,7 +163,7 @@ public void updateUser(long userId, UserRegisterForm userToUpdate, User authenti
163
163
164
164
Update newUpdate = new Update ();
165
165
166
- boolean changesWereMade = updateUserName (newUpdate , userToUpdate .getUsername ());
166
+ boolean changesWereMade = updateUserName (newUpdate , userEntityToUpdate , userToUpdate .getUsername ());
167
167
boolean passwordWasUpdated = updatePassword (newUpdate , userToUpdate .getPassword (), userToUpdate .getConfirmationPassword ());
168
168
changesWereMade = passwordWasUpdated || changesWereMade ;
169
169
boolean userGroupsWereUpdated = updateGroups (newUpdate , userToUpdate .getGroupIds (), authenticatedUserIsAdmin );
@@ -221,13 +221,19 @@ private boolean updatePassword(Update newUpdate, String password, String confirm
221
221
return false ;
222
222
}
223
223
224
- private boolean updateUserName (Update update , String username ) {
224
+ private boolean updateUserName (Update update , UserEntity userEntityToUpdate , String username ) {
225
225
if (null != username ) {
226
226
if (!stringIsValid (username ))
227
227
throw new UserNotUpdatedException ("Wanted to change username, but username was not valid." );
228
228
229
- if (null != getUserWithUsername (username ))
230
- throw new UserNotUpdatedException ("Username already taken." );
229
+ UserEntity userWithNewUsername = getUserWithUsername (username );
230
+ if (null != userWithNewUsername ) {
231
+ if (userWithNewUsername .getUserId () == userEntityToUpdate .getUserId ()) {
232
+ return false ;
233
+ } else {
234
+ throw new UserNotUpdatedException ("Username already taken." );
235
+ }
236
+ }
231
237
232
238
update .set ("username" , username );
233
239
update .set ("lowercaseUsername" , username .toLowerCase ());
0 commit comments