This repository was archived by the owner on Apr 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/ff 106 #34
Merged
Merged
Feature/ff 106 #34
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
43e1b95
fixed tests, added shallow logic.
open-schnick 390bcd3
updated feature files, and step def.
open-schnick 29d7aee
FF-106 Write Logic for User Edit
open-schnick 642715f
Tweaked Cucumber Steps. Fixed Serialization.
open-schnick fc1f171
Added additional check, fixed some bugs.
open-schnick 91faab1
FF-106 Wrote UnitTests for User Edit
open-schnick 4aa831b
Damn you password check.
open-schnick 596c144
Bumped Version to v0.0.5
open-schnick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,20 @@ | |
import de.filefighter.rest.domain.user.data.persistance.UserRepository; | ||
import de.filefighter.rest.domain.user.exceptions.UserNotFoundException; | ||
import de.filefighter.rest.domain.user.exceptions.UserNotRegisteredException; | ||
import de.filefighter.rest.domain.user.exceptions.UserNotUpdatedException; | ||
import de.filefighter.rest.domain.user.group.GroupRepository; | ||
import de.filefighter.rest.domain.user.group.Groups; | ||
import de.filefighter.rest.rest.exceptions.RequestDidntMeetFormalRequirementsException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.data.mongodb.core.MongoTemplate; | ||
import org.springframework.data.mongodb.core.query.Criteria; | ||
import org.springframework.data.mongodb.core.query.Query; | ||
import org.springframework.data.mongodb.core.query.Update; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Arrays; | ||
import java.util.regex.Pattern; | ||
|
||
import static de.filefighter.rest.domain.common.Utils.stringIsValid; | ||
|
@@ -26,16 +33,19 @@ public class UserBusinessService { | |
private final UserRepository userRepository; | ||
private final UserDtoService userDtoService; | ||
private final GroupRepository groupRepository; | ||
private final MongoTemplate mongoTemplate; | ||
|
||
|
||
private static final Logger LOG = LoggerFactory.getLogger(UserBusinessService.class); | ||
|
||
@Value("${filefighter.disable-password-check}") | ||
public boolean passwordCheckDisabled; | ||
|
||
public UserBusinessService(UserRepository userRepository, UserDtoService userDtoService, GroupRepository groupRepository) { | ||
public UserBusinessService(UserRepository userRepository, UserDtoService userDtoService, GroupRepository groupRepository, MongoTemplate mongoTemplate) { | ||
this.userRepository = userRepository; | ||
this.userDtoService = userDtoService; | ||
this.groupRepository = groupRepository; | ||
this.mongoTemplate = mongoTemplate; | ||
} | ||
|
||
public long getUserCount() { | ||
|
@@ -97,14 +107,15 @@ public void registerNewUser(UserRegisterForm newUser) { | |
|
||
// check pws. | ||
String password = newUser.getPassword(); | ||
passwordIsValid(password); | ||
if (!passwordIsValid(password)) | ||
throw new UserNotRegisteredException("Password needs to be at least 8 characters long and, contains at least one uppercase and lowercase letter and a number."); | ||
|
||
String confirmationPassword = newUser.getConfirmationPassword(); | ||
|
||
if (!password.contentEquals(confirmationPassword)) | ||
throw new UserNotRegisteredException("Passwords do not match."); | ||
|
||
if(password.toLowerCase().contains(username.toLowerCase())) | ||
if (password.toLowerCase().contains(username.toLowerCase())) | ||
throw new UserNotRegisteredException("Username must not appear in password."); | ||
|
||
//check groups | ||
|
@@ -130,14 +141,93 @@ public void registerNewUser(UserRegisterForm newUser) { | |
.build()); | ||
} | ||
|
||
public void passwordIsValid(String password) { | ||
public boolean passwordIsValid(String password) { | ||
if (!Utils.stringIsValid(password)) | ||
throw new UserNotRegisteredException("Password was empty."); | ||
return false; | ||
|
||
if(this.passwordCheckDisabled) return; | ||
if (this.passwordCheckDisabled) return true; | ||
|
||
Pattern pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=\\S+$).{8,20}$"); | ||
if (!pattern.matcher(password).matches()) | ||
throw new UserNotRegisteredException("Password needs to be at least 8 characters long and, contains at least one uppercase and lowercase letter and a number."); | ||
Pattern pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=\\S+).{8,20}$"); | ||
return pattern.matcher(password).matches(); | ||
} | ||
|
||
public void updateUser(long userId, UserRegisterForm userToUpdate, User authenticatedUser) { | ||
if (null == userToUpdate) | ||
throw new UserNotUpdatedException("No updates specified."); | ||
|
||
if(null == authenticatedUser.getGroups()) | ||
throw new UserNotUpdatedException("Authenticated User is not allowed"); | ||
|
||
boolean authenticatedUserIsAdmin = Arrays.stream(authenticatedUser.getGroups()).anyMatch(g -> g == Groups.ADMIN); | ||
if (userId != authenticatedUser.getId() && !authenticatedUserIsAdmin) | ||
throw new UserNotUpdatedException("Only Admins are allowed to update other users."); | ||
|
||
UserEntity userEntityToUpdate = userRepository.findByUserId(userId); | ||
Update newUpdate = new Update(); | ||
boolean changesWereMade = false; | ||
|
||
// username | ||
String username = userToUpdate.getUsername(); | ||
if (null != username) { | ||
if (!stringIsValid(username)) | ||
throw new UserNotUpdatedException("Wanted to change username, but username was not valid."); | ||
|
||
User user = null; | ||
try { | ||
user = this.findUserByUsername(username); | ||
} catch (UserNotFoundException ignored) { | ||
LOG.info("Username '{}' is free to use.", username); | ||
} | ||
|
||
if (null != user) | ||
throw new UserNotUpdatedException("Username already taken."); | ||
|
||
changesWereMade = true; | ||
newUpdate.set("username", username); | ||
} | ||
|
||
// pw | ||
if (null != userToUpdate.getPassword()) { | ||
String password = userToUpdate.getPassword(); | ||
String confirmation = userToUpdate.getConfirmationPassword(); | ||
|
||
if (!stringIsValid(password) || !stringIsValid(confirmation)) | ||
throw new UserNotUpdatedException("Wanted to change password, but password was not valid."); | ||
|
||
if (!passwordIsValid(password)) | ||
throw new UserNotUpdatedException("Password needs to be at least 8 characters long and, contains at least one uppercase and lowercase letter and a number."); | ||
|
||
if (!password.contentEquals(confirmation)) | ||
throw new UserNotUpdatedException("Passwords do not match."); | ||
|
||
if (password.toLowerCase().contains(userEntityToUpdate.getLowercaseUsername())) | ||
throw new UserNotUpdatedException("Username must not appear in password."); | ||
|
||
changesWereMade = true; | ||
newUpdate.set("password", password); | ||
} | ||
|
||
// groups | ||
if (null != userToUpdate.getGroupIds()) { | ||
try { | ||
for (Groups group : groupRepository.getGroupsByIds(userToUpdate.getGroupIds())) { | ||
if (group == Groups.ADMIN && !authenticatedUserIsAdmin) | ||
throw new UserNotUpdatedException("Only admins can add users to group " + Groups.ADMIN.getDisplayName()); | ||
} | ||
} catch (IllegalArgumentException exception) { | ||
throw new UserNotUpdatedException("One or more groups do not exist."); | ||
} | ||
|
||
changesWereMade = true; | ||
newUpdate.set("groupIds", userToUpdate.getGroupIds()); | ||
} | ||
|
||
if(!changesWereMade) | ||
throw new UserNotUpdatedException("No changes were made."); | ||
|
||
Query query = new Query(); | ||
query.addCriteria(Criteria.where("userId").is(userId)); | ||
mongoTemplate.findAndModify(query, newUpdate, UserEntity.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
} | ||
} | ||
|
22 changes: 22 additions & 0 deletions
22
src/main/java/de/filefighter/rest/domain/user/exceptions/UserNotUpdatedAdvise.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package de.filefighter.rest.domain.user.exceptions; | ||
|
||
import de.filefighter.rest.rest.ServerResponse; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ControllerAdvice | ||
class UserNotUpdatedAdvise { | ||
|
||
@ResponseBody | ||
@ExceptionHandler(UserNotUpdatedException.class) | ||
@ResponseStatus(HttpStatus.CONFLICT) | ||
ResponseEntity<ServerResponse> userNotUpdatedExceptionHandler(UserNotUpdatedException ex) { | ||
LoggerFactory.getLogger(UserNotUpdatedException.class).warn(ex.getMessage()); | ||
return new ResponseEntity<>(new ServerResponse(HttpStatus.CONFLICT, ex.getMessage()), HttpStatus.CONFLICT); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/de/filefighter/rest/domain/user/exceptions/UserNotUpdatedException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package de.filefighter.rest.domain.user.exceptions; | ||
|
||
public class UserNotUpdatedException extends RuntimeException{ | ||
public UserNotUpdatedException() { | ||
super("User could not get updated"); | ||
} | ||
|
||
public UserNotUpdatedException(String reason) { | ||
super("User could not get updated. "+reason); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but new username..