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.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   */
65  public class LoginAction extends PortletAction {
66  
67      public void processAction(
68              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
69              ActionRequest actionRequest, ActionResponse actionResponse)
70          throws Exception {
71  
72          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
73              WebKeys.THEME_DISPLAY);
74  
75          /*if (actionRequest.getRemoteUser() != null) {
76              actionResponse.sendRedirect(themeDisplay.getPathMain());
77  
78              return;
79          }*/
80  
81          try {
82              PortletPreferences preferences =
83                  PortletPreferencesFactoryUtil.getPortletSetup(actionRequest);
84  
85              login(themeDisplay, actionRequest, actionResponse, preferences);
86          }
87          catch (Exception e) {
88              if (e instanceof AuthException) {
89                  Throwable cause = e.getCause();
90  
91                  if (cause instanceof PasswordExpiredException ||
92                      cause instanceof UserLockoutException) {
93  
94                      SessionErrors.add(
95                          actionRequest, cause.getClass().getName());
96                  }
97                  else {
98                      SessionErrors.add(actionRequest, e.getClass().getName());
99                  }
100             }
101             else if (e instanceof CookieNotSupportedException ||
102                      e instanceof NoSuchUserException ||
103                      e instanceof PasswordExpiredException ||
104                      e instanceof UserEmailAddressException ||
105                      e instanceof UserIdException ||
106                      e instanceof UserLockoutException ||
107                      e instanceof UserPasswordException ||
108                      e instanceof UserScreenNameException) {
109 
110                 SessionErrors.add(actionRequest, e.getClass().getName());
111             }
112             else {
113                 PortalUtil.sendError(e, actionRequest, actionResponse);
114             }
115         }
116     }
117 
118     public ActionForward render(
119             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
120             RenderRequest renderRequest, RenderResponse renderResponse)
121         throws Exception {
122 
123         return mapping.findForward("portlet.login.login");
124     }
125 
126     protected boolean isCheckMethodOnProcessAction() {
127         return _CHECK_METHOD_ON_PROCESS_ACTION;
128     }
129 
130     protected void login(
131             ThemeDisplay themeDisplay, ActionRequest actionRequest,
132             ActionResponse actionResponse, PortletPreferences preferences)
133         throws Exception {
134 
135         HttpServletRequest request = PortalUtil.getHttpServletRequest(
136             actionRequest);
137         HttpServletResponse response = PortalUtil.getHttpServletResponse(
138             actionResponse);
139 
140         String login = ParamUtil.getString(actionRequest, "login");
141         String password = ParamUtil.getString(actionRequest, "password");
142         boolean rememberMe = ParamUtil.getBoolean(actionRequest, "rememberMe");
143 
144         String authType = preferences.getValue("authType", null);
145 
146         LoginUtil.login(
147             request, response, login, password, rememberMe, authType);
148 
149         if (PropsValues.PORTAL_JAAS_ENABLE) {
150             actionResponse.sendRedirect(
151                 themeDisplay.getPathMain() + "/portal/protected");
152         }
153         else {
154             String redirect = ParamUtil.getString(actionRequest, "redirect");
155 
156             if (Validator.isNotNull(redirect)) {
157                 actionResponse.sendRedirect(redirect);
158             }
159             else {
160                 actionResponse.sendRedirect(themeDisplay.getPathMain());
161             }
162         }
163     }
164 
165     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
166 
167 }