@@ -161,75 +161,90 @@ public void updateUser(long userId, UserRegisterForm userToUpdate, User authenti
161
161
throw new UserNotUpdatedException ("Only Admins are allowed to update other users." );
162
162
163
163
UserEntity userEntityToUpdate = userRepository .findByUserId (userId );
164
+ if (null == userEntityToUpdate )
165
+ throw new UserNotUpdatedException ("User does not exist, use register endpoint." );
166
+
164
167
Update newUpdate = new Update ();
165
- boolean changesWereMade = false ;
166
168
167
- // username
168
- String username = userToUpdate .getUsername ();
169
- if (null != username ) {
170
- if (!stringIsValid (username ))
171
- throw new UserNotUpdatedException ("Wanted to change username, but username was not valid." );
169
+ boolean changesWereMade = updateUserName (newUpdate , userToUpdate .getUsername ());
172
170
173
- User user = null ;
171
+ boolean usernameIsValid = stringIsValid (userToUpdate .getUsername ());
172
+ String lowerCaseUsername = usernameIsValid ? userToUpdate .getUsername ().toLowerCase () : userEntityToUpdate .getLowercaseUsername ();
173
+ boolean passwordWasUpdated = updatePassword (newUpdate , userToUpdate .getPassword (), userToUpdate .getConfirmationPassword (), lowerCaseUsername );
174
+ changesWereMade = passwordWasUpdated || changesWereMade ;
175
+
176
+ boolean userGroupsWereUpdated = updateGroups (newUpdate , userToUpdate .getGroupIds (), authenticatedUserIsAdmin );
177
+ changesWereMade = userGroupsWereUpdated || changesWereMade ;
178
+
179
+ if (!changesWereMade )
180
+ throw new UserNotUpdatedException ("No changes were made." );
181
+
182
+ Query query = new Query ();
183
+ query .addCriteria (Criteria .where ("userId" ).is (userId ));
184
+ mongoTemplate .findAndModify (query , newUpdate , UserEntity .class );
185
+ }
186
+
187
+ private boolean updateGroups (Update newUpdate , long [] groupIds , boolean authenticatedUserIsAdmin ) {
188
+ if (null != groupIds ) {
174
189
try {
175
- user = this .findUserByUsername (username );
176
- } catch (UserNotFoundException ignored ) {
177
- LOG .info ("Username '{}' is free to use." , username );
190
+ for (Groups group : groupRepository .getGroupsByIds (groupIds )) {
191
+ if (group == Groups .ADMIN && !authenticatedUserIsAdmin )
192
+ throw new UserNotUpdatedException ("Only admins can add users to group " + Groups .ADMIN .getDisplayName () + "." );
193
+ }
194
+ } catch (IllegalArgumentException exception ) {
195
+ throw new UserNotUpdatedException ("One or more groups do not exist." );
178
196
}
179
197
180
- if (null != user )
181
- throw new UserNotUpdatedException ("Username already taken." );
182
-
183
- changesWereMade = true ;
184
- newUpdate .set ("username" , username );
198
+ newUpdate .set ("groupIds" , groupIds );
199
+ return true ;
185
200
}
201
+ return false ;
202
+ }
186
203
187
- // pw
188
- if (null != userToUpdate .getPassword ()) {
189
- String password = userToUpdate .getPassword ();
190
- String confirmation = userToUpdate .getConfirmationPassword ();
204
+ private boolean updatePassword (Update newUpdate , String password , String confirmationPassword , String lowercaseUserName ) {
205
+ if (null != password ) {
191
206
192
- if (!stringIsValid (password ) || !stringIsValid (confirmation ))
207
+ if (!stringIsValid (password ) || !stringIsValid (confirmationPassword ))
193
208
throw new UserNotUpdatedException ("Wanted to change password, but password was not valid." );
194
209
195
210
if (!passwordIsValid (password ))
196
211
throw new UserNotUpdatedException ("Password needs to be at least 8 characters long and, contains at least one uppercase and lowercase letter and a number." );
197
212
198
- if (!password .contentEquals (confirmation ))
213
+ if (!password .contentEquals (confirmationPassword ))
199
214
throw new UserNotUpdatedException ("Passwords do not match." );
200
215
201
- if (password .toLowerCase ().contains (userEntityToUpdate . getLowercaseUsername () ))
216
+ if (password .toLowerCase ().contains (lowercaseUserName ))
202
217
throw new UserNotUpdatedException ("Username must not appear in password." );
203
218
204
- changesWereMade = true ;
205
219
newUpdate .set ("password" , password );
206
-
207
220
//update refreshToken
208
221
String newRefreshToken = AccessTokenBusinessService .generateRandomTokenValue ();
209
222
newUpdate .set ("refreshToken" , newRefreshToken );
223
+
224
+ return true ;
210
225
}
226
+ return false ;
227
+ }
228
+
229
+ private boolean updateUserName (Update update , String username ) {
230
+ if (null != username ) {
231
+ if (!stringIsValid (username ))
232
+ throw new UserNotUpdatedException ("Wanted to change username, but username was not valid." );
211
233
212
- // groups
213
- if (null != userToUpdate .getGroupIds ()) {
234
+ User user = null ;
214
235
try {
215
- for (Groups group : groupRepository .getGroupsByIds (userToUpdate .getGroupIds ())) {
216
- if (group == Groups .ADMIN && !authenticatedUserIsAdmin )
217
- throw new UserNotUpdatedException ("Only admins can add users to group " + Groups .ADMIN .getDisplayName () + "." );
218
- }
219
- } catch (IllegalArgumentException exception ) {
220
- throw new UserNotUpdatedException ("One or more groups do not exist." );
236
+ user = this .findUserByUsername (username );
237
+ } catch (UserNotFoundException ignored ) {
238
+ LOG .info ("Username '{}' is free to use." , username );
221
239
}
222
240
223
- changesWereMade = true ;
224
- newUpdate .set ("groupIds" , userToUpdate .getGroupIds ());
225
- }
226
-
227
- if (!changesWereMade )
228
- throw new UserNotUpdatedException ("No changes were made." );
241
+ if (null != user )
242
+ throw new UserNotUpdatedException ("Username already taken." );
229
243
230
- Query query = new Query ();
231
- query .addCriteria (Criteria .where ("userId" ).is (userId ));
232
- mongoTemplate .findAndModify (query , newUpdate , UserEntity .class );
244
+ update .set ("username" , username );
245
+ return true ;
246
+ }
247
+ return false ;
233
248
}
234
249
235
250
public long generateRandomUserId () {
0 commit comments