Liferay Custom Portlet To Change Password

create-your-own-portlet-to-change-liferay-password


aim

In this article we will create a custom portlet to change liferay password. Sometimes we need our own functionality to change liferay credentials. For example consider you working on user management where you want user to update their own password. This article is a quick recipe to build our own custom portlet to change password effectively. 


Before you continue this article we suggest you to go through the below article

AuthType:3 Ways to Login Liferay

Source Code Download:

The source code of the portlet is attached at the bottom of this article. You can download the portlet and deploy in your liferay server. After deployment the portlet will look like this

change-password-portlet

Liferay API to change password:

In liferay source code open the file UserLocalServiceImpl.java. Below is the method we can use for changing password of the logged in user

[example title=”Method Signature”]
public long authenticateForBasic(
long companyId, String authType, String login, String password)
throws PortalException, SystemException {
[/example]

The explanation of the method is explained below

method-signature-change-password

 

On the basis of the above method below is the portlet class developed. Read the comments carefully in the code.

[example title=”Source code UpdatePortlet.java”]
package com.proliferay.demo.portlet;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.ProcessAction;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.CompanyConstants;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class UpdatePortlet extends MVCPortlet{

@ProcessAction(name=”updatePassword”)
public void updatePassword(ActionRequest actionRequest, ActionResponse actionResponse){
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
_log.info(“:::::::::::::calling updatePassword::::::::::::::::”);
String current = ParamUtil.getString(actionRequest, “current”,””);
String password1 = ParamUtil.getString(actionRequest, “password1″,””);
String password2 = ParamUtil.getString(actionRequest, “password2″,””);
String errorKey = “”;

try {
String authType = themeDisplay.getCompany().getAuthType();
String login = “”;
/**
* authType can be of three types.
* Therefore based on authType login can email address or
* screen name or user id of the logged in user
*/
if(authType.equals(CompanyConstants.AUTH_TYPE_EA)){
login = themeDisplay.getUser().getEmailAddress();
}else if(authType.equals(CompanyConstants.AUTH_TYPE_SN)){
login = themeDisplay.getUser().getScreenName();
}else if(authType.equals(CompanyConstants.AUTH_TYPE_ID)){
login = String.valueOf(themeDisplay.getUser().getUserId());
}

/**
* The method authenticateForBasic returns userId of the logged in user if all
* the parameters in the method are correct. Otherwise it will return 0.
* Notice the if condition
*/

long userId=UserLocalServiceUtil.authenticateForBasic(themeDisplay.getCompanyId(), authType, login, current);
if(themeDisplay.getUserId()!=userId)
{
errorKey = “invalid-current-password”;
throw new Exception(“Invalid current password.”);
}
if(!password1.equals(password2))
{
errorKey = “confirm-new-password”;
throw new Exception(“Please confirm new password.”);
}
UserLocalServiceUtil.updatePassword(userId, password1, password2, false);

} catch (PortalException e) {
_log.error(e.getMessage(), e);
} catch (SystemException e) {
_log.error(e.getMessage(), e);
} catch (Exception e) {
_log.error(e.getMessage(), e);
}

if(Validator.isNull(errorKey)){
SessionMessages.add(actionRequest, “password-update-success”);
}else{
SessionErrors.add(actionRequest, errorKey);
}

}

private Log _log = LogFactoryUtil.getLog(UpdatePortlet.class.getName());

}
[/example]

Below is the source code of JSP

[example title=”Source code view.jsp”]
<%@ taglib uri=”http://java.sun.com/portlet_2_0″ prefix=”portlet” %>
<%@ taglib uri=”http://alloy.liferay.com/tld/aui” prefix=”aui” %>
<%@ taglib uri=”http://liferay.com/tld/ui” prefix=”liferay-ui” %>

<portlet:defineObjects />

<portlet:actionURL var=”updatePasswordURL” name=”updatePassword”/>

<liferay-ui:success key=”password-update-success”  message=”Password Updated Successfully”/>

<liferay-ui:error key=”invalid-current-password” message=”Invalid Current Password”/>
<liferay-ui:error key=”confirm-new-password” message=”New password and confirm password should be same”/>

<aui:form method=”post” action=”<%=updatePasswordURL.toString() %>”>

<aui:input  name=”current” type=”password” label=”current-password”>
<aui:validator name=”required”/>
</aui:input>
<aui:input  name=”password1″ type=”password” label=”new-password”>
<aui:validator name=”required”/>
</aui:input>

<aui:input  name=”password2″ type=”password” label=”confirm-password”>
<aui:validator name=”required”/>
</aui:input>

<span id=”<portlet:namespace />passwordCapsLockSpan” style=”display: none;”><liferay-ui:message key=”caps-lock-is-on” /></span>
<aui:button-row>
<aui:button type=”submit” value=”Update Password”/>
</aui:button-row>

</aui:form>

<aui:script use=”aui-base”>
var current = A.one(‘#<portlet:namespace />current’);
var password1 = A.one(‘#<portlet:namespace />password1’);
var password2 = A.one(‘#<portlet:namespace />password2’);

if (current) {
current.on(
‘keypress’,
function(event) {
Liferay.Util.showCapsLock(event, ‘<portlet:namespace />passwordCapsLockSpan’);
}
);
}

if (password1) {
password1.on(
‘keypress’,
function(event) {
Liferay.Util.showCapsLock(event, ‘<portlet:namespace />passwordCapsLockSpan’);
}
);
}

if (password2) {
password2.on(
‘keypress’,
function(event) {
Liferay.Util.showCapsLock(event, ‘<portlet:namespace />passwordCapsLockSpan’);
}
);
}
</aui:script>
[/example]

 Download Source Code

About The Author

1 thought on “Liferay Custom Portlet To Change Password”

  1. I have created the portlet using above mentioned information, but i got the following error:
    java.lang.NoSuchMethodException: com.liferay.util.bridges.mvc.MVCPortlet.updatePassword(javax.portlet.ActionRequest, javax.portlet.ActionResponse).
    How can I fix it..??

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top