1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.login.action;
21  
22  import com.liferay.portal.CookieNotSupportedException;
23  import com.liferay.portal.NoSuchUserException;
24  import com.liferay.portal.PasswordExpiredException;
25  import com.liferay.portal.SendPasswordException;
26  import com.liferay.portal.UserEmailAddressException;
27  import com.liferay.portal.UserIdException;
28  import com.liferay.portal.UserLockoutException;
29  import com.liferay.portal.UserPasswordException;
30  import com.liferay.portal.UserScreenNameException;
31  import com.liferay.portal.action.LoginAction;
32  import com.liferay.portal.kernel.captcha.CaptchaTextException;
33  import com.liferay.portal.kernel.captcha.CaptchaUtil;
34  import com.liferay.portal.kernel.servlet.SessionErrors;
35  import com.liferay.portal.kernel.servlet.SessionMessages;
36  import com.liferay.portal.kernel.util.Constants;
37  import com.liferay.portal.kernel.util.ParamUtil;
38  import com.liferay.portal.kernel.util.Validator;
39  import com.liferay.portal.security.auth.AuthException;
40  import com.liferay.portal.struts.PortletAction;
41  import com.liferay.portal.theme.ThemeDisplay;
42  import com.liferay.portal.util.PortalUtil;
43  import com.liferay.portal.util.PropsValues;
44  import com.liferay.portal.util.WebKeys;
45  
46  import javax.portlet.ActionRequest;
47  import javax.portlet.ActionResponse;
48  import javax.portlet.PortletConfig;
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="ViewAction.java.html"><b><i>View Source</i></b></a>
61   *
62   * @author Brian Wing Shun Chan
63   *
64   */
65  public class ViewAction extends PortletAction {
66  
67      public void processAction(
68              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
69              ActionRequest actionRequest, ActionResponse actionResponse)
70          throws Exception {
71  
72          String cmd = actionRequest.getParameter(Constants.CMD);
73  
74          if (cmd.equals("forgot-password")) {
75              HttpServletRequest request = PortalUtil.getHttpServletRequest(
76                  actionRequest);
77  
78              try {
79                  if (PropsValues.CAPTCHA_CHECK_PORTAL_SEND_PASSWORD) {
80                      CaptchaUtil.check(actionRequest);
81                  }
82  
83                  LoginAction.sendPassword(request);
84  
85                  SessionMessages.add(request, "request_processed");
86              }
87              catch (Exception e) {
88                  if (e instanceof CaptchaTextException ||
89                      e instanceof NoSuchUserException ||
90                      e instanceof SendPasswordException ||
91                      e instanceof UserEmailAddressException) {
92  
93                      SessionErrors.add(request, e.getClass().getName());
94                  }
95                  else {
96                      PortalUtil.sendError(e, actionRequest, actionResponse);
97                  }
98              }
99          }
100         else {
101             ThemeDisplay themeDisplay =
102                 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
103 
104             if (actionRequest.getRemoteUser() != null) {
105                 actionResponse.sendRedirect(themeDisplay.getPathMain());
106             }
107             else if (Validator.isNotNull(cmd)) {
108                 try {
109                     login(themeDisplay, actionRequest, actionResponse);
110                 }
111                 catch (Exception e) {
112                     if (e instanceof AuthException) {
113                         Throwable cause = e.getCause();
114 
115                         if (cause instanceof PasswordExpiredException ||
116                             cause instanceof UserLockoutException) {
117 
118                             SessionErrors.add(
119                                 actionRequest, cause.getClass().getName());
120                         }
121                         else {
122                             SessionErrors.add(
123                                 actionRequest, e.getClass().getName());
124                         }
125                     }
126                     else if (e instanceof CookieNotSupportedException ||
127                              e instanceof NoSuchUserException ||
128                              e instanceof PasswordExpiredException ||
129                              e instanceof UserEmailAddressException ||
130                              e instanceof UserIdException ||
131                              e instanceof UserLockoutException ||
132                              e instanceof UserPasswordException ||
133                              e instanceof UserScreenNameException) {
134 
135                         SessionErrors.add(
136                             actionRequest, e.getClass().getName());
137                     }
138                     else {
139                         PortalUtil.sendError(e, actionRequest, actionResponse);
140                     }
141                 }
142             }
143         }
144     }
145 
146     public ActionForward render(
147             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
148             RenderRequest renderRequest, RenderResponse renderResponse)
149         throws Exception {
150 
151         return mapping.findForward("portlet.login.view");
152     }
153 
154     protected void login(
155             ThemeDisplay themeDisplay, ActionRequest actionRequest,
156             ActionResponse actionResponse)
157         throws Exception {
158 
159         HttpServletRequest request = PortalUtil.getHttpServletRequest(
160             actionRequest);
161         HttpServletResponse response = PortalUtil.getHttpServletResponse(
162             actionResponse);
163 
164         String login = ParamUtil.getString(actionRequest, "login");
165         String password = ParamUtil.getString(actionRequest, "password");
166         boolean rememberMe = ParamUtil.getBoolean(actionRequest, "rememberMe");
167 
168         LoginAction.login(request, response, login, password, rememberMe);
169 
170         if (PropsValues.PORTAL_JAAS_ENABLE) {
171             actionResponse.sendRedirect(
172                 themeDisplay.getPathMain() + "/portal/protected");
173         }
174         else {
175             String redirect = ParamUtil.getString(actionRequest, "redirect");
176 
177             if (Validator.isNotNull(redirect)) {
178                 actionResponse.sendRedirect(redirect);
179             }
180             else {
181                 actionResponse.sendRedirect(themeDisplay.getPathMain());
182             }
183         }
184     }
185 
186 }