Skip to content

Commit

Permalink
add changePassword method in userService
Browse files Browse the repository at this point in the history
  • Loading branch information
slapers committed Sep 22, 2015
1 parent 745fe77 commit 68391f8
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/main/java/io/authomator/api/domain/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,32 @@ public User resetForgot(final String id, final String newPassword) throws UserNo
return userRepository.save(user);
}


//TODO: implement testing
/**
* Change password for a user by checking his current password before changing it
*
* @param id
* @param currentPassword
* @param newPassword
* @return
* @throws UserNotFoundException
* @throws InvalidCredentialsException
*/
public User changePassword(final String id, final String currentPassword, final String newPassword) throws UserNotFoundException, InvalidCredentialsException{

User user = userRepository.findOne(id);

if (user == null) {
throw new UserNotFoundException("mongoId: " + id);
}

if ( ! BCrypt.checkpw(currentPassword, user.getPassword())){
throw new InvalidCredentialsException(user.getEmail(), currentPassword);
};

user.setPassword(newPassword);
return userRepository.save(user);

}
}

0 comments on commit 68391f8

Please sign in to comment.