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