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.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   */
68  public class ViewAction extends PortletAction {
69  
70      public void processAction(
71              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
72              ActionRequest actionRequest, ActionResponse actionResponse)
73          throws Exception {
74  
75          String cmd = actionRequest.getParameter(Constants.CMD);
76  
77          if (cmd.equals("forgot-password")) {
78              HttpServletRequest request = PortalUtil.getHttpServletRequest(
79                  actionRequest);
80  
81              try {
82                  if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
83                      CaptchaUtil.check(actionRequest);
84                  }
85  
86                  LoginAction.sendPassword(request);
87  
88                  SessionMessages.add(request, "request_processed");
89              }
90              catch (Exception e) {
91                  if (e instanceof CaptchaTextException ||
92                      e instanceof NoSuchUserException ||
93                      e instanceof SendPasswordException ||
94                      e instanceof UserEmailAddressException) {
95  
96                      SessionErrors.add(request, e.getClass().getName());
97                  }
98                  else {
99                      PortalUtil.sendError(e, actionRequest, actionResponse);
100                 }
101             }
102         }
103         else {
104             ThemeDisplay themeDisplay =
105                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
106 
107             if (actionRequest.getRemoteUser() != null) {
108                 actionResponse.sendRedirect(themeDisplay.getPathMain());
109             }
110             else if (Validator.isNotNull(cmd)) {
111                 try {
112                     login(themeDisplay, actionRequest, actionResponse);
113                 }
114                 catch (Exception e) {
115                     if (e instanceof AuthException) {
116                         Throwable cause = e.getCause();
117 
118                         if (cause instanceof PasswordExpiredException ||
119                             cause instanceof UserLockoutException) {
120 
121                             SessionErrors.add(
122                                 actionRequest, cause.getClass().getName());
123                         }
124                         else {
125                             SessionErrors.add(
126                                 actionRequest, e.getClass().getName());
127                         }
128                     }
129                     else if (e instanceof CookieNotSupportedException ||
130                              e instanceof NoSuchUserException ||
131                              e instanceof PasswordExpiredException ||
132                              e instanceof UserEmailAddressException ||
133                              e instanceof UserIdException ||
134                              e instanceof UserLockoutException ||
135                              e instanceof UserPasswordException ||
136                              e instanceof UserScreenNameException) {
137 
138                         SessionErrors.add(
139                             actionRequest, e.getClass().getName());
140                     }
141                     else {
142                         PortalUtil.sendError(e, actionRequest, actionResponse);
143                     }
144                 }
145             }
146         }
147     }
148 
149     public ActionForward render(
150             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
151             RenderRequest renderRequest, RenderResponse renderResponse)
152         throws Exception {
153 
154         return mapping.findForward("portlet.login.view");
155     }
156 
157     protected void login(
158             ThemeDisplay themeDisplay, ActionRequest actionRequest,
159             ActionResponse actionResponse)
160         throws Exception {
161 
162         HttpServletRequest request = PortalUtil.getHttpServletRequest(
163             actionRequest);
164         HttpServletResponse response = PortalUtil.getHttpServletResponse(
165             actionResponse);
166 
167         String login = ParamUtil.getString(actionRequest, "login");
168         String password = ParamUtil.getString(actionRequest, "password");
169         boolean rememberMe = ParamUtil.getBoolean(actionRequest, "rememberMe");
170 
171         LoginAction.login(request, response, login, password, rememberMe);
172 
173         if (PropsValues.PORTAL_JAAS_ENABLE) {
174             actionResponse.sendRedirect(
175                 themeDisplay.getPathMain() + "/portal/protected");
176         }
177         else {
178             String redirect = ParamUtil.getString(actionRequest, "redirect");
179 
180             if (Validator.isNotNull(redirect)) {
181                 actionResponse.sendRedirect(redirect);
182             }
183             else {
184                 actionResponse.sendRedirect(themeDisplay.getPathMain());
185             }
186         }
187     }
188 
189 }