Search in sources :

Example 1 with PasswordResetMail

use of org.haiku.haikudepotserver.passwordreset.model.PasswordResetMail in project haikudepotserver by haiku.

the class PasswordResetServiceImpl method createTokenAndInvite.

/**
 * <p>This method will create a user password reset token and will also email the user
 * about it so that they are able to click on a link in the email, visit the application
 * server and get their password changed.</p>
 */
private void createTokenAndInvite(User user) throws PasswordResetException {
    Preconditions.checkArgument(null != user, "the user must be provided");
    Preconditions.checkState(!Strings.isNullOrEmpty(user.getEmail()), "the user must have an email configured");
    ObjectContext contextLocal = serverRuntime.newContext();
    User userLocal = User.getByObjectId(contextLocal, user.getObjectId());
    UserPasswordResetToken userPasswordResetToken = contextLocal.newObject(UserPasswordResetToken.class);
    userPasswordResetToken.setUser(userLocal);
    userPasswordResetToken.setCode(UUID.randomUUID().toString());
    userPasswordResetToken.setCreateTimestamp(new java.sql.Timestamp(Clock.systemUTC().millis()));
    PasswordResetMail mailModel = new PasswordResetMail();
    mailModel.setPasswordResetBaseUrl(baseUrl + "/" + URL_SEGMENT_PASSWORDRESET + "/");
    mailModel.setUserNickname(user.getNickname());
    mailModel.setUserPasswordResetTokenCode(userPasswordResetToken.getCode());
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom(from);
    message.setTo(user.getEmail());
    message.setSubject(fillFreemarkerTemplate(mailModel, MAIL_SUBJECT, user.getNaturalLanguage()));
    message.setText(fillFreemarkerTemplate(mailModel, MAIL_PLAINTEXT, user.getNaturalLanguage()));
    contextLocal.commitChanges();
    try {
        this.mailSender.send(message);
    } catch (MailException me) {
        throw new PasswordResetException("the password reset email to " + user.toString() + " was not able to be sent", me);
    }
}
Also used : User(org.haiku.haikudepotserver.dataobjects.User) UserPasswordResetToken(org.haiku.haikudepotserver.dataobjects.UserPasswordResetToken) SimpleMailMessage(org.springframework.mail.SimpleMailMessage) PasswordResetMail(org.haiku.haikudepotserver.passwordreset.model.PasswordResetMail) ObjectContext(org.apache.cayenne.ObjectContext) MailException(org.springframework.mail.MailException)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)1 User (org.haiku.haikudepotserver.dataobjects.User)1 UserPasswordResetToken (org.haiku.haikudepotserver.dataobjects.UserPasswordResetToken)1 PasswordResetMail (org.haiku.haikudepotserver.passwordreset.model.PasswordResetMail)1 MailException (org.springframework.mail.MailException)1 SimpleMailMessage (org.springframework.mail.SimpleMailMessage)1