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.AddressCityException;
18  import com.liferay.portal.AddressStreetException;
19  import com.liferay.portal.AddressZipException;
20  import com.liferay.portal.CompanyMaxUsersException;
21  import com.liferay.portal.ContactFirstNameException;
22  import com.liferay.portal.ContactFullNameException;
23  import com.liferay.portal.ContactLastNameException;
24  import com.liferay.portal.DuplicateUserEmailAddressException;
25  import com.liferay.portal.DuplicateUserScreenNameException;
26  import com.liferay.portal.EmailAddressException;
27  import com.liferay.portal.NoSuchCountryException;
28  import com.liferay.portal.NoSuchLayoutException;
29  import com.liferay.portal.NoSuchListTypeException;
30  import com.liferay.portal.NoSuchOrganizationException;
31  import com.liferay.portal.NoSuchRegionException;
32  import com.liferay.portal.OrganizationParentException;
33  import com.liferay.portal.PhoneNumberException;
34  import com.liferay.portal.RequiredFieldException;
35  import com.liferay.portal.RequiredUserException;
36  import com.liferay.portal.ReservedUserEmailAddressException;
37  import com.liferay.portal.ReservedUserScreenNameException;
38  import com.liferay.portal.TermsOfUseException;
39  import com.liferay.portal.UserEmailAddressException;
40  import com.liferay.portal.UserIdException;
41  import com.liferay.portal.UserPasswordException;
42  import com.liferay.portal.UserScreenNameException;
43  import com.liferay.portal.UserSmsException;
44  import com.liferay.portal.WebsiteURLException;
45  import com.liferay.portal.kernel.captcha.CaptchaTextException;
46  import com.liferay.portal.kernel.captcha.CaptchaUtil;
47  import com.liferay.portal.kernel.servlet.SessionErrors;
48  import com.liferay.portal.kernel.servlet.SessionMessages;
49  import com.liferay.portal.kernel.util.Constants;
50  import com.liferay.portal.kernel.util.ParamUtil;
51  import com.liferay.portal.kernel.util.Validator;
52  import com.liferay.portal.model.Company;
53  import com.liferay.portal.model.CompanyConstants;
54  import com.liferay.portal.model.Layout;
55  import com.liferay.portal.model.User;
56  import com.liferay.portal.security.auth.PrincipalException;
57  import com.liferay.portal.service.LayoutLocalServiceUtil;
58  import com.liferay.portal.service.ServiceContext;
59  import com.liferay.portal.service.ServiceContextFactory;
60  import com.liferay.portal.service.UserServiceUtil;
61  import com.liferay.portal.struts.PortletAction;
62  import com.liferay.portal.theme.ThemeDisplay;
63  import com.liferay.portal.util.PortalUtil;
64  import com.liferay.portal.util.PropsValues;
65  import com.liferay.portal.util.WebKeys;
66  import com.liferay.portlet.login.util.LoginUtil;
67  
68  import javax.portlet.ActionRequest;
69  import javax.portlet.ActionResponse;
70  import javax.portlet.PortletConfig;
71  import javax.portlet.PortletURL;
72  import javax.portlet.RenderRequest;
73  import javax.portlet.RenderResponse;
74  
75  import javax.servlet.http.HttpServletRequest;
76  import javax.servlet.http.HttpServletResponse;
77  import javax.servlet.http.HttpSession;
78  
79  import org.apache.struts.action.ActionForm;
80  import org.apache.struts.action.ActionForward;
81  import org.apache.struts.action.ActionMapping;
82  
83  /**
84   * <a href="CreateAccountAction.java.html"><b><i>View Source</i></b></a>
85   *
86   * @author Brian Wing Shun Chan
87   * @author Amos Fong
88   */
89  public class CreateAccountAction extends PortletAction {
90  
91      public void processAction(
92              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
93              ActionRequest actionRequest, ActionResponse actionResponse)
94          throws Exception {
95  
96          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
97  
98          try {
99              if (cmd.equals(Constants.ADD)) {
100                 addUser(actionRequest, actionResponse);
101             }
102         }
103         catch (Exception e) {
104             if (e instanceof AddressCityException ||
105                 e instanceof AddressStreetException ||
106                 e instanceof AddressZipException ||
107                 e instanceof CaptchaTextException ||
108                 e instanceof CompanyMaxUsersException ||
109                 e instanceof ContactFirstNameException ||
110                 e instanceof ContactFullNameException ||
111                 e instanceof ContactLastNameException ||
112                 e instanceof DuplicateUserEmailAddressException ||
113                 e instanceof DuplicateUserScreenNameException ||
114                 e instanceof EmailAddressException ||
115                 e instanceof NoSuchCountryException ||
116                 e instanceof NoSuchListTypeException ||
117                 e instanceof NoSuchOrganizationException ||
118                 e instanceof NoSuchRegionException ||
119                 e instanceof OrganizationParentException ||
120                 e instanceof PhoneNumberException ||
121                 e instanceof RequiredFieldException ||
122                 e instanceof RequiredUserException ||
123                 e instanceof ReservedUserEmailAddressException ||
124                 e instanceof ReservedUserScreenNameException ||
125                 e instanceof TermsOfUseException ||
126                 e instanceof UserEmailAddressException ||
127                 e instanceof UserIdException ||
128                 e instanceof UserPasswordException ||
129                 e instanceof UserScreenNameException ||
130                 e instanceof UserSmsException ||
131                 e instanceof WebsiteURLException) {
132 
133                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
134             }
135             else {
136                 throw e;
137             }
138         }
139 
140         if (Validator.isNull(PropsValues.COMPANY_SECURITY_STRANGERS_URL)) {
141             return;
142         }
143 
144         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
145             WebKeys.THEME_DISPLAY);
146 
147         try {
148             Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
149                 themeDisplay.getScopeGroupId(), false,
150                 PropsValues.COMPANY_SECURITY_STRANGERS_URL);
151 
152             String redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
153 
154             sendRedirect(actionRequest, actionResponse, redirect);
155         }
156         catch (NoSuchLayoutException nsle) {
157         }
158     }
159 
160     public ActionForward render(
161             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
162             RenderRequest renderRequest, RenderResponse renderResponse)
163         throws Exception {
164 
165         Company company = PortalUtil.getCompany(renderRequest);
166 
167         if (!company.isStrangers()) {
168             throw new PrincipalException();
169         }
170 
171         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
172             WebKeys.THEME_DISPLAY);
173 
174         renderResponse.setTitle(themeDisplay.translate("create-account"));
175 
176         return mapping.findForward("portlet.login.create_account");
177     }
178 
179     protected void addUser(
180             ActionRequest actionRequest, ActionResponse actionResponse)
181         throws Exception {
182 
183         HttpServletRequest request = PortalUtil.getHttpServletRequest(
184             actionRequest);
185         HttpSession session = request.getSession();
186 
187         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
188             WebKeys.THEME_DISPLAY);
189 
190         Company company = themeDisplay.getCompany();
191 
192         boolean autoPassword = true;
193         String password1 = null;
194         String password2 = null;
195         boolean autoScreenName = isAutoScreenName();
196         String screenName = ParamUtil.getString(actionRequest, "screenName");
197         String emailAddress = ParamUtil.getString(
198             actionRequest, "emailAddress");
199         String openId = ParamUtil.getString(actionRequest, "openId");
200         String firstName = ParamUtil.getString(actionRequest, "firstName");
201         String middleName = ParamUtil.getString(actionRequest, "middleName");
202         String lastName = ParamUtil.getString(actionRequest, "lastName");
203         int prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
204         int suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
205         boolean male = ParamUtil.get(actionRequest, "male", true);
206         int birthdayMonth = ParamUtil.getInteger(
207             actionRequest, "birthdayMonth");
208         int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
209         int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
210         String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
211         long[] groupIds = null;
212         long[] organizationIds = null;
213         long[] roleIds = null;
214         long[] userGroupIds = null;
215         boolean sendEmail = true;
216 
217         ServiceContext serviceContext = ServiceContextFactory.getInstance(
218             User.class.getName(), actionRequest);
219 
220         if (PropsValues.LOGIN_CREATE_ACCOUNT_ALLOW_CUSTOM_PASSWORD) {
221             autoPassword = false;
222 
223             password1 = ParamUtil.getString(actionRequest, "password1");
224             password2 = ParamUtil.getString(actionRequest, "password2");
225         }
226 
227         boolean openIdPending = false;
228 
229         Boolean openIdLoginPending = (Boolean)session.getAttribute(
230             WebKeys.OPEN_ID_LOGIN_PENDING);
231 
232         if ((openIdLoginPending != null) &&
233             (openIdLoginPending.booleanValue()) &&
234             (Validator.isNotNull(openId))) {
235 
236             sendEmail = false;
237             openIdPending = true;
238         }
239 
240         if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
241             CaptchaUtil.check(actionRequest);
242         }
243 
244         User user = UserServiceUtil.addUser(
245             company.getCompanyId(), autoPassword, password1, password2,
246             autoScreenName, screenName, emailAddress, openId,
247             themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
248             suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
249             groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
250             serviceContext);
251 
252         if (openIdPending) {
253             session.setAttribute(
254                 WebKeys.OPEN_ID_LOGIN, new Long(user.getUserId()));
255 
256             session.removeAttribute(WebKeys.OPEN_ID_LOGIN_PENDING);
257         }
258         else {
259 
260             // Session messages
261 
262             SessionMessages.add(request, "user_added", user.getEmailAddress());
263             SessionMessages.add(
264                 request, "user_added_password", user.getPasswordUnencrypted());
265         }
266 
267         // Send redirect
268 
269         String login = null;
270 
271         if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
272             login = String.valueOf(user.getUserId());
273         }
274         else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
275             login = user.getScreenName();
276         }
277         else {
278             login = user.getEmailAddress();
279         }
280 
281         sendRedirect(
282             actionRequest, actionResponse, themeDisplay, login,
283             user.getPasswordUnencrypted());
284     }
285 
286     protected boolean isAutoScreenName() {
287         return _AUTO_SCREEN_NAME;
288     }
289 
290     protected void sendRedirect(
291             ActionRequest actionRequest, ActionResponse actionResponse,
292             ThemeDisplay themeDisplay, String login, String password)
293         throws Exception {
294 
295         HttpServletRequest request = PortalUtil.getHttpServletRequest(
296             actionRequest);
297 
298         String redirect = PortalUtil.escapeRedirect(
299             ParamUtil.getString(actionRequest, "redirect"));
300 
301         if (Validator.isNotNull(redirect)) {
302             HttpServletResponse response = PortalUtil.getHttpServletResponse(
303                 actionResponse);
304 
305             LoginUtil.login(request, response, login, password, false, null);
306         }
307         else {
308             PortletURL loginURL = LoginUtil.getLoginURL(
309                 request, themeDisplay.getPlid());
310 
311             loginURL.setParameter("login", login);
312 
313             redirect = loginURL.toString();
314         }
315 
316         actionResponse.sendRedirect(redirect);
317     }
318 
319     protected boolean isCheckMethodOnProcessAction() {
320         return _CHECK_METHOD_ON_PROCESS_ACTION;
321     }
322 
323     private static final boolean _AUTO_SCREEN_NAME = false;
324 
325     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
326 
327 }