1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.StringUtil;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.PortalUtil;
24  import com.liferay.portal.util.PortletKeys;
25  import com.liferay.portal.util.PrefsPropsUtil;
26  import com.liferay.portal.util.PropsValues;
27  import com.liferay.portal.util.WebKeys;
28  import com.liferay.portlet.PortletURLImpl;
29  
30  import javax.portlet.PortletMode;
31  import javax.portlet.PortletRequest;
32  import javax.portlet.PortletURL;
33  import javax.portlet.WindowState;
34  
35  import javax.servlet.http.HttpServletRequest;
36  import javax.servlet.http.HttpServletResponse;
37  import javax.servlet.http.HttpSession;
38  
39  import org.apache.struts.action.Action;
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="LoginAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Scott Lee
49   */
50  public class LoginAction extends Action {
51  
52      public ActionForward execute(
53              ActionMapping mapping, ActionForm form, HttpServletRequest request,
54              HttpServletResponse response)
55          throws Exception {
56  
57          HttpSession session = request.getSession();
58  
59          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
60              WebKeys.THEME_DISPLAY);
61  
62          if ((session.getAttribute("j_username") != null) &&
63              (session.getAttribute("j_password") != null)) {
64  
65              if (PropsValues.PORTAL_JAAS_ENABLE) {
66                  return mapping.findForward("/portal/touch_protected.jsp");
67              }
68              else {
69                  response.sendRedirect(themeDisplay.getPathMain());
70  
71                  return null;
72              }
73          }
74  
75          String redirect = PortalUtil.getCommunityLoginURL(themeDisplay);
76  
77          if (Validator.isNull(redirect)) {
78              redirect = PropsValues.AUTH_LOGIN_URL;
79          }
80  
81          if (Validator.isNull(redirect)) {
82              PortletURL portletURL = new PortletURLImpl(
83                  request, PortletKeys.LOGIN, themeDisplay.getPlid(),
84                  PortletRequest.RENDER_PHASE);
85  
86              portletURL.setWindowState(WindowState.MAXIMIZED);
87              portletURL.setPortletMode(PortletMode.VIEW);
88  
89              portletURL.setParameter("saveLastPath", "0");
90              portletURL.setParameter("struts_action", "/login/login");
91  
92              redirect = portletURL.toString();
93          }
94  
95          if (PropsValues.COMPANY_SECURITY_AUTH_REQUIRES_HTTPS) {
96              String portalURL = PortalUtil.getPortalURL(request);
97  
98              String portalURLSecure = PortalUtil.getPortalURL(request, true);
99  
100             if (!portalURL.equals(portalURLSecure)) {
101                 redirect = StringUtil.replaceFirst(
102                     redirect, portalURL, portalURLSecure);
103             }
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 }