@RequestMapping(value = "/{id}/resetPassword", method = RequestMethod.POST) public final BaseResponse resetPassword( @RequestHeader("Accept-Language") final String encoding, @PathVariable("id") final long id, @RequestParam("oldPassword") final String oldPassword, @RequestParam("password") final String password) { final BaseResponse response = new BaseResponse(); try { User user = userRepository.findOne(id); PasswordEncoder encoder = new BCryptPasswordEncoder(); if ((oldPassword.length() > 0) && !encoder.matches(oldPassword, user.getPassword())) { response.setError( ErrorCodeEnum.PASSWORD_NOT_MATCH, "The old password and the original password does not match"); LOGGER.error("The old password and the original password does not match"); return response; } String encryptionPassword = encoder.encode(password); user.setPassword(encryptionPassword); userRepository.save(user); response.setSuccess(); response.setResponseMessage("Success!"); } catch (Exception e) { response.setError(ErrorCodeEnum.SQL_QUERY_ERROR, e.getMessage()); LOGGER.error(e.getMessage()); } return response; }
@RequestMapping(value = "/createUser", method = RequestMethod.POST) public final BaseResponse createUser( @RequestHeader("Accept-Language") final String encoding, @RequestParam("username") final String username, @RequestParam("password") final String password, @RequestParam("enabled") final boolean enabled, @RequestParam("email") final String email, @RequestParam("groups") final Group group) { final BaseResponse response = new BaseResponse(); try { PasswordEncoder encoder = new BCryptPasswordEncoder(); User user = new User(); user.setUsername(username); String encryptionPassword = encoder.encode(password); user.setPassword(encryptionPassword); user.setEnabled(enabled); user.setEmail(email); user.setGroups(Arrays.asList(group)); userRepository.save(user); response.setSuccess(); response.setResponseMessage("Success!"); } catch (Exception e) { response.setError(ErrorCodeEnum.SQL_QUERY_ERROR, e.getMessage()); LOGGER.error(e.getMessage()); } return response; }