use of site.model.User in project jprime by bgjug.
the class UserController method createNewPassPost.
@RequestMapping(value = "createNewPassword", method = RequestMethod.POST)
public String createNewPassPost(@RequestParam(value = "tokenId", required = true) String tokenId, @RequestParam(value = "password", required = true) String password, @RequestParam(value = "cpassword", required = true) String cpassword, final RedirectAttributes redirectAttrs, Model model) {
User owner = resetPassService.checkTokenValidity(tokenId);
if (owner == null) {
model.addAttribute("error_msg", "Your token is invalid. Request a new one");
model.addAttribute("tokenId", tokenId);
return CREATE_NEW_PASSWORD_JSP;
}
if (StringUtils.isEmpty(password) || !password.equals(cpassword)) {
model.addAttribute("error_msg", "Passwords did not match");
model.addAttribute("tokenId", tokenId);
return CREATE_NEW_PASSWORD_JSP;
}
owner.setPassword(passwordEncoder.encode(password));
getUserRepository().save(owner);
resetPassService.setTokenToUsed(tokenId);
redirectAttrs.addFlashAttribute("user", owner);
return "redirect:/successfulPasswordChange";
}
use of site.model.User in project jprime by bgjug.
the class ResetPasswordService method setTokenToUsed.
public void setTokenToUsed(String tokenId) {
String tokenShaHex = Sha512DigestUtils.shaHex(tokenId);
ResetPasswordToken resetPasswordToken = resetPassRepository.findByTokenId(tokenShaHex);
User owner = resetPasswordToken.getOwner();
List<ResetPasswordToken> tokens = resetPassRepository.findAllByOwner(owner);
for (ResetPasswordToken token : tokens) {
token.setUsed(true);
}
resetPassRepository.saveAll(tokens);
}
use of site.model.User in project jprime by bgjug.
the class ResetPasswordTest method setUp.
@Before
public void setUp() throws Exception {
UserController userControllerBean = wac.getBean(UserController.class);
this.mailServiceMock = new MailServiceMock();
userControllerBean.setMailService(mailServiceMock);
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
User user = new User();
user.setEmail("testEmail@gmail.com");
user.setFirstName("Zhorzh");
user.setLastName("Raychev");
user.setPhone("0888181912");
user.setPassword("abcd1234");
userRepository.save(user);
}
use of site.model.User in project jprime by bgjug.
the class ResetPasswordTest method resetPasswordFullTest.
@Test
public void resetPasswordFullTest() throws Exception {
mockMvc.perform(post("/resetPassword").param("email", "testEmail@gmail.com")).andExpect(status().isFound()).andExpect(view().name("redirect:/resetPassword"));
assertThat(mailServiceMock.recipientAddresses.size(), is(1));
String lastMessageText = mailServiceMock.lastMessageText;
assertThat(lastMessageText, notNullValue());
String resetPassURL = getResetPassURL(lastMessageText);
assertThat(resetPassURL, notNullValue());
String tokenId = getTokenIdFromURL(resetPassURL);
assertThat(resetPassURL, notNullValue());
mockMvc.perform(get("/createNewPassword").param("tokenId", tokenId)).andExpect(status().isOk()).andExpect(view().name(CREATE_NEW_PASSWORD_JSP)).andExpect(model().attribute("tokenId", equalTo(tokenId)));
mockMvc.perform(post("/createNewPassword").param("tokenId", tokenId).param("password", "PASSWORDS").param("cpassword", "DONT MATCH")).andExpect(status().isOk()).andExpect(view().name(CREATE_NEW_PASSWORD_JSP)).andExpect(model().attribute("error_msg", notNullValue())).andExpect(model().attribute("tokenId", equalTo(tokenId)));
User user = userRepository.findUserByEmail("testEmail@gmail.com");
String oldPasswordHash = user.getPassword();
mockMvc.perform(post("/createNewPassword").param("tokenId", tokenId).param("password", "aaaa1234").param("cpassword", "aaaa1234")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/successfulPasswordChange"));
user = userRepository.findUserByEmail("testEmail@gmail.com");
String newPasswordHash = user.getPassword();
assertThat(oldPasswordHash, not(equalTo(newPasswordHash)));
mockMvc.perform(get("/createNewPassword").param("tokenId", tokenId)).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/home"));
}
use of site.model.User in project jprime by bgjug.
the class UserController method resetPassword.
@RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
public String resetPassword(@RequestParam(value = "email") String email, Model model, final RedirectAttributes redirectAttrs) {
EmailValidator mailValidator = new EmailValidator();
if (!mailValidator.isValid(email, null)) {
model.addAttribute("error_msg", "Please, enter a valid mail address");
return RESET_PASSWORD_JSP;
}
User user = getUserRepository().findUserByEmail(email);
if (user != null) {
String tokenId = resetPassService.createNewToken(user);
try {
String mailContent = buildResetMailContent(user, tokenId, "/resetPasswordMail.html");
String mailTitle = "Reset your JPrime password";
mailService.sendEmail(email, mailTitle, mailContent);
} catch (MessagingException | IOException | URISyntaxException e) {
logger.error("Error while sending ResetPassword Mail to " + user, e);
}
}
redirectAttrs.addFlashAttribute("sent_to_email", email);
return "redirect:/resetPassword";
}
Aggregations