<%@ page import="org.apache.log4j.*" %> <%@ page import="com.ecyrd.jspwiki.*" %> <%@ page import="java.util.*" %> <%@ page import="javax.mail.*" %> <%@ page import="com.ecyrd.jspwiki.auth.user.*" %> <%@ page import="com.ecyrd.jspwiki.auth.*" %> <%@ page import="com.ecyrd.jspwiki.util.*" %> <%@ page errorPage="/Error.jsp" %> <%@ taglib uri="/WEB-INF/jspwiki.tld" prefix="wiki" %> <%! public void jspInit() { wiki = WikiEngine.getInstance( getServletConfig() ); } Logger log = Logger.getLogger("JSPWiki"); WikiEngine wiki; String message = null; public boolean resetPassword(WikiEngine wiki, HttpServletRequest request) { // Reset pw for account name String name = request.getParameter("name"); UserDatabase userDatabase = wiki.getUserManager().getUserDatabase(); boolean success = false; try { UserProfile profile = null; try { profile = userDatabase.find(name); } catch (NoSuchPrincipalException e) { // Try email as well } if (profile == null) { profile = userDatabase.findByEmail(name); } String email = profile.getEmail(); String randomPassword = TextUtil.generateRandomPassword(); // Try sending email first, as that is more likely to fail. String mailMessage = "As requested, your new password for login '" + profile.getLoginName() + "' is '" + randomPassword + "'.\n\n" + "You may log in at " + wiki.getURLConstructor().makeURL(WikiContext.NONE, "Login.jsp", true, "") + ".\n\n" + "--" + wiki.getApplicationName(); MailUtil.sendMessage( wiki.getWikiProperties(), email, "New password for " + wiki.getApplicationName(), mailMessage ); log.info("User "+email+" requested and received a new password."); // Mail succeeded. Now reset the password. // If this fails, we're kind of screwed, because we already emailed. profile.setPassword(randomPassword); userDatabase.save(profile); userDatabase.commit(); success = true; } catch (NoSuchPrincipalException e) { message = "No user or email '" + name + "' was found."; log.info("Tried to reset password for non-existent user '" + name + "'"); } catch (SendFailedException e) { message = "Internal error: couldn't send the email! Contact the site administrator, please."; log.error("Tried to reset password and got SendFailedException: " + e); } catch (Exception e) { message = "Internal error. Contact the site administrator, please."; log.error("Tried to reset password and got another exception: " + e); } return success; } %> <% // Create wiki context and check for authorization WikiContext wikiContext = wiki.createContext( request, WikiContext.VIEW ); if(!wikiContext.hasAccess( response )) return; WikiSession wikiSession = wikiContext.getWikiSession(); String action = request.getParameter("action"); response.setContentType("text/html; charset="+wiki.getContentEncoding() ); response.setHeader( "Cache-control", "max-age=0" ); response.setDateHeader( "Expires", new Date().getTime() ); response.setDateHeader( "Last-Modified", new Date().getTime() ); %> <wiki:Variable var="applicationname" />: <wiki:PageName />
<% boolean done = false; if ((action != null) && (action.equals("resetPassword"))) { if (resetPassword(wiki, request)) { done = true; wikiSession.addMessage("A new password has been emailed to the requested account."); %>

Password reset

Click here to log in once you retrieve your new password.

<% } else { // Error wikiSession.addMessage(message); %>

Unable to reset password. Please try again.

<% } } // Display something to ask for a username if (!done) { %>
Lost or forgot your password? Enter your account name or email here:
<%} %>