Friday, November 29, 2013

Liferay : Custom Login Landing page

Hi All,

Here i am going to explain how we have to redirect to our community page once you login into liferay. I am continue with the post thinking you had already configure eclipse for development. You have to know little bit about the hooks concept in liferay.

Hooks can over right many things like login, logout etc.

I am using login.events.post. which will fire once a member try to login into a liferay application.
We have to extend Action to our code as shown below:

public class CustomLoginPostAction extends Action {
   
    Logger logger= Logger.getLogger(getClass());

    public CustomLoginPostAction() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void run(HttpServletRequest request, HttpServletResponse response)
            throws ActionException {
            doRun(request, response);
        }
        catch (Exception e) {
          throw new ActionException(e);
        }
    }

    protected void doRun(HttpServletRequest request, HttpServletResponse response)
            throws SystemException, PortalException, Exception
          {
            long companyId = PortalUtil.getCompanyId(request);

            String path = PrefsPropsUtil.getString(companyId, "default.landing.page.path");

            if (logger.isInfoEnabled())
            {
                logger.info("default.landing.page.path=" + path);
            }

              path = getCustomLandingPage(request);

            HttpSession session = request.getSession();
            session.setAttribute("LAST_PATH", new LastPath("", path));
          }

          private String getCustomLandingPage(HttpServletRequest request)
            throws PortalException, SystemException, Exception
          {
            String customLandingPagePath = "";
            long companyId = PortalUtil.getCompanyId(request);

            String landingPageTypeSelection = PrefsPropsUtil.getString(companyId, "custom.landing.page.type");

            if ((landingPageTypeSelection != null) && (!landingPageTypeSelection.trim().isEmpty()))
            {
              LandingPageType landingPageType = LandingPageTypeFactory.getLandingPageTypeInstance(landingPageTypeSelection);

              customLandingPagePath = landingPageType.getLandingPagePath(request);
            }

            return customLandingPagePath;
          }
}

try the above code in your hook it will land to your respective page.