1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.action;
16  
17  import com.liferay.portal.kernel.util.HttpUtil;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.kernel.util.PropsKeys;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.theme.ThemeDisplay;
22  import com.liferay.portal.util.PortalUtil;
23  import com.liferay.portal.util.PortletKeys;
24  import com.liferay.portal.util.PrefsPropsUtil;
25  import com.liferay.portal.util.PropsValues;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.PortletURLImpl;
28  
29  import javax.portlet.PortletMode;
30  import javax.portlet.PortletRequest;
31  import javax.portlet.PortletURL;
32  import javax.portlet.WindowState;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  import javax.servlet.http.HttpSession;
37  
38  import org.apache.struts.action.Action;
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="LoginAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Scott Lee
48   */
49  public class LoginAction extends Action {
50  
51      public ActionForward execute(
52              ActionMapping mapping, ActionForm form, HttpServletRequest request,
53              HttpServletResponse response)
54          throws Exception {
55  
56          HttpSession session = request.getSession();
57  
58          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
59              WebKeys.THEME_DISPLAY);
60  
61          if (session.getAttribute("j_username") != null &&
62              session.getAttribute("j_password") != null) {
63  
64              if (PropsValues.PORTAL_JAAS_ENABLE) {
65                  return mapping.findForward("/portal/touch_protected.jsp");
66              }
67              else {
68                  response.sendRedirect(themeDisplay.getPathMain());
69  
70                  return null;
71              }
72          }
73  
74          String redirect = PortalUtil.getCommunityLoginURL(themeDisplay);
75  
76          if (Validator.isNull(redirect)) {
77              redirect = PropsValues.AUTH_LOGIN_URL;
78          }
79  
80          if (Validator.isNull(redirect)) {
81              PortletURL portletURL = new PortletURLImpl(
82                  request, PortletKeys.LOGIN, themeDisplay.getPlid(),
83                  PortletRequest.RENDER_PHASE);
84  
85              portletURL.setWindowState(WindowState.MAXIMIZED);
86              portletURL.setPortletMode(PortletMode.VIEW);
87  
88              portletURL.setParameter("saveLastPath", "0");
89              portletURL.setParameter("struts_action", "/login/login");
90  
91              redirect = portletURL.toString();
92          }
93  
94          if (PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS) {
95              redirect = HttpUtil.protocolize(redirect, true);
96          }
97  
98          String loginRedirect = ParamUtil.getString(request, "redirect");
99  
100         if (Validator.isNotNull(loginRedirect)) {
101             if (PrefsPropsUtil.getBoolean(
102                     themeDisplay.getCompanyId(), PropsKeys.CAS_AUTH_ENABLED,
103                     PropsValues.CAS_AUTH_ENABLED)) {
104 
105                 redirect = loginRedirect;
106             }
107             else {
108                 String loginPortletNamespace = PortalUtil.getPortletNamespace(
109                     PropsValues.AUTH_LOGIN_PORTLET_NAME);
110 
111                 String loginRedirectParameter =
112                     loginPortletNamespace + "redirect";
113 
114                 redirect = HttpUtil.setParameter(
115                     redirect, "p_p_id", PropsValues.AUTH_LOGIN_PORTLET_NAME);
116                 redirect = HttpUtil.setParameter(
117                     redirect, "p_p_lifecycle", "0");
118                 redirect = HttpUtil.setParameter(
119                     redirect, loginRedirectParameter, loginRedirect);
120             }
121         }
122 
123         response.sendRedirect(redirect);
124 
125         return null;
126     }
127 
128 }