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