A common trend is to allow users to sign into their accounts using either their email address or their username. A client requested that its users be allowed to do this using Liferay 6.0.5. Users are teachers who rarely use the site over the summer and are likely to forget their username (called screen name in Liferay) and/or email. This was a fairly easy customization.
DISCLAIMER: This approach was successful for Liferay version 6.0.5. No promises can be made about other versions.
If you have not already set up an ext plugin for your Liferay instance, follow the instructions here.
We will be overriding the portlet login struts action. Open struts-config-ext.xml in your ext plugin and add the new action node:
[xml]
<?xml version="1.0"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<action-mappings>
<!– Your other mappings go here –>
<!– Login: overridden so that users can choose to login with either their screen name OR their email address –>
<action path="/login/login" type="com.sample.MyLoginAction">
<forward name="portlet.login.login" path="portlet.login.login" />
</action>
</action-mappings>
</struts-config>
[/xml]
Create your new class, MyLoginAction, under my-ext/docroot/WEB-INF/ext-inpl/src/com/sample. Your class should extend the portlet LoginAction (com.liferay.portlet.login.action.LoginAction), not the portal LoginAction. You’ll copy the login method from LoginAction. Add the logic to check if the user’s login looks like an email address or not (printouts added to test – remove them when you’re finished!):
[java]
package com.sample;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletPreferences;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.model.Company;
import com.liferay.portal.model.CompanyConstants;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.PropsValues;
import com.liferay.portlet.login.action.LoginAction;
import com.liferay.portlet.login.util.LoginUtil;
public class MyLoginAction extends LoginAction {
protected void login(
ThemeDisplay themeDisplay, ActionRequest actionRequest,
ActionResponse actionResponse, PortletPreferences preferences)
throws Exception {
//ORIGINAL
HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);
HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);
String login = ParamUtil.getString(actionRequest, "login");
String password = ParamUtil.getString(actionRequest, "password");
boolean rememberMe = ParamUtil.getBoolean(actionRequest, "rememberMe");
String authType = preferences.getValue("authType", null);
//START CUSTOM
if (Validator.isNull(authType)) {
Company company = PortalUtil.getCompany(actionRequest);
authType = company.getAuthType();
}
System.out.println("login: " + login);
System.out.println("authType BEFORE checking login format: " + authType);
if ( login.indexOf("@") != -1 && login.indexOf(".") != -1 ) {
//If the login looks like an email, use the email authType constant.
authType = CompanyConstants.AUTH_TYPE_EA;
}
else {
//If it doesn’t look like an email, use the screen name authType constant.
authType = CompanyConstants.AUTH_TYPE_SN;
}
System.out.println("authType AFTER checking login format: " + authType);
//END CUSTOM
//ORIGINAL
LoginUtil.login(request, response, login, password, rememberMe, authType);
if (PropsValues.PORTAL_JAAS_ENABLE) {
actionResponse.sendRedirect(
themeDisplay.getPathMain() + "/portal/protected");
}
else {
String redirect = ParamUtil.getString(actionRequest, "redirect");
if (Validator.isNotNull(redirect)) {
redirect = PortalUtil.escapeRedirect(redirect);
actionResponse.sendRedirect(redirect);
}
else {
actionResponse.sendRedirect(themeDisplay.getPathMain());
}
}
}
}
[/java]
Now you’ll need to customize your login.jsp with the proper label.
- In your ext plugin, create the following folder structure: my-extdocrootWEB-INFext-webdocroothtmlportletlogin.
- Make sure you have the Liferay source downloaded for your specific version. Go to {liferay-portal-src-x.x.x}portal-webdocroothtmlportletlogin and copy login.jsp into your ext plugin.
- Around line 84, remove the logic that checks authType and change the label of the input to the text of your choice. (I also have a custom hook, so I created a new property for the label called login-screen-name-or-email in a Language_en.properties file. You can put straight text in here I believe.)
[sourcecode highlight=”2″]
<aui:fieldset>
<aui:input label="login-screen-name-or-email" name="login" type="text" value="<%= login %>" />
…
</aui:fieldset>
[/sourcecode]
That’s it! Have fun deploying it! [sarcasm font – it’s a pain in the ass] That will be a future post: How to Deploy Liferay Ext Plugins Successfully!
Christy