03 October 2015

login with screen name and email address

http://www.liferay.com/web/lauri.hiltunen/blog/-/blogs/tip-of-the-day-login-magic-by-overriding-struts-actions


package com.sample.login;

import javax.portlet.ActionRequest;
import javax.portlet.filter.ActionRequestWrapper;

import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.util.PortalUtil;

public class LoginParamWrapper extends ActionRequestWrapper {


@Override
public String getParameter(String name) {
String value=super.getParameter(name);
if(LoginParamKeys.LOGIN.equals(name) && Validator.isEmailAddress(value))
{
ActionRequest actionRequest=this.getRequest(); 
long companyId=PortalUtil.getCompanyId(actionRequest);
value=LoginNameResolver.resolveScreenNameFromEmailAddress(value, companyId);
}
// TODO Auto-generated method stub
return value;
}

public LoginParamWrapper(ActionRequest request) {
super(request);
// TODO Auto-generated constructor stub
}

}

--
package com.sample.login;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;

public class LoginNameResolver {
public static String resolveScreenNameFromEmailAddress(String email,long companyId)
{
String screenName="";
try {
User user=UserLocalServiceUtil.getUserByEmailAddress(companyId, email);
screenName=user.getScreenName();
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 return screenName;
}

}
---

package com.sample.login;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import com.liferay.portal.kernel.struts.BaseStrutsPortletAction;
import com.liferay.portal.kernel.struts.StrutsPortletAction;

public class LoginAction extends BaseStrutsPortletAction {

@Override
public void processAction(StrutsPortletAction originalStrutsPortletAction,
PortletConfig portletConfig, ActionRequest actionRequest,
ActionResponse actionResponse) throws Exception {
// TODO Auto-generated method stub
originalStrutsPortletAction.processAction(originalStrutsPortletAction, portletConfig, new LoginParamWrapper(actionRequest), actionResponse);
}

@Override
public String render(StrutsPortletAction originalStrutsPortletAction,
PortletConfig portletConfig, RenderRequest renderRequest,
RenderResponse renderResponse) throws Exception {
// TODO Auto-generated method stub
return originalStrutsPortletAction.render(originalStrutsPortletAction, portletConfig, renderRequest,
renderResponse);
}

@Override
public void serveResource(StrutsPortletAction originalStrutsPortletAction,
PortletConfig portletConfig, ResourceRequest resourceRequest,
ResourceResponse resourceResponse) throws Exception {
// TODO Auto-generated method stub
originalStrutsPortletAction.serveResource(originalStrutsPortletAction, portletConfig,
resourceRequest, resourceResponse);
}

}
---
package com.sample.login;

public class LoginParamKeys {
public static String LOGIN="login";

}

No comments: