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