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