throws Exception {
String username = Util.getParam(params, Params.USERNAME);
String template = Util.getParam(params, Params.TEMPLATE, CHANGE_EMAIL_XSLT);
final User user = context.getBean(UserRepository.class).findOneByUsername(username);
if (user == null) {
throw new UserNotFoundEx(username);
}
// only let registered users change their password
if (user.getProfile() != Profile.RegisteredUser) {
// Don't throw OperationNotAllowedEx because it is not related to not having enough priviledges
throw new IllegalArgumentException("Only users with profile RegisteredUser can change their password using this option");
}
// get mail settings
GeonetContext gc = (GeonetContext) context.getHandlerContext(Geonet.CONTEXT_NAME);
SettingManager sm = gc.getBean(SettingManager.class);
String adminEmail = sm.getValue("system/feedback/email");
String thisSite = sm.getSiteName();
SettingInfo si = context.getBean(SettingInfo.class);
String siteURL = si.getSiteUrl() + context.getBaseUrl();
// construct change key - only valid today
String scrambledPassword = user.getPassword();
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String todaysDate = sdf.format(cal.getTime());
String changeKey = PasswordUtil.encode(context, scrambledPassword+todaysDate);
// generate email details using customisable stylesheet
// TODO: allow internationalised emails
Element root = new Element("root");
root.addContent(new Element("username").setText(username));
for (String email : user.getEmailAddresses()) {
root.addContent(new Element("email").setText(email));
}
root.addContent(new Element("site").setText(thisSite));
root.addContent(new Element("siteURL").setText(siteURL));
root.addContent(new Element("changeKey").setText(changeKey));