Search in sources :

Example 1 with User

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";
}
Also used : User(site.model.User) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with User

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);
}
Also used : User(site.model.User) ResetPasswordToken(site.model.ResetPasswordToken)

Example 3 with User

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);
}
Also used : User(site.model.User) Before(org.junit.Before)

Example 4 with 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"));
}
Also used : User(site.model.User) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with User

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";
}
Also used : EmailValidator(org.hibernate.validator.internal.constraintvalidators.bv.EmailValidator) User(site.model.User) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

User (site.model.User)8 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ResetPasswordToken (site.model.ResetPasswordToken)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 MessagingException (javax.mail.MessagingException)1 EmailValidator (org.hibernate.validator.internal.constraintvalidators.bv.EmailValidator)1 DateTime (org.joda.time.DateTime)1 Before (org.junit.Before)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1