1   /**
2    * Copyright (c) 2000-2007 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.GetterUtil;
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.ActionConstants;
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.PropsUtil;
44  import com.liferay.portal.util.WebKeys;
45  import com.liferay.util.servlet.SessionErrors;
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  import javax.servlet.jsp.PageContext;
56  
57  import org.apache.struts.action.ActionForm;
58  import org.apache.struts.action.ActionForward;
59  import org.apache.struts.action.ActionMapping;
60  
61  /**
62   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Brian Wing Shun Chan
65   *
66   */
67  public class ViewAction extends PortletAction {
68  
69      public void processAction(
70              ActionMapping mapping, ActionForm form, PortletConfig config,
71              ActionRequest req, ActionResponse res)
72          throws Exception {
73  
74          String cmd = req.getParameter(Constants.CMD);
75  
76          ThemeDisplay themeDisplay =
77              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
78  
79          if (req.getRemoteUser() != null) {
80              res.sendRedirect(themeDisplay.getPathMain());
81          }
82          else if (Validator.isNotNull(cmd)) {
83              try {
84                  login(themeDisplay, req, res);
85              }
86              catch (Exception e) {
87                  if (e instanceof AuthException) {
88                      Throwable cause = e.getCause();
89  
90                      if (cause instanceof PasswordExpiredException ||
91                          cause instanceof UserLockoutException) {
92  
93                          SessionErrors.add(req, cause.getClass().getName());
94                      }
95                      else {
96                          SessionErrors.add(req, e.getClass().getName());
97                      }
98                  }
99                  else if (e instanceof CookieNotSupportedException ||
100                          e instanceof NoSuchUserException ||
101                          e instanceof PasswordExpiredException ||
102                          e instanceof UserEmailAddressException ||
103                          e instanceof UserIdException ||
104                          e instanceof UserLockoutException ||
105                          e instanceof UserPasswordException ||
106                          e instanceof UserScreenNameException) {
107 
108                     SessionErrors.add(req, e.getClass().getName());
109                 }
110                 else {
111                     req.setAttribute(PageContext.EXCEPTION, e);
112 
113                     setForward(req, ActionConstants.COMMON_ERROR);
114                 }
115             }
116         }
117     }
118 
119     public ActionForward render(
120             ActionMapping mapping, ActionForm form, PortletConfig config,
121             RenderRequest req, RenderResponse res)
122         throws Exception {
123 
124         return mapping.findForward("portlet.login.view");
125     }
126 
127     protected void login(
128             ThemeDisplay themeDisplay, ActionRequest req, ActionResponse res)
129         throws Exception {
130 
131         HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
132         HttpServletResponse httpRes = PortalUtil.getHttpServletResponse(res);
133 
134         String login = ParamUtil.getString(req, "login");
135         String password = ParamUtil.getString(req, "password");
136         boolean rememberMe = ParamUtil.getBoolean(req, "rememberMe");
137 
138         LoginAction.login(httpReq, httpRes, login, password, rememberMe);
139 
140         if (GetterUtil.getBoolean(
141                 PropsUtil.get(PropsUtil.PORTAL_JAAS_ENABLE))) {
142 
143             res.sendRedirect(themeDisplay.getPathMain() + "/portal/protected");
144         }
145         else {
146             res.sendRedirect(themeDisplay.getPathMain());
147         }
148     }
149 
150 }