Custom redirect after login liferay

I assume you installed liferay portal v5.2.3 Bundled with Tomcat 5.5 by this way http://blog.csdn.net/wxhawk/archive/2010/11/15/6009959.aspx. 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 );
         }
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值