Create Custom Login Portlet In Liferay
*Liferay 6.2 version
1. Before start portlet creation add below property in portal-ext.properties file,
session.enable.phishing.protection=false
then start/restart server.
2. Now create a custom portlet name called "login" or "custom login" or "xyz" as per your requirement naming convention.
3. Add below lines in liferay-portlet.xml file
<instanceable>false</instanceable>
<private-session-attributes>false</private-session-attributes>
4. In view.jsp paste below code
<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="com.liferay.portal.kernel.util.Validator"%>
<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@page import="javax.portlet.ActionRequest"%>
<%@page import="javax.portlet.PortletURL"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui"%>
<portlet:defineObjects />
<%
String passwordNotMatched = Validator.isNotNull((String)request.getAttribute("PASSWORD_NOT_MATCHED")) ? (String)request.getAttribute("PASSWORD_NOT_MATCHED") : StringPool.BLANK;
String emailNotExist = Validator.isNotNull((String)request.getAttribute("EMAIL_NOT_EXIST")) ? (String)request.getAttribute("EMAIL_NOT_EXIST"): StringPool.BLANK;
PortletURL loginURL = renderResponse.createActionURL();
loginURL.setParameter(ActionRequest.ACTION_NAME, "loginUser");
%>
<%
if(Validator.isNotNull(emailNotExist)){%>
<div style="color:red;"><%=emailNotExist%></div>
<%}else if(Validator.isNotNull(passwordNotMatched)){%>
<div style="color:red;"><%=passwordNotMatched%></div>
<%}%>
<aui:form method="post" action="<%=loginURL.toString() %>" id="aui-login-form">
<div class="col-lg-4 col-a-vik col-b-vik">
<span style="color:black;font-size: 10.5px;" id="label-email">Email Id</span>
<div class="input-group">
<input class="form-control" type="text" placeholder="Email Id" name="<portlet:namespace/>emailId" id="<portlet:namespace/>emailId" style="margin-bottom: 0px;">
</div>
</div>
<div class="col-lg-4 col-a-vik col-b-vik">
<span style="color:black;font-size: 10.5px;" id="label-pwd">Password</span>
<div class="input-group">
<input class="form-control" type="password" placeholder="password" id="<portlet:namespace/>password" name="<portlet:namespace/>password" style="margin-bottom: 0px;">
</div>
</div>
<div class="col-lg-4 col-a-vik col-b-vik">
<aui:button type="submit" id="btn-submit" style="background:#EE9400" value="Log In"/>
</div>
</aui:form>
5. In portlet controller/action class paste below code
package com.test;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.CompanyConstants;
import com.liferay.portal.model.User;
import com.liferay.portal.service.PasswordTrackerLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;
import java.io.IOException;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Portlet implementation class Login
*/
public class Login extends MVCPortlet {
public void loginUser(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException{
System.out.println("login action called");
String email = ParamUtil.getString(actionRequest, "emailId").trim();
String password = ParamUtil.getString(actionRequest, "password").trim();
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
User userDetail = null;
try {
userDetail = UserLocalServiceUtil.fetchUserByEmailAddress(themeDisplay.getCompanyId(), email);
if(Validator.isNull(userDetail)){
actionRequest.setAttribute("EMAIL_NOT_EXIST", "Account not exist with email");
}else{
boolean passwordMatched = PasswordTrackerLocalServiceUtil.isSameAsCurrentPassword(userDetail.getUserId(), password);
if(!passwordMatched){
actionRequest.setAttribute("PASSWORD_NOT_MATCHED", "Please Enter Correct Password");
}else{
// if user exist with provide email and given password matched with user database password
ClassLoader pcl = PortalClassLoaderUtil.getClassLoader();
Class lClass = pcl.loadClass("com.liferay.portlet.login.util.LoginUtil");
java.lang.reflect.Method method = lClass.getDeclaredMethod("login", HttpServletRequest.class, HttpServletResponse.class, String.class, String.class, Boolean.TYPE, String.class);
method.invoke(null, request, response, email, password, false, CompanyConstants.AUTH_TYPE_EA);
//redirection after login...
String screenName = userDetail.getScreenName();
String portalURL = PortalUtil.getPortalURL(actionRequest);
if(PortalUtil.isOmniadmin(userDetail.getUserId()))
{
System.out.println("admin logged in, redirecting to admin specified private page");
actionResponse.sendRedirect(portalURL + "/user/"+ screenName+"/adminuser");
} else {
System.out.println("normal user logged in, redirecting to private page specified for normal user");
actionResponse.sendRedirect(portalURL + "/user/" + screenName +"/normaluser");
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
*change pages name given above for user redirection after login as per your requirement.
Now deploy this portlet , after deploying add this portlet on any page try to login using admin /normal user credential .
