I assume you installed liferay portal v5.2.3 Bundled with Tomcat 5.5 by this way http://wangxiangblog.blogspot.com/2010/04/install-liferay-framework-and-integrate.html. Our target is to redirect to a specified community page after a user login. Of course the user is a member of the specified community.
Another target is not to change liferay source code, just to override its default behavior.
go to http://sourceforge.net/projects/lportal/ and download liferay-portal-src-5.2.3.zip,
unzip the downloaded source code and import to eclipse to create a new class CustomLoginPostAction in portal-impl sub-project in liferay, the code is here
package com.liferay.portal.events;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.Group;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.struts.LastPath;
import com.liferay.portal.util.WebKeys;
import java.util.List;
public class CustomLoginPostAction extends Action
{
private static Log _log = LogFactoryUtil.getLog(CustomLoginPostAction.class);
public void run( HttpServletRequest req, HttpServletResponse res ) throws ActionException
{
try
{
HttpSession ses = req.getSession();
Map params = new HashMap();
Long id = ( Long ) ses.getAttribute( "USER_ID" );
long userId = id.longValue();
long groupId = 0;
String frindlyUrl = null;
List groups = GroupLocalServiceUtil.getUserGroups( userId );
for ( Group aGroup : groups )
{
if( aGroup.isCommunity() && !aGroup.getName().equals( "Guest" ) )
{
groupId = aGroup.getGroupId();
frindlyUrl= aGroup.getFriendlyURL();
}
}
LastPath lastPath = null;
if( groupId <= 0 )
{
lastPath = new LastPath( "/c", "/portal/layout", params );
}
else
{
lastPath = new LastPath( "", "/web"+frindlyUrl, params );
}
ses.setAttribute( WebKeys.LAST_PATH, lastPath );
System.out.println("User logined in , redirect to page:" + lastPath);
_log.info( "User logined in , redirect to page:" + lastPath );
} catch ( Exception e )
{
throw new ActionException( e );
}
}
}
copy CustomLoginPostAction.class to C:\liferay-portal-5.2.3\tomcat-5.5.27\webapps\ROOT\WEB-INF\classes\com\liferay\portal\events
if you cannot find folder com\liferay\portal\events under C:\liferay-portal-5.2.3\tomcat-5.5.27\webapps\ROOT\WEB-INF\classes, just create it manually.
add the two config items in port-ext.properties which location is C:\liferay-portal-5.2.3\tomcat-5.5.27\webapps\ROOT\WEB-INF\classes
auth.forward.by.last.path=true
login.events.post=com.liferay.portal.events.DefaultLandingPageAction,com.liferay.portal.events.CustomLoginPostAction
This setting will overwrite the value of "auth.forward.by.last.path" in portal.properties which default value is "false".
Now, after users login, they will be redirected to the last page they were at, instead of their own private community.
The second item means CustomLoginPostAction will replace DefaultLandingPageAction which is the default redirect processor.
---- how to test ---
1. create a community A and community B and design public page for these two communities.
2. create a new account usera for community A and account userb for community B.
3. if you login as usera, system will redirect to community A public page, not system welcome page instead.
if you login as userb, system will redirect to community B public page as well.
Sombody suggests to add above class file into portal-impl.jar, it is not a good practise because you change the original liferay.
if you reinstall liferay, you will lose the new redirect logic.
if want to redirect to user's personal page, CustomLoginPostAction should be following:
package com.liferay.portal.events;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.Group;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.struts.LastPath;
import com.liferay.portal.util.WebKeys;
import java.util.List;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;
public class CustomLoginPostAction extends Action
{
private static Log _log = LogFactoryUtil.getLog(CustomLoginPostAction.class);
public void run( HttpServletRequest req, HttpServletResponse res ) throws ActionException
{
try
{
HttpSession ses = req.getSession();
Map params = new HashMap();
// Long id = ( Long ) ses.getAttribute( "USER_ID" );
// long userId = id.longValue();
User loginUser = UserLocalServiceUtil.getUser(com.liferay.portal.util.PortalUtil.getUserId(req));
LastPath lastPath = null;
if( loginUser == null )
{
lastPath = new LastPath( "/c", "/portal/layout", params );
}
else
{
lastPath = new LastPath( "", "/web/"+loginUser.getScreenName(), params );
}
ses.setAttribute( WebKeys.LAST_PATH, lastPath );
System.out.println("User logined in , redirect to page >>>>>>> " + lastPath);
_log.info( "User logined in , redirect to page:" + lastPath );
} catch ( Exception e )
{
throw new ActionException( e );
}
}
}
Thanks a lot Xiang Wang ....your custom redirect is work well and solved my problem....once again thanks.
ReplyDeleteGreat work!
ReplyDeleteHow would i apply this in Liferay 6.0.5? Any idea please? thanks
ReplyDeleteHi i am having a problem regarding liferay login redirection. I am showing an image from many images regardless user is logged in or not and giving a comment area. i want that if i have a login button near textarea and want to send the user at login page after pressing button and after login i want user to be redirected on same page with same image. I want to know the approach and will this code help me a bit.
ReplyDelete