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);
}
}
Aggregations