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.portlet.login.action;
24  
25  import com.liferay.portal.CookieNotSupportedException;
26  import com.liferay.portal.NoSuchUserException;
27  import com.liferay.portal.PasswordExpiredException;
28  import com.liferay.portal.UserEmailAddressException;
29  import com.liferay.portal.UserIdException;
30  import com.liferay.portal.UserLockoutException;
31  import com.liferay.portal.UserPasswordException;
32  import com.liferay.portal.UserScreenNameException;
33  import com.liferay.portal.kernel.servlet.SessionErrors;
34  import com.liferay.portal.kernel.util.ParamUtil;
35  import com.liferay.portal.kernel.util.Validator;
36  import com.liferay.portal.security.auth.AuthException;
37  import com.liferay.portal.struts.PortletAction;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.util.PortalUtil;
40  import com.liferay.portal.util.PropsValues;
41  import com.liferay.portal.util.WebKeys;
42  import com.liferay.portlet.PortletPreferencesFactoryUtil;
43  import com.liferay.portlet.login.util.LoginUtil;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.PortletPreferences;
49  import javax.portlet.RenderRequest;
50  import javax.portlet.RenderResponse;
51  
52  import javax.servlet.http.HttpServletRequest;
53  import javax.servlet.http.HttpServletResponse;
54  
55  import org.apache.struts.action.ActionForm;
56  import org.apache.struts.action.ActionForward;
57  import org.apache.struts.action.ActionMapping;
58  
59  /**
60   * <a href="LoginAction.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Brian Wing Shun Chan
63   */
64  public class LoginAction extends PortletAction {
65  
66      public void processAction(
67              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
68              ActionRequest actionRequest, ActionResponse actionResponse)
69          throws Exception {
70  
71          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
72              WebKeys.THEME_DISPLAY);
73  
74          /*if (actionRequest.getRemoteUser() != null) {
75              actionResponse.sendRedirect(themeDisplay.getPathMain());
76  
77              return;
78          }*/
79  
80          try {
81              PortletPreferences preferences =
82                  PortletPreferencesFactoryUtil.getPortletSetup(actionRequest);
83  
84              login(themeDisplay, actionRequest, actionResponse, preferences);
85          }
86          catch (Exception e) {
87              if (e instanceof AuthException) {
88                  Throwable cause = e.getCause();
89  
90                  if (cause instanceof PasswordExpiredException ||
91                      cause instanceof UserLockoutException) {
92  
93                      SessionErrors.add(
94                          actionRequest, cause.getClass().getName());
95                  }
96                  else {
97                      SessionErrors.add(actionRequest, e.getClass().getName());
98                  }
99              }
100             else if (e instanceof CookieNotSupportedException ||
101                      e instanceof NoSuchUserException ||
102                      e instanceof PasswordExpiredException ||
103                      e instanceof UserEmailAddressException ||
104                      e instanceof UserIdException ||
105                      e instanceof UserLockoutException ||
106                      e instanceof UserPasswordException ||
107                      e instanceof UserScreenNameException) {
108 
109                 SessionErrors.add(actionRequest, e.getClass().getName());
110             }
111             else {
112                 PortalUtil.sendError(e, actionRequest, actionResponse);
113             }
114         }
115     }
116 
117     public ActionForward render(
118             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
119             RenderRequest renderRequest, RenderResponse renderResponse)
120         throws Exception {
121 
122         return mapping.findForward("portlet.login.login");
123     }
124 
125     protected boolean isCheckMethodOnProcessAction() {
126         return _CHECK_METHOD_ON_PROCESS_ACTION;
127     }
128 
129     protected void login(
130             ThemeDisplay themeDisplay, ActionRequest actionRequest,
131             ActionResponse actionResponse, PortletPreferences preferences)
132         throws Exception {
133 
134         HttpServletRequest request = PortalUtil.getHttpServletRequest(
135             actionRequest);
136         HttpServletResponse response = PortalUtil.getHttpServletResponse(
137             actionResponse);
138 
139         String login = ParamUtil.getString(actionRequest, "login");
140         String password = ParamUtil.getString(actionRequest, "password");
141         boolean rememberMe = ParamUtil.getBoolean(actionRequest, "rememberMe");
142 
143         String authType = preferences.getValue("authType", null);
144 
145         LoginUtil.login(
146             request, response, login, password, rememberMe, authType);
147 
148         if (PropsValues.PORTAL_JAAS_ENABLE) {
149             actionResponse.sendRedirect(
150                 themeDisplay.getPathMain() + "/portal/protected");
151         }
152         else {
153             String redirect = ParamUtil.getString(actionRequest, "redirect");
154 
155             if (Validator.isNotNull(redirect)) {
156                 redirect = PortalUtil.escapeRedirect(redirect);
157 
158                 actionResponse.sendRedirect(redirect);
159             }
160             else {
161                 actionResponse.sendRedirect(themeDisplay.getPathMain());
162             }
163         }
164     }
165 
166     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
167 
168 }