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.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  public class LoginAction extends Action {
56  
57      public ActionForward execute(
58              ActionMapping mapping, ActionForm form, HttpServletRequest request,
59              HttpServletResponse response)
60          throws Exception {
61  
62          HttpSession session = request.getSession();
63  
64          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
65              WebKeys.THEME_DISPLAY);
66  
67          if (session.getAttribute("j_username") != null &&
68              session.getAttribute("j_password") != null) {
69  
70              if (PropsValues.PORTAL_JAAS_ENABLE) {
71                  return mapping.findForward("/portal/touch_protected.jsp");
72              }
73              else {
74                  response.sendRedirect(themeDisplay.getPathMain());
75  
76                  return null;
77              }
78          }
79  
80          String redirect = PortalUtil.getCommunityLoginURL(themeDisplay);
81  
82          if (Validator.isNull(redirect)) {
83              redirect = PropsValues.AUTH_LOGIN_URL;
84          }
85  
86          if (Validator.isNull(redirect)) {
87              PortletURL portletURL = new PortletURLImpl(
88                  request, PortletKeys.LOGIN, themeDisplay.getPlid(),
89                  PortletRequest.RENDER_PHASE);
90  
91              portletURL.setWindowState(WindowState.MAXIMIZED);
92              portletURL.setPortletMode(PortletMode.VIEW);
93  
94              portletURL.setParameter("saveLastPath", "0");
95              portletURL.setParameter("struts_action", "/login/login");
96  
97              redirect = portletURL.toString();
98          }
99  
100         if (PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS) {
101             redirect = HttpUtil.protocolize(redirect, true);
102         }
103 
104         String loginRedirect = ParamUtil.getString(request, "redirect");
105 
106         if (Validator.isNotNull(loginRedirect)) {
107             String loginPortletNamespace = PortalUtil.getPortletNamespace(
108                 PropsValues.AUTH_LOGIN_PORTLET_NAME);
109 
110             String loginRedirectParameter = loginPortletNamespace + "redirect";
111 
112             redirect = HttpUtil.setParameter(
113                 redirect, "p_p_id", PropsValues.AUTH_LOGIN_PORTLET_NAME);
114             redirect = HttpUtil.setParameter(redirect, "p_p_lifecycle", "0");
115             redirect = HttpUtil.setParameter(
116                 redirect, loginRedirectParameter, loginRedirect);
117         }
118 
119         response.sendRedirect(redirect);
120 
121         return null;
122     }
123 
124 }