1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.UserEmailAddressException;
21  import com.liferay.portal.UserIdException;
22  import com.liferay.portal.UserLockoutException;
23  import com.liferay.portal.UserPasswordException;
24  import com.liferay.portal.UserScreenNameException;
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.security.auth.AuthException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.PortletPreferencesFactoryUtil;
35  import com.liferay.portlet.login.util.LoginUtil;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.PortletPreferences;
41  import javax.portlet.RenderRequest;
42  import javax.portlet.RenderResponse;
43  
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="LoginAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   */
56  public class LoginAction extends PortletAction {
57  
58      public void processAction(
59              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
60              ActionRequest actionRequest, ActionResponse actionResponse)
61          throws Exception {
62  
63          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
64              WebKeys.THEME_DISPLAY);
65  
66          /*if (actionRequest.getRemoteUser() != null) {
67              actionResponse.sendRedirect(themeDisplay.getPathMain());
68  
69              return;
70          }*/
71  
72          try {
73              PortletPreferences preferences =
74                  PortletPreferencesFactoryUtil.getPortletSetup(actionRequest);
75  
76              login(themeDisplay, actionRequest, actionResponse, preferences);
77          }
78          catch (Exception e) {
79              if (e instanceof AuthException) {
80                  Throwable cause = e.getCause();
81  
82                  if (cause instanceof PasswordExpiredException ||
83                      cause instanceof UserLockoutException) {
84  
85                      SessionErrors.add(
86                          actionRequest, cause.getClass().getName());
87                  }
88                  else {
89                      SessionErrors.add(actionRequest, e.getClass().getName());
90                  }
91              }
92              else if (e instanceof CookieNotSupportedException ||
93                       e instanceof NoSuchUserException ||
94                       e instanceof PasswordExpiredException ||
95                       e instanceof UserEmailAddressException ||
96                       e instanceof UserIdException ||
97                       e instanceof UserLockoutException ||
98                       e instanceof UserPasswordException ||
99                       e instanceof UserScreenNameException) {
100 
101                 SessionErrors.add(actionRequest, e.getClass().getName());
102             }
103             else {
104                 PortalUtil.sendError(e, actionRequest, actionResponse);
105             }
106         }
107     }
108 
109     public ActionForward render(
110             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
111             RenderRequest renderRequest, RenderResponse renderResponse)
112         throws Exception {
113 
114         return mapping.findForward("portlet.login.login");
115     }
116 
117     protected boolean isCheckMethodOnProcessAction() {
118         return _CHECK_METHOD_ON_PROCESS_ACTION;
119     }
120 
121     protected void login(
122             ThemeDisplay themeDisplay, ActionRequest actionRequest,
123             ActionResponse actionResponse, PortletPreferences preferences)
124         throws Exception {
125 
126         HttpServletRequest request = PortalUtil.getHttpServletRequest(
127             actionRequest);
128         HttpServletResponse response = PortalUtil.getHttpServletResponse(
129             actionResponse);
130 
131         String login = ParamUtil.getString(actionRequest, "login");
132         String password = ParamUtil.getString(actionRequest, "password");
133         boolean rememberMe = ParamUtil.getBoolean(actionRequest, "rememberMe");
134 
135         String authType = preferences.getValue("authType", null);
136 
137         LoginUtil.login(
138             request, response, login, password, rememberMe, authType);
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                 redirect = PortalUtil.escapeRedirect(redirect);
149 
150                 actionResponse.sendRedirect(redirect);
151             }
152             else {
153                 actionResponse.sendRedirect(themeDisplay.getPathMain());
154             }
155         }
156     }
157 
158     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
159 
160 }