To implement the change password functionality into your custom portlet in Liferay 6.2, just follow the below steps:
1- Write the below line in liferay-portlet.xml file for the portlet in with you want to perform action to change the password
<private-session-attributes>false</private-session-attributes>
2- Write the below lines in protal-ext.properties to enable the session to store the current password after login
session.shared.attributes.excludes = USER_PASSWORD
session.store.password=true
3- Write the below code to implement the UI for "Change Password" in the jsp file
change_password.jsp
<liferay-portlet:actionURL var="updateUserPassword" name="updateUserPassword">
</liferay-portlet:actionURL>
<aui:form action="<%=updateUserPassword%>" id="updateForm" name="updateForm" method="post" autocomplete="off">
<liferay-ui:error exception="<%= UserPasswordException.class %>"
message="that-password-has-already-been-used-please-enter-in-a-different-password" />
<liferay-ui:error key="error-key" message="Current Password is invalid."></liferay-ui:error>
<aui:fieldset cssClass="lfr-portrait-editor">
<aui:input autoFocus="<%= true %>" type="password" label="Current Password" value="" name="currentPassword" autocomplete="off">
<aui:validator name="required" />
</aui:input>
<aui:input label="New Password" name="password1" size="30" type="password" value="">
<aui:validator name="required" />
</aui:input>
<aui:input label="Enter Again" name="password2" size="30" type="password" value="">
<aui:validator name="required" />
<aui:validator name="equalTo">
'#<portlet:namespace />password1'
</aui:validator>
</aui:input>
<aui:button-row>
<aui:button type="submit"/>
</aui:button-row>
</aui:fieldset>
</aui:form>
4- Finally write the below method in portlet action file
public void updateUserPassword(ActionRequest actionRequest,ActionResponse actionResponse) throws PortalException, SystemException, EncryptorException, IOException, PortletException
{
try {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
HttpSession session = request.getSession();
//Get the user's current password from session
String loginPassword = (String)session.getAttribute(WebKeys.USER_PASSWORD);
String currentPassword = (String) actionRequest.getParameter("currentPassword").trim();
String newPassword = (String) actionRequest.getParameter("password1").trim();
String confirmPassword = (String) actionRequest.getParameter("password2").trim();
if (loginPassword.equals(currentPassword) && newPassword.equals(confirmPassword)) {
UserLocalServiceUtil.updatePassword(themeDisplay.getUserId(), newPassword,confirmPassword, false);
//Set the entered new password into session after changing the password
session.setAttribute(WebKeys.USER_PASSWORD, newPassword);
SessionMessages.add(actionRequest, "request_processed", "Password updated successfully.");
actionRequest.setAttribute("passwordUpdated", "updated");
actionResponse.setRenderParameter("mvcPath", "/change_password.jsp");
} else {
SessionErrors.add(actionRequest, "error-key", "Sorry, Something wrong.");
actionResponse.setRenderParameter("mvcPath", "/change_password.jsp");
}
} catch (UserPasswordException e) {
SessionErrors.add(actionRequest, UserPasswordException.class.getName(), e);
actionResponse.setRenderParameter("mvcPath", "/change_password.jsp");
}
}
Hope this will help you :)
0 Comment(s)