
Customizing the default page after login
How do I specify which page a newly logged in user gets redirected to?
How do I redirect my users to the page they were just looking at.. after they login? #
All you need to do is set the following value in your portal-ext.properties file:
auth.forward.by.last.path=true
Also make sure that there is no landing page defined. Remove lines with
default.landing.page.path
How do I redirect my users to a specific page (or community).. after they login? #
4.3.x and on #
If your "redirect rule" is very simple.. such as "after login, redirect all users to page '/web/guest/home'", then you just need to add 2 values to your properties file:
auth.forward.by.last.path=true default.landing.page.path=/web/guest/home
If you want to include more complex logic, you will need to modify the DefaultLandingPageAction.java file
DefaultLandingPageAction.java #
In portal.properties, notice the property login.events.post, after LoginPostAction is called, DefaultLandingPageAction is also called.
login.events.post=com.liferay.portal.events.LoginPostAction,com.liferay.portal.events.DefaultLandingPageAction
This class "DefaultLandingPageAction.java" has been created for you here:
/portal-impl/src/com/liferay/portal/events/DefaultLandingPageAction.java
Liferay is setup to read two properties, AUTH_FORWARD_BY_LAST_PATH and DEFAULT_LANDING_PAGE_PATH. To do more complex redirect logic, we are basically setting a property to tell Liferay to send the newly logged in user to the page specified in path, the page he was just looking at. BUT.. before we do this, we override the value of path. Now we have complete control over where to send the user.
For more complex redirect logic we just need to pull the necessary values to get our desired path, then override the path variable.
You can look at this code snippet to help get you started on how to pull the desired information:
ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY); User user = themeDisplay.getUser(); long userId = themeDisplay.getUserId(); List userGroups = UserGroupLocalServiceUtil.getUserUserGroups(userId);
Finally, override this path variable
String path = PropsUtil.get(PropsUtil.DEFAULT_LANDING_PAGE_PATH);
4.2.x and before #
take a look at the bottom of the LoginPostAction.java file. at the bottom of the run method is some code that has been commented out:
portal-ejb\src\com\liferay\portal\events\LoginPostAction.java:
// 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 in.. which is stored in the variable "lastPath".
Soo.. lets say that you wanted to redirect certain users to certain pages. If you changed that "lastPath" value.. you now control what page the newly logged in user would get redirected to. You could get the logged in user info via:
PortalUtil.getUserId(req)
and then query the database based on that userid to get other info.
On a side note, if you were to do this quickly, you can just uncomment the code and add your own logic to set the lastPath variable at the end of the LoginPostAction method, but a cleaner way would be to put these modifications elsewhere.
1. create a new class, and in the run() method, put in your code. This class will have the same structure as LoginPostAction, except that it only contains the new code (the code that was commented out).
2. Then change the value in "portal.properties" by editting your "portal-ext.properties" file so that it will also hit your newly created class.
3. Add your own class to the "login.events.post" line so that it looks something like:
login.events.post=com.liferay.portal.events.LoginPostAction,yourclasshere