Thursday, July 12, 2012

Liferay Hook-4- Custom Action PreLogin and PostLogin


-      Some action classes get called while performing some events in liferay.

-      For Example:

-      Login event :

login.events.pre        :        Called before get Login
-      login.events.pre=com.liferay.portal.events.LoginPreAction


-      login.events.post      :        Called after get Login
login.events.post=com.liferay.portal.events.ChannelLoginPostAction,      com.liferay.portal.events.DefaultLandingPageAction,
com.liferay.portal.events.LoginPostAction


-      Logout event :

logout.events.pre      :        called before get Logout
logout.events.pre=com.liferay.portal.events.LogoutPreAction

logout.events.post    :        called after get Logout
logout.events.post=com.liferay.portal.events.LogoutPostAction,com.liferay.portal.events.DefaultLogoutPageAction,com.liferay.portal.events.SiteMinderLogoutAction

-      Startup Events :
-      Global startup event that runs once when the portal initializes.
global.startup.events=com.liferay.portal.events.GlobalStartupAction


-      Shutdown Events
-      Global shutdown event that runs once when the portal shuts down.
 global.shutdown.events=com.liferay.portal.events.GlobalShutdownAction

-      All this events are specified in portal.properties file.
-      In order to call custom action for this events , specify custom action in     portal.properties file.

-      Steps:
-      We are going to use same hook : myfirsthook-hook :
-      Create a new package com.liferay.custom.action within \hooks\myfirsthook-hook\docroot\WEB-INF\src  folder.
-       
Create four classes each extending com.liferay.portal.kernel.events.Action class and implementing run method


-      CustomPostLoginAction.java


package com.liferay.custom.action;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;

public class CustomPostLoginAction extends Action{
       @Override
public void run(HttpServletRequest httpreq, HttpServletResponse httpres)
                     throws ActionException {
       System.out.println("***********************************");
       System.out.println("Hey Buddy U Post Login Now"+new Date());
       System.out.println("***********************************");
              }
}

-      CustomPreLoginAction.java


package com.liferay.custom.action;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;

public class CustomPreLoginAction extends Action {

       @Override
public void run(HttpServletRequest httpreq, HttpServletResponse httpres)
              throws ActionException {
              System.out.println("***********************************");
              System.out.println("Hey Buddy U Pre Login Now"+new Date());
              System.out.println("***********************************");
       }
}



-      CustomPreLogoutAction.java

package com.liferay.custom.action;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;

public class CustomPreLogoutAction extends Action {
       @Override
public void run(HttpServletRequest httpreq, HttpServletResponse httpres)   throws ActionException {
              System.out.println("***********************************");
              System.out.println("Hey Buddy U Pre Logout Now"+new Date());
              System.out.println("***********************************");
       }
}


-      CustomPostLogoutAction.java


package com.liferay.custom.action;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;

public class CustomPostLogoutAction extends Action {
       @Override
public void run(HttpServletRequest httpreq, HttpServletResponse httpres)
                     throws ActionException {
              System.out.println("***********************************");
              System.out.println("Hey Buddy U Post Logout Now"+new Date());
              System.out.println("***********************************");   
       }
}



-      Modify portal.properties file :


login.events.pre=com.liferay.custom.action.CustomPreLoginAction
login.events.post=com.liferay.custom.action.CustomPostLoginAction
logout.events.pre=com.liferay.custom.action.CustomPreLogoutAction
logout.events.post=com.liferay.custom.action.CustomPostLogoutAction


-      Modify liferay-hook.xml :


<hook>
       <portal-properties>portal.propertiesportal-properties>
hook>

 


-      Deploy Hook


-      On  Login u get custom messages on tomcat console :


 

 
-      On Logout u get custom messages on tomcat console :




-      Some Useful Properties :

-      Forward user to last visited page after Login :
-      auth.forward.by.last.path=true
-      .
-      Forward/Redirect  user to specified page after Login:
-      auth.forward.by.redirect=true
-      .
-      Login friendly url :
-      auth.login.site.url=/login
-      .
-      Login Url where Login portlet is deployed :
-      auth.login.url=/web/guest/home
-      .
-      Default Landing page after login.
 default.landing.page.path=
-      Default Logout page afte logout.
default.logout.page.path=
 ----------------------------------------------------------------------------------------------------


-      Forwarding a user after Login to its public pages  :


package com.liferay.custom.action;
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.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.struts.LastPath;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.kernel.util.WebKeys;

public class CustomPublicUserPagePostLoginAction extends Action{
       @Override
public void run(HttpServletRequest httpsreq, HttpServletResponse httpsres)
                     throws ActionException {
       /* Make sure auth.forward.by.last.path=true in portal.propertie file */
              User user= null;
              String username=null;
              try
                     user = PortalUtil.getUser(httpsreq);
                     username = user.getScreenName();
              } catch (PortalException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (SystemException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
LastPath publiclastPath = new LastPath(StringPool.BLANK, "/web/"+username+"/home");
       /* it gives user public page url :/web/test/home */
       HttpSession session = httpsreq.getSession();
       session.setAttribute(WebKeys.LAST_PATH, publiclastPath);
       }
}


------------------------------------------------------------------------------------------------
 
-      Forward a user to its private pages after Login :

package com.liferay.custom.action;
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.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.struts.LastPath;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.util.PortalUtil;

public class CustomPrivateUserPagePostLoginAction extends Action {
       @Override
public void run(HttpServletRequest httpsreq, HttpServletResponse httpsres)
                     throws ActionException {
       /* Make sure auth.forward.by.last.path=true in portal.propertie file */
              User user= null;
              String username=null;
              try
                     user = PortalUtil.getUser(httpsreq);
                     username = user.getScreenName();
              } catch (PortalException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              } catch (SystemException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
              }
             
LastPath privatelastPath = new LastPath(StringPool.BLANK, "/user/"+username+"/home");
       /* it gives user private page url :/user/test/home */
       HttpSession session = httpsreq.getSession();
       session.setAttribute(WebKeys.LAST_PATH, privatelastPath);
       }
}     

-------------------------------------------------------------------   


-      Note : if there are multiple Hooks Overriding same functionality ,then they are get executed in the order written in portal.properties.


login.events.post=com.liferay.custom.action.CustomPostLoginAction, com.liferay.custom.action.CustomPublicUserPagePostLoginAction ,com.liferay.custom.action.CustomPrivateUserPagePostLoginAction

--------------------------------------------------------------------------------------

3 comments:

Anonymous said...

Hi DarshanKumar
Is it possible to pass object from action class in MVC to CustomPreLogoutAction in hook

Thank You
azar

Bhimanna Halburgi said...

Hi Drasankumar,

Nice explanation, please can get source code for this hook.

Arun Kumar said...
This comment has been removed by a blog administrator.

Junit 5 Architecture

Modules in JUnit 5 : •         junit-jupiter-api: This module defines the API that you need to write your tests. •         ...