Minor corrections for account route.

This commit is contained in:
Florian
2018-05-13 15:11:19 +02:00
parent 3928efbae9
commit 1563e9ed43
2 changed files with 49 additions and 15 deletions
@@ -1,5 +1,6 @@
package org.codiki.account;
import java.io.IOException;
import java.util.Optional;
import javax.naming.AuthenticationException;
@@ -29,10 +30,12 @@ public class AccountService {
* @param pUser
* The user sent from client.
* @return The user populated with the generated token.
* @throws IOException
* If the credentials are bad.
* @throws AuthenticationException
* If the credentials are wrong.
*/
public UserDTO checkCredentials(HttpServletResponse pResponse, UserDTO pUser) {
public UserDTO checkCredentials(HttpServletResponse pResponse, UserDTO pUser) throws IOException {
UserDTO result = null;
Optional<User> user = userRepository.findByEmail(pUser.getEmail());
@@ -41,29 +44,28 @@ public class AccountService {
tokenService.addUser(user.get());
result = new UserDTO(user.get(), true);
} else {
pResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
pResponse.sendError(HttpServletResponse.SC_FORBIDDEN);
}
return result;
}
public boolean changePassword(final User pUser, final PasswordWrapperDTO pPasswordWrapper,
final HttpServletResponse pResponse) {
boolean result = false;
public void changePassword(final User pUser, final PasswordWrapperDTO pPasswordWrapper,
final HttpServletResponse pResponse) throws IOException {
if(pPasswordWrapper.getNewPassword().equals(pPasswordWrapper.getConfirmPassword())) {
// We fetch the connected user from database to get his hashed password
final Optional<User> userFromDb = userRepository.findById(pUser.getId());
if(userFromDb.isPresent() && StringUtils.compareHash(pPasswordWrapper.getOldPassword(),
userFromDb.get().getPassword())) {
result = true;
userFromDb.get().setPassword(StringUtils.hashPassword(pPasswordWrapper.getNewPassword()));
userRepository.save(userFromDb.get());
} else {
pResponse.setStatus(HttpServletResponse.SC_FORBIDDEN);
pResponse.sendError(HttpServletResponse.SC_FORBIDDEN,
"Le mot de passe saisi ne correspond pas au votre.");
}
} else {
pResponse.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Le mot de passe saisi ne correspond pas au votre.");
}
return result;
}
}