001
014
015 package com.liferay.portlet.login.action;
016
017 import com.liferay.portal.AddressCityException;
018 import com.liferay.portal.AddressStreetException;
019 import com.liferay.portal.AddressZipException;
020 import com.liferay.portal.CompanyMaxUsersException;
021 import com.liferay.portal.ContactFirstNameException;
022 import com.liferay.portal.ContactFullNameException;
023 import com.liferay.portal.ContactLastNameException;
024 import com.liferay.portal.DuplicateOpenIdException;
025 import com.liferay.portal.DuplicateUserEmailAddressException;
026 import com.liferay.portal.DuplicateUserScreenNameException;
027 import com.liferay.portal.EmailAddressException;
028 import com.liferay.portal.GroupFriendlyURLException;
029 import com.liferay.portal.NoSuchCountryException;
030 import com.liferay.portal.NoSuchLayoutException;
031 import com.liferay.portal.NoSuchListTypeException;
032 import com.liferay.portal.NoSuchOrganizationException;
033 import com.liferay.portal.NoSuchRegionException;
034 import com.liferay.portal.NoSuchUserException;
035 import com.liferay.portal.OrganizationParentException;
036 import com.liferay.portal.PhoneNumberException;
037 import com.liferay.portal.RequiredFieldException;
038 import com.liferay.portal.RequiredUserException;
039 import com.liferay.portal.ReservedUserEmailAddressException;
040 import com.liferay.portal.ReservedUserScreenNameException;
041 import com.liferay.portal.TermsOfUseException;
042 import com.liferay.portal.UserEmailAddressException;
043 import com.liferay.portal.UserIdException;
044 import com.liferay.portal.UserPasswordException;
045 import com.liferay.portal.UserScreenNameException;
046 import com.liferay.portal.UserSmsException;
047 import com.liferay.portal.WebsiteURLException;
048 import com.liferay.portal.kernel.captcha.CaptchaMaxChallengesException;
049 import com.liferay.portal.kernel.captcha.CaptchaTextException;
050 import com.liferay.portal.kernel.captcha.CaptchaUtil;
051 import com.liferay.portal.kernel.servlet.SessionErrors;
052 import com.liferay.portal.kernel.servlet.SessionMessages;
053 import com.liferay.portal.kernel.util.Constants;
054 import com.liferay.portal.kernel.util.ParamUtil;
055 import com.liferay.portal.kernel.util.Validator;
056 import com.liferay.portal.kernel.workflow.WorkflowConstants;
057 import com.liferay.portal.model.Company;
058 import com.liferay.portal.model.CompanyConstants;
059 import com.liferay.portal.model.Layout;
060 import com.liferay.portal.model.User;
061 import com.liferay.portal.security.auth.PrincipalException;
062 import com.liferay.portal.service.LayoutLocalServiceUtil;
063 import com.liferay.portal.service.ServiceContext;
064 import com.liferay.portal.service.ServiceContextFactory;
065 import com.liferay.portal.service.UserLocalServiceUtil;
066 import com.liferay.portal.service.UserServiceUtil;
067 import com.liferay.portal.struts.PortletAction;
068 import com.liferay.portal.theme.ThemeDisplay;
069 import com.liferay.portal.util.PortalUtil;
070 import com.liferay.portal.util.PropsValues;
071 import com.liferay.portal.util.WebKeys;
072 import com.liferay.portlet.login.util.LoginUtil;
073
074 import javax.portlet.ActionRequest;
075 import javax.portlet.ActionResponse;
076 import javax.portlet.PortletConfig;
077 import javax.portlet.PortletURL;
078 import javax.portlet.RenderRequest;
079 import javax.portlet.RenderResponse;
080
081 import javax.servlet.http.HttpServletRequest;
082 import javax.servlet.http.HttpServletResponse;
083 import javax.servlet.http.HttpSession;
084
085 import org.apache.struts.action.ActionForm;
086 import org.apache.struts.action.ActionForward;
087 import org.apache.struts.action.ActionMapping;
088
089
095 public class CreateAccountAction extends PortletAction {
096
097 @Override
098 public void processAction(
099 ActionMapping actionMapping, ActionForm actionForm,
100 PortletConfig portletConfig, ActionRequest actionRequest,
101 ActionResponse actionResponse)
102 throws Exception {
103
104 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
105 WebKeys.THEME_DISPLAY);
106
107 Company company = themeDisplay.getCompany();
108
109 if (!company.isStrangers()) {
110 throw new PrincipalException();
111 }
112
113 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
114
115 try {
116 if (cmd.equals(Constants.ADD)) {
117 if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
118 CaptchaUtil.check(actionRequest);
119 }
120
121 addUser(actionRequest, actionResponse);
122 }
123 else if (cmd.equals(Constants.RESET)) {
124 resetUser(actionRequest, actionResponse);
125 }
126 else if (cmd.equals(Constants.UPDATE)) {
127 updateIncompleteUser(actionRequest, actionResponse);
128 }
129 }
130 catch (Exception e) {
131 if (e instanceof DuplicateUserEmailAddressException ||
132 e instanceof DuplicateUserScreenNameException) {
133
134 String emailAddress = ParamUtil.getString(
135 actionRequest, "emailAddress");
136
137 try {
138 User user = UserLocalServiceUtil.getUserByEmailAddress(
139 themeDisplay.getCompanyId(), emailAddress);
140
141 if (user.getStatus() !=
142 WorkflowConstants.STATUS_INCOMPLETE) {
143
144 SessionErrors.add(actionRequest, e.getClass(), e);
145 }
146 else {
147 setForward(
148 actionRequest, "portlet.login.update_account");
149 }
150 }
151 catch (NoSuchUserException nsue) {
152 SessionErrors.add(actionRequest, e.getClass(), e);
153 }
154 }
155 else if (e instanceof AddressCityException ||
156 e instanceof AddressStreetException ||
157 e instanceof AddressZipException ||
158 e instanceof CaptchaMaxChallengesException ||
159 e instanceof CaptchaTextException ||
160 e instanceof CompanyMaxUsersException ||
161 e instanceof ContactFirstNameException ||
162 e instanceof ContactFullNameException ||
163 e instanceof ContactLastNameException ||
164 e instanceof DuplicateOpenIdException ||
165 e instanceof EmailAddressException ||
166 e instanceof GroupFriendlyURLException ||
167 e instanceof NoSuchCountryException ||
168 e instanceof NoSuchListTypeException ||
169 e instanceof NoSuchOrganizationException ||
170 e instanceof NoSuchRegionException ||
171 e instanceof OrganizationParentException ||
172 e instanceof PhoneNumberException ||
173 e instanceof RequiredFieldException ||
174 e instanceof RequiredUserException ||
175 e instanceof ReservedUserEmailAddressException ||
176 e instanceof ReservedUserScreenNameException ||
177 e instanceof TermsOfUseException ||
178 e instanceof UserEmailAddressException ||
179 e instanceof UserIdException ||
180 e instanceof UserPasswordException ||
181 e instanceof UserScreenNameException ||
182 e instanceof UserSmsException ||
183 e instanceof WebsiteURLException) {
184
185 SessionErrors.add(actionRequest, e.getClass(), e);
186 }
187 else {
188 throw e;
189 }
190 }
191
192 if (Validator.isNull(PropsValues.COMPANY_SECURITY_STRANGERS_URL)) {
193 return;
194 }
195
196 try {
197 Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
198 themeDisplay.getScopeGroupId(), false,
199 PropsValues.COMPANY_SECURITY_STRANGERS_URL);
200
201 String redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
202
203 sendRedirect(actionRequest, actionResponse, redirect);
204 }
205 catch (NoSuchLayoutException nsle) {
206 }
207 }
208
209 @Override
210 public ActionForward render(
211 ActionMapping actionMapping, ActionForm actionForm,
212 PortletConfig portletConfig, RenderRequest renderRequest,
213 RenderResponse renderResponse)
214 throws Exception {
215
216 ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
217 WebKeys.THEME_DISPLAY);
218
219 Company company = themeDisplay.getCompany();
220
221 if (!company.isStrangers()) {
222 return actionMapping.findForward("portlet.login.login");
223 }
224
225 renderResponse.setTitle(themeDisplay.translate("create-account"));
226
227 return actionMapping.findForward(
228 getForward(renderRequest, "portlet.login.create_account"));
229 }
230
231 protected void addUser(
232 ActionRequest actionRequest, ActionResponse actionResponse)
233 throws Exception {
234
235 HttpServletRequest request = PortalUtil.getHttpServletRequest(
236 actionRequest);
237 HttpSession session = request.getSession();
238
239 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
240 WebKeys.THEME_DISPLAY);
241
242 Company company = themeDisplay.getCompany();
243
244 boolean autoPassword = true;
245 String password1 = null;
246 String password2 = null;
247 boolean autoScreenName = isAutoScreenName();
248 String screenName = ParamUtil.getString(actionRequest, "screenName");
249 String emailAddress = ParamUtil.getString(
250 actionRequest, "emailAddress");
251 long facebookId = ParamUtil.getLong(actionRequest, "facebookId");
252 String openId = ParamUtil.getString(actionRequest, "openId");
253 String firstName = ParamUtil.getString(actionRequest, "firstName");
254 String middleName = ParamUtil.getString(actionRequest, "middleName");
255 String lastName = ParamUtil.getString(actionRequest, "lastName");
256 int prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
257 int suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
258 boolean male = ParamUtil.getBoolean(actionRequest, "male", true);
259 int birthdayMonth = ParamUtil.getInteger(
260 actionRequest, "birthdayMonth");
261 int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
262 int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
263 String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
264 long[] groupIds = null;
265 long[] organizationIds = null;
266 long[] roleIds = null;
267 long[] userGroupIds = null;
268 boolean sendEmail = true;
269
270 ServiceContext serviceContext = ServiceContextFactory.getInstance(
271 User.class.getName(), actionRequest);
272
273 if (PropsValues.LOGIN_CREATE_ACCOUNT_ALLOW_CUSTOM_PASSWORD) {
274 autoPassword = false;
275
276 password1 = ParamUtil.getString(actionRequest, "password1");
277 password2 = ParamUtil.getString(actionRequest, "password2");
278 }
279
280 boolean openIdPending = false;
281
282 Boolean openIdLoginPending = (Boolean)session.getAttribute(
283 WebKeys.OPEN_ID_LOGIN_PENDING);
284
285 if ((openIdLoginPending != null) && openIdLoginPending.booleanValue() &&
286 Validator.isNotNull(openId)) {
287
288 sendEmail = false;
289 openIdPending = true;
290 }
291
292 User user = UserServiceUtil.addUserWithWorkflow(
293 company.getCompanyId(), autoPassword, password1, password2,
294 autoScreenName, screenName, emailAddress, facebookId, openId,
295 themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
296 suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
297 groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
298 serviceContext);
299
300 if (openIdPending) {
301 session.setAttribute(
302 WebKeys.OPEN_ID_LOGIN, new Long(user.getUserId()));
303
304 session.removeAttribute(WebKeys.OPEN_ID_LOGIN_PENDING);
305 }
306 else {
307
308
309
310 if (user.getStatus() == WorkflowConstants.STATUS_APPROVED) {
311 SessionMessages.add(
312 request, "user_added", user.getEmailAddress());
313 SessionMessages.add(
314 request, "user_added_password",
315 user.getPasswordUnencrypted());
316 }
317 else {
318 SessionMessages.add(
319 request, "user_pending", user.getEmailAddress());
320 }
321 }
322
323
324
325 String login = null;
326
327 if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
328 login = String.valueOf(user.getUserId());
329 }
330 else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
331 login = user.getScreenName();
332 }
333 else {
334 login = user.getEmailAddress();
335 }
336
337 sendRedirect(
338 actionRequest, actionResponse, themeDisplay, login,
339 user.getPasswordUnencrypted());
340 }
341
342 protected boolean isAutoScreenName() {
343 return _AUTO_SCREEN_NAME;
344 }
345
346 @Override
347 protected boolean isCheckMethodOnProcessAction() {
348 return _CHECK_METHOD_ON_PROCESS_ACTION;
349 }
350
351 protected void resetUser(
352 ActionRequest actionRequest, ActionResponse actionResponse)
353 throws Exception {
354
355 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
356 WebKeys.THEME_DISPLAY);
357
358 String emailAddress = ParamUtil.getString(
359 actionRequest, "emailAddress");
360
361 User anonymousUser = UserLocalServiceUtil.getUserByEmailAddress(
362 themeDisplay.getCompanyId(), emailAddress);
363
364 if (anonymousUser.getStatus() != WorkflowConstants.STATUS_INCOMPLETE) {
365 throw new PrincipalException();
366 }
367
368 UserLocalServiceUtil.deleteUser(anonymousUser.getUserId());
369
370 addUser(actionRequest, actionResponse);
371 }
372
373 protected void sendRedirect(
374 ActionRequest actionRequest, ActionResponse actionResponse,
375 ThemeDisplay themeDisplay, String login, String password)
376 throws Exception {
377
378 HttpServletRequest request = PortalUtil.getHttpServletRequest(
379 actionRequest);
380
381 String redirect = PortalUtil.escapeRedirect(
382 ParamUtil.getString(actionRequest, "redirect"));
383
384 if (Validator.isNotNull(redirect)) {
385 HttpServletResponse response = PortalUtil.getHttpServletResponse(
386 actionResponse);
387
388 LoginUtil.login(request, response, login, password, false, null);
389 }
390 else {
391 PortletURL loginURL = LoginUtil.getLoginURL(
392 request, themeDisplay.getPlid());
393
394 loginURL.setParameter("login", login);
395
396 redirect = loginURL.toString();
397 }
398
399 actionResponse.sendRedirect(redirect);
400 }
401
402 protected void updateIncompleteUser(
403 ActionRequest actionRequest, ActionResponse actionResponse)
404 throws Exception {
405
406 HttpServletRequest request = PortalUtil.getHttpServletRequest(
407 actionRequest);
408
409 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
410 WebKeys.THEME_DISPLAY);
411
412 boolean autoPassword = true;
413 String password1 = null;
414 String password2 = null;
415 boolean autoScreenName = false;
416 String screenName = ParamUtil.getString(actionRequest, "screenName");
417 String emailAddress = ParamUtil.getString(
418 actionRequest, "emailAddress");
419 long facebookId = ParamUtil.getLong(actionRequest, "facebookId");
420 String openId = ParamUtil.getString(actionRequest, "openId");
421 String firstName = ParamUtil.getString(actionRequest, "firstName");
422 String middleName = ParamUtil.getString(actionRequest, "middleName");
423 String lastName = ParamUtil.getString(actionRequest, "lastName");
424 int prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
425 int suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
426 boolean male = ParamUtil.getBoolean(actionRequest, "male", true);
427 int birthdayMonth = ParamUtil.getInteger(
428 actionRequest, "birthdayMonth");
429 int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
430 int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
431 String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
432 boolean updateUserInformation = true;
433 boolean sendEmail = true;
434
435 ServiceContext serviceContext = ServiceContextFactory.getInstance(
436 User.class.getName(), actionRequest);
437
438 User user = UserServiceUtil.updateIncompleteUser(
439 themeDisplay.getCompanyId(), autoPassword, password1, password2,
440 autoScreenName, screenName, emailAddress, facebookId, openId,
441 themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
442 suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
443 sendEmail, updateUserInformation, serviceContext);
444
445
446
447 if (user.getStatus() == WorkflowConstants.STATUS_APPROVED) {
448 SessionMessages.add(request, "user_added", user.getEmailAddress());
449 SessionMessages.add(
450 request, "user_added_password", user.getPasswordUnencrypted());
451 }
452 else {
453 SessionMessages.add(
454 request, "user_pending", user.getEmailAddress());
455 }
456
457
458
459 String login = null;
460
461 Company company = themeDisplay.getCompany();
462
463 if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
464 login = String.valueOf(user.getUserId());
465 }
466 else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
467 login = user.getScreenName();
468 }
469 else {
470 login = user.getEmailAddress();
471 }
472
473 sendRedirect(
474 actionRequest, actionResponse, themeDisplay, login,
475 user.getPasswordUnencrypted());
476 }
477
478 private static final boolean _AUTO_SCREEN_NAME = false;
479
480 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
481
482 }