1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.Validator;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.PortletKeys;
31  import com.liferay.portal.util.PropsValues;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.PortletURLImpl;
34  
35  import javax.portlet.PortletMode;
36  import javax.portlet.PortletRequest;
37  import javax.portlet.PortletURL;
38  import javax.portlet.WindowState;
39  
40  import javax.servlet.http.HttpServletRequest;
41  import javax.servlet.http.HttpServletResponse;
42  import javax.servlet.http.HttpSession;
43  
44  import org.apache.struts.action.Action;
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionForward;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="LoginAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   * @author Scott Lee
54   *
55   */
56  public class LoginAction extends Action {
57  
58      public ActionForward execute(
59              ActionMapping mapping, ActionForm form, HttpServletRequest request,
60              HttpServletResponse response)
61          throws Exception {
62  
63          HttpSession session = request.getSession();
64  
65          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
66              WebKeys.THEME_DISPLAY);
67  
68          if (session.getAttribute("j_username") != null &&
69              session.getAttribute("j_password") != null) {
70  
71              if (PropsValues.PORTAL_JAAS_ENABLE) {
72                  return mapping.findForward("/portal/touch_protected.jsp");
73              }
74              else {
75                  response.sendRedirect(themeDisplay.getPathMain());
76  
77                  return null;
78              }
79          }
80  
81          String redirect = PortalUtil.getCommunityLoginURL(themeDisplay);
82  
83          if (Validator.isNull(redirect)) {
84              redirect = PropsValues.AUTH_LOGIN_URL;
85          }
86  
87          if (Validator.isNull(redirect)) {
88              PortletURL portletURL = new PortletURLImpl(
89                  request, PortletKeys.LOGIN, themeDisplay.getPlid(),
90                  PortletRequest.RENDER_PHASE);
91  
92              portletURL.setWindowState(WindowState.MAXIMIZED);
93              portletURL.setPortletMode(PortletMode.VIEW);
94  
95              portletURL.setParameter("saveLastPath", "0");
96              portletURL.setParameter("struts_action", "/login/login");
97  
98              redirect = portletURL.toString();
99          }
100 
101         if (PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS) {
102             redirect = HttpUtil.protocolize(redirect, true);
103         }
104 
105         String loginRedirect = ParamUtil.getString(request, "redirect");
106 
107         if (Validator.isNotNull(loginRedirect)) {
108             String loginPortletNamespace = PortalUtil.getPortletNamespace(
109                 PropsValues.AUTH_LOGIN_PORTLET_NAME);
110 
111             String loginRedirectParameter = loginPortletNamespace + "redirect";
112 
113             redirect = HttpUtil.setParameter(
114                 redirect, "p_p_id", PropsValues.AUTH_LOGIN_PORTLET_NAME);
115             redirect = HttpUtil.setParameter(redirect, "p_p_lifecycle", "0");
116             redirect = HttpUtil.setParameter(
117                 redirect, loginRedirectParameter, loginRedirect);
118         }
119 
120         response.sendRedirect(redirect);
121 
122         return null;
123     }
124 
125 }