String login = f.getString(Params.RETRIEVE_PASSWORD.PARAMS.LOGIN);
String email = f.getString(Params.RETRIEVE_PASSWORD.PARAMS.EMAIL);
String type = request.getParameter(Params.RETRIEVE_PASSWORD.PARAMS.TYPE);
request.removeAttribute(Params.RETRIEVE_PASSWORD.REQUEST.ERROR_KEY);
UserService us = ServiceFactory.getUserService();
User u = null;
if (Params.RETRIEVE_PASSWORD.CONST.TYPE_LOGIN.equals(type)) {
if (!StringUtils.isBlank(login)) {
u = us.getUser(login);
} else {
addWOJMessages(request, RessourceBundleApplicationMessage.error("woj.retrieve-password.error.no.login", null));
return "account.retrieve-password.show";
}
} else if (Params.RETRIEVE_PASSWORD.CONST.TYPE_EMAIL.equals(type)) {
if (!StringUtils.isBlank(email)) {
u = us.getUserByEmail(email);
} else {
addWOJMessages(request, RessourceBundleApplicationMessage.error("woj.retrieve-password.error.no.email", null));
return "account.retrieve-password.show";
}
} else {
addWOJMessages(request, RessourceBundleApplicationMessage.error("woj.retrieve-password.error.no.type", null));
return "account.retrieve-password.show";
}
if (u==null) {
// no user found
addWOJMessages(request, RessourceBundleApplicationMessage.error("woj.retrieve-password.error.no.user.found", null));
return "account.retrieve-password.show";
}
String newPassword = AlphaNumericalPassGenerator.INSTANCE.generate();
u.setPassword(ChecksumUtil.encodeAsString(ChecksumUtil.SHA_1, newPassword));
LOGGER.debug("changing password for user " + u.getLogin() + "[" + newPassword + ":" + u.getPassword() + "]");
us.update(u);
Portal.getInstance().getMailService().asyncSendMail(Portal.getInstance().getMailFactory().createRetrievePasswordMail(u.getEmail(), u.getLogin(), newPassword));
return null;
}
}