Reset password box

Another use case that our application is going to support is the one that lets the user reset his/her password. This is a simplified use case, so the user must insert the username. Then the application just resets the password and sends an email with the new password to the account owner.

We are doing all the stuff we have talked about by now in this chapter, so we will be fast explaining what to do in order to add this support to our application.

First of all, let's create a support bean called ResetPwdHelper in the book.richfaces.advcm.modules.login package using this code:

@Name("resetPwdHelper")
public class ResetPwdHelper {
@In (create=true)
Renderer renderer;
@In (create=true)
EntityManager entityManager;
@In
FacesMessages facesMessages;
private String newPassword;
private String username;
public void reset() {
try {
String query = "select c from Contact c where c.username = :username";
Contact actor = (Contact)(entityManager.createQuery(query) .setParameter("username", getUsername()) .getSingleResult());
// Saving the new password (will be used
// by the email template)
setNewPassword(PasswordUtils.getRandomPassword());
// Updating the entity
actor.setPassword(getNewPassword());
// Saving it
entityManager.merge(actor);
// Sending the email renderer.render("/mailTemplates/resetPasswordMail.xhtml");
facesMessages.add(Severity.INFO, "#{messages['newPasswordSent']}");
// Empty the data
setUsername(null);
setNewPassword(null);
} catch (NoResultException e) { facesMessages.addToControl("username",Severity.ERROR, "#{messages['usernameNotFound']}");
}
}
reset password boxreset password boxusingpublic String getNewPassword() {
return newPassword;
}
public void setNewPassword(String forgotPwdNewPassword) {
this.newPassword = forgotPwdNewPassword;
}
@NotNull
public String getUsername() {
return username;
}
public void setUsername(String email) {
this.username = email;
}
}

The username property is the one that the user will fill from the form (notice that we used the @NotNull Hibernate Validator annotation in order to inform JSF that the property must not be empty); the reset() method retrieves the user with that username, changes the password, and informs him/her by sending an email with the new password.

Let's create the form create a file called resetPassword.xhtml inside the /view/user/ directory of our project (use home.xhtml as a template). Add the following code for the form inside the body definition:

<h:form id="login">
<rich:panel>
<f:facet name="header">
#{messages['forgotUsername']}
</f:facet>
<div class="dialog">
<h:panelGrid columns="3" rowClasses="prop"
columnClasses="name,value,validatormsg">
<h:outputLabel for="email">
#{messages['email']}:
</h:outputLabel>
<h:inputText id="email"
value="#{forgotUsernameHelper.email}">
<rich:beanValidator />
</h:inputText>
<rich:message for="email"
styleClass="messagesingle"
errorClass="errormsg"
infoClass="infomsg"
warnClass="warnmsg">
</rich:message>
</h:panelGrid>
</div>
</rich:panel>
<div class="actionButtons">
<a:commandButton value="#{messages['sendUsername']}"
action="#{forgotUsernameHelper.sendUsername}"
reRender="email">
<s:defaultAction />
</a:commandButton>
</div>
</h:form>

In this case, we have just the username field, so we use rich:beanValidator in the registration form instead of rich:graphValidator that we've seen. After this field, there is also an internationalized link for users who forgot their username (we'll see this in the next section).

You may also have noticed that now we are using an Ajax commandButton, which just re-renders the username field to empty it after the execution of the action (if you see the reset() method in the support bean, we empty the data after sending the email).

The last missing piece is the email template that the user will receive: let's create a file called resetPasswordMail.xhtml inside the /view/mailTemplates/ directory and open it.

Let's add the following code to it:

<m:message xmlns="http://www.w3.org/1999/xhtml"
xmlns:m="http://jboss.com/products/seam/mail"
xmlns:h="http://java.sun.com/jsf/html">
<m:from name="Advanced Contact Manager administration"> [email protected] </m:from>
<m:to name="#{resetPwdHelper.email}"> #{resetPwdHelper.email}
</m:to>
<m:subject>#{messages['resetPwdMailSubject']}</m:subject>
<m:body>
<p>
<h:outputText value="#{messages['resetPwdMailContent']}" /> <br/><br/>
<h:outputText value="#{messages['resetPwdMailPwdLabel']}: " /> <h:outputText value="#{resetPwdHelper.resetPwdNewPassword}" style="font-weight: bold;" />
</p>
</m:body>
</m:message>

We're done! We've created an internationalized email for our user, and here is a screenshot of the panel created:

Reset password box
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.131.255