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