Search in sources :

Example 1 with ResetPasswordException

use of pl.pollub.cs.pentagoncafe.flare.exception.ResetPasswordException in project Flare-event-calendar by PollubCafe.

the class SecurityServiceImpl method resetPassword.

@Override
@Transactional(rollbackFor = { MessagingException.class, ObjectNotFoundException.class, ResetPasswordException.class })
public void resetPassword(EmailReqDTO emailReqDTO) {
    String recipientAddress = emailReqDTO.getEmail();
    User user = userRepository.findByEmail(recipientAddress).orElseThrow(() -> new ObjectNotFoundException(User.class, "e-mail", emailReqDTO.getEmail()));
    if (!user.isEnabled()) {
        throw new ResetPasswordException(messages.get("login.userAccount.disabled"));
    }
    if (user.isBanned()) {
        throw new ResetPasswordException(messages.get("login.userAccount.locked"));
    }
    String generatedPassword = RandomPasswordGenerator.generate();
    user.setPassword(passwordEncoder.encode(generatedPassword));
    userRepository.save(user);
    Email resetPasswordEmail = emailBuilder.buildResetPasswordEmail(generatedPassword).to(recipientAddress);
    this.sendResetPasswordEmail(resetPasswordEmail);
}
Also used : User(pl.pollub.cs.pentagoncafe.flare.domain.User) Email(pl.pollub.cs.pentagoncafe.flare.component.email.Email) HtmlEmail(pl.pollub.cs.pentagoncafe.flare.component.email.HtmlEmail) ObjectNotFoundException(pl.pollub.cs.pentagoncafe.flare.exception.ObjectNotFoundException) ResetPasswordException(pl.pollub.cs.pentagoncafe.flare.exception.ResetPasswordException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Transactional (org.springframework.transaction.annotation.Transactional)1 Email (pl.pollub.cs.pentagoncafe.flare.component.email.Email)1 HtmlEmail (pl.pollub.cs.pentagoncafe.flare.component.email.HtmlEmail)1 User (pl.pollub.cs.pentagoncafe.flare.domain.User)1 ObjectNotFoundException (pl.pollub.cs.pentagoncafe.flare.exception.ObjectNotFoundException)1 ResetPasswordException (pl.pollub.cs.pentagoncafe.flare.exception.ResetPasswordException)1