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.util.Constants;
35  import com.liferay.portal.kernel.util.ParamUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.security.auth.AuthException;
38  import com.liferay.portal.struts.ActionConstants;
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  import com.liferay.util.servlet.SessionErrors;
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  import javax.servlet.jsp.PageContext;
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 config,
70              ActionRequest req, ActionResponse res)
71          throws Exception {
72  
73          String cmd = req.getParameter(Constants.CMD);
74  
75          ThemeDisplay themeDisplay =
76              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
77  
78          if (req.getRemoteUser() != null) {
79              res.sendRedirect(themeDisplay.getPathMain());
80          }
81          else if (Validator.isNotNull(cmd)) {
82              try {
83                  login(themeDisplay, req, res);
84              }
85              catch (Exception e) {
86                  if (e instanceof AuthException) {
87                      Throwable cause = e.getCause();
88  
89                      if (cause instanceof PasswordExpiredException ||
90                          cause instanceof UserLockoutException) {
91  
92                          SessionErrors.add(req, cause.getClass().getName());
93                      }
94                      else {
95                          SessionErrors.add(req, 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(req, e.getClass().getName());
108                 }
109                 else {
110                     req.setAttribute(PageContext.EXCEPTION, e);
111 
112                     setForward(req, ActionConstants.COMMON_ERROR);
113                 }
114             }
115         }
116     }
117 
118     public ActionForward render(
119             ActionMapping mapping, ActionForm form, PortletConfig config,
120             RenderRequest req, RenderResponse res)
121         throws Exception {
122 
123         return mapping.findForward("portlet.login.view");
124     }
125 
126     protected void login(
127             ThemeDisplay themeDisplay, ActionRequest req, ActionResponse res)
128         throws Exception {
129 
130         HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
131         HttpServletResponse httpRes = PortalUtil.getHttpServletResponse(res);
132 
133         String login = ParamUtil.getString(req, "login");
134         String password = ParamUtil.getString(req, "password");
135         boolean rememberMe = ParamUtil.getBoolean(req, "rememberMe");
136 
137         LoginAction.login(httpReq, httpRes, login, password, rememberMe);
138 
139         if (PropsValues.PORTAL_JAAS_ENABLE) {
140             res.sendRedirect(themeDisplay.getPathMain() + "/portal/protected");
141         }
142         else {
143             String redirect = ParamUtil.getString(req, "redirect");
144 
145             if (Validator.isNotNull(redirect)) {
146                 res.sendRedirect(redirect);
147             }
148             else {
149                 res.sendRedirect(themeDisplay.getPathMain());
150             }
151         }
152     }
153 
154 }