public void execute(Event<UIForgetPassword> event) throws Exception {
UIForgetPassword uiForm = event.getSource();
UILogin uilogin = uiForm.getParent();
WebuiRequestContext requestContext = event.getRequestContext();
PortalRequestContext portalContext = PortalRequestContext.getCurrentInstance();
MailService mailSrc = uiForm.getApplicationComponent(MailService.class);
OrganizationService orgSrc = uiForm.getApplicationComponent(OrganizationService.class);
String userName = uiForm.getUIStringInput(Username).getValue();
String email = uiForm.getUIStringInput(Email).getValue();
uiForm.reset();
User user = null;
String tokenId = null;
// User provided his username
if (userName != null) {
user = orgSrc.getUserHandler().findUserByName(userName);
if (user == null) {
requestContext.getUIApplication().addMessage(
new ApplicationMessage("UIForgetPassword.msg.user-not-exist", null));
return;
}
}
// User provided his email address
if (user == null && email != null) {
Query query = new Query();
// Querying on email won't work. PLIDM-12
// Note that querying on email is inefficient as it loops over all users...
query.setEmail(email);
PageList<User> users = orgSrc.getUserHandler().findUsers(query);
if (users.getAll().size() > 0) {
user = users.getAll().get(0);
} else {
requestContext.getUIApplication().addMessage(
new ApplicationMessage("UIForgetPassword.msg.email-not-exist", null));
return;
}
}
email = user.getEmail();
// Create token
RemindPasswordTokenService tokenService = uiForm.getApplicationComponent(RemindPasswordTokenService.class);
Credentials credentials = new Credentials(user.getUserName(), "");
tokenId = tokenService.createToken(credentials);
String portalName = URLEncoder.encode(Util.getUIPortal().getName(), "UTF-8");
ResourceBundle res = requestContext.getApplicationResourceBundle();
String headerMail = "headermail";
String footerMail = "footer";
try {
headerMail = res.getString(uiForm.getId() + ".mail.header") + "\n\n"
+ res.getString(uiForm.getId() + ".mail.user") + user.getUserName() + "\n"
+ res.getString(uiForm.getId() + ".mail.link");
footerMail = "\n\n\n" + res.getString(uiForm.getId() + ".mail.footer");
} catch (MissingResourceException e) {
log.error(e.getMessage(), e);
}
HttpServletRequest request = portalContext.getRequest();
String host = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
String activeLink = host + requestContext.getRequestContextPath() + "/public/" + portalName + "?"
+ ComponentURL.PORTAL_COMPONENT_ID + "=UIPortal&portal:action=RecoveryPasswordAndUsername&tokenId="
+ tokenId;
String mailText = headerMail + "\n" + activeLink + footerMail;