1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.action;
24  
25  import com.liferay.portal.kernel.util.HttpUtil;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.PropsKeys;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.portal.util.PrefsPropsUtil;
33  import com.liferay.portal.util.PropsValues;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.PortletURLImpl;
36  
37  import javax.portlet.PortletMode;
38  import javax.portlet.PortletRequest;
39  import javax.portlet.PortletURL;
40  import javax.portlet.WindowState;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  import javax.servlet.http.HttpSession;
45  
46  import org.apache.struts.action.Action;
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="LoginAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   * @author Scott Lee
56   */
57  public class LoginAction extends Action {
58  
59      public ActionForward execute(
60              ActionMapping mapping, ActionForm form, HttpServletRequest request,
61              HttpServletResponse response)
62          throws Exception {
63  
64          HttpSession session = request.getSession();
65  
66          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
67              WebKeys.THEME_DISPLAY);
68  
69          if (session.getAttribute("j_username") != null &&
70              session.getAttribute("j_password") != null) {
71  
72              if (PropsValues.PORTAL_JAAS_ENABLE) {
73                  return mapping.findForward("/portal/touch_protected.jsp");
74              }
75              else {
76                  response.sendRedirect(themeDisplay.getPathMain());
77  
78                  return null;
79              }
80          }
81  
82          String redirect = PortalUtil.getCommunityLoginURL(themeDisplay);
83  
84          if (Validator.isNull(redirect)) {
85              redirect = PropsValues.AUTH_LOGIN_URL;
86          }
87  
88          if (Validator.isNull(redirect)) {
89              PortletURL portletURL = new PortletURLImpl(
90                  request, PortletKeys.LOGIN, themeDisplay.getPlid(),
91                  PortletRequest.RENDER_PHASE);
92  
93              portletURL.setWindowState(WindowState.MAXIMIZED);
94              portletURL.setPortletMode(PortletMode.VIEW);
95  
96              portletURL.setParameter("saveLastPath", "0");
97              portletURL.setParameter("struts_action", "/login/login");
98  
99              redirect = portletURL.toString();
100         }
101 
102         if (PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS) {
103             redirect = HttpUtil.protocolize(redirect, true);
104         }
105 
106         String loginRedirect = ParamUtil.getString(request, "redirect");
107 
108         if (Validator.isNotNull(loginRedirect)) {
109             if (PrefsPropsUtil.getBoolean(
110                     themeDisplay.getCompanyId(), PropsKeys.CAS_AUTH_ENABLED,
111                     PropsValues.CAS_AUTH_ENABLED)) {
112 
113                 redirect = loginRedirect;
114             }
115             else {
116                 String loginPortletNamespace = PortalUtil.getPortletNamespace(
117                     PropsValues.AUTH_LOGIN_PORTLET_NAME);
118 
119                 String loginRedirectParameter =
120                     loginPortletNamespace + "redirect";
121 
122                 redirect = HttpUtil.setParameter(
123                     redirect, "p_p_id", PropsValues.AUTH_LOGIN_PORTLET_NAME);
124                 redirect = HttpUtil.setParameter(
125                     redirect, "p_p_lifecycle", "0");
126                 redirect = HttpUtil.setParameter(
127                     redirect, loginRedirectParameter, loginRedirect);
128             }
129         }
130 
131         response.sendRedirect(redirect);
132 
133         return null;
134     }
135 
136 }