
Custom Post-login Redirect
This article needs updating. For more information, please see Wiki - Need Updating
There are articles with content about the same topic. See Customizing the default page after login
Introduction #
The default behavior after login is to redirect the user to his private community (if the user has one). A common requested customization is to change this behavior so that the logged in user will be redirected to a different page (to the guest community, the last page they were at, or some other page based on some custom logic).
Steps#
This can be done by editing 2 files:
ext/ext-ejb/classes/portal-ext.properties:
set
auth.forward.by.last.path=true
This will overwrite the "auth.forward.by.last.path" value in portal.properties which is set to "false" by default. Now, after users login, they will be redirected to the last page they were at, instead of their own private community.
::In v4.3, auth.forward.by.last.path is true by default.
However, if we wanted to customize this redirect even further, we just need to edit one more file.
v4.2#
com.liferay.portal.events.LoginPostAction.java
(the bottom of the .run() method has the following code that has been commented out):
// To manually set a path for the user to forward to, edit // portal.properties and set auth.forward.by.last.path to true. /*Map params = new HashMap(); params.put("p_l_id", new String[] {"PRI.3.1"}); LastPath lastPath = new LastPath("/c", "/portal/layout", params); ses.setAttribute(WebKeys.LAST_PATH, lastPath);*/
If you switch the "auth.forward.by.last.path" variable to "true", then after you login, it will redirect you to the url you were just at before you logged out, which is stored in the variable "lastPath".
v4.3#
com.liferay.portal.events.DefaultLandingPageAction.java
(see comments in the file as well)
if (Validator.isNotNull(path)) { LastPath lastPath = new LastPath( StringPool.BLANK, path, new HashMap()); HttpSession ses = req.getSession(); ses.setAttribute(WebKeys.LAST_PATH, lastPath); } // The commented code shows how you can programmaticaly set the user's // landing page. You can modify this class to utilize a custom alogrithm // for forwarding a user to his landing page. See the references to this // class in portal.properties. /*Map params = new HashMap(); params.put("p_l_id", new String[] {"PRI.1.1"}); LastPath lastPath = new LastPath("/c", "/portal/layout", params); ses.setAttribute(WebKeys.LAST_PATH, lastPath);*/
All#
But here is where the "lastPath" variable is determined. So lets say that you wanted to redirect certain users to a certain page, if you changed that "lastPath" value.. you now control what page the newly logged in user would get redirected to.
Helpful notes in writing custom redirect#
You could get the logged in user info via: PortalUtil.getUserId(req)
v4.2#
By default, the guest community is: PUB.1.1
so to redirect to the public community change the "params.put" line to look like:
params.put("p_l_id", new String[] {"PUB.1.1"});
v4.3#
In v4.3 p_l_id's are long integers, not Strings.
Also, a user's first layout can't be assumed as in 'v4.2'. But we have a convenient method for finding the default Layout's p_l_id:
user.getGroup().getDefaultPrivatePlid()
Best Practices - Extending the Source#
If you just wanted to do this quickly, you can just uncomment the code at the bottom in your LoginPostAction.java file and add your own logic to set the lastPath variable at the end of the run() method, but we would be mixing our changes in with the original source files. This makes our changes hard to keep track of and messy when it comes to updating to newer versions of Liferay. The best way to make these modifications would be to "extend the source".
For this example, we will be creating a new class called "CustomLoginPostAction", this will ONLY contain our additional logic. We will hook in this class so that our logic gets executed after the standard LoginPostAction class gets called. To do this, follow these steps:
Create folder structure#
We will be creating a folder structure that mirrors the folder structure of the portal source code (LoginPostAction.java is in "portal/portal-ejb/src/com/liferay/portal/events/"). By default, the "liferay/portal/events" portion of the folder structure doesnt exist in the EXT environment. In our EXT environment, create the path "ext/ext-ejb/src/com/liferay/portal/events/".
Create a custom LoginPostAction class#
Create a new file in our "ext/ext-ejb/src/com/liferay/portal/events/" folder called "CustomLoginPostAction.java". We will be placing our custom code in the run() method, which will be executed after the run() method of the standard LoginPostAction class.
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.struts.Action; import com.liferay.portal.struts.ActionException; import com.liferay.portal.struts.LastPath; import com.liferay.portal.util.WebKeys; /** * @author Scott Lee* */ public class CustomLoginPostAction extends Action { public void run(HttpServletRequest req, HttpServletResponse res) throws ActionException { try { HttpSession ses = req.getSession(); // To manually set a path for the user to forward to, edit // portal.properties and set auth.forward.by.last.path to true. Map params = new HashMap(); //params.put("p_l_id", new String[] {"PRI.3.1"}); params.put("p_l_id", new String[] {"PUB.1.1"}); LastPath lastPath = new LastPath("/c", "/portal/layout", params); ses.setAttribute(WebKeys.LAST_PATH, lastPath); } catch (Exception e) { throw new ActionException(e); } } }
Overwrite values in portal.properties#
We will overwrite a couple values in "portal.properties" by editing our "portal-ext.properties" file. Add these 2 lines of code:
auth.forward.by.last.path=true login.events.post=com.liferay.portal.events.LoginPostAction,com.liferay.portal.events.CustomLoginPostAction
Deploy Changes#
Make sure tomcat is shutdown, and then run the "ant deploy" from EXT