1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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.NoSuchListTypeException;
28  import com.liferay.portal.NoSuchOrganizationException;
29  import com.liferay.portal.NoSuchRegionException;
30  import com.liferay.portal.OrganizationParentException;
31  import com.liferay.portal.PhoneNumberException;
32  import com.liferay.portal.RequiredFieldException;
33  import com.liferay.portal.RequiredUserException;
34  import com.liferay.portal.ReservedUserEmailAddressException;
35  import com.liferay.portal.ReservedUserScreenNameException;
36  import com.liferay.portal.TermsOfUseException;
37  import com.liferay.portal.UserEmailAddressException;
38  import com.liferay.portal.UserIdException;
39  import com.liferay.portal.UserPasswordException;
40  import com.liferay.portal.UserScreenNameException;
41  import com.liferay.portal.UserSmsException;
42  import com.liferay.portal.WebsiteURLException;
43  import com.liferay.portal.action.LoginAction;
44  import com.liferay.portal.kernel.captcha.CaptchaTextException;
45  import com.liferay.portal.kernel.captcha.CaptchaUtil;
46  import com.liferay.portal.kernel.language.LanguageUtil;
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.HttpUtil;
51  import com.liferay.portal.kernel.util.ParamUtil;
52  import com.liferay.portal.kernel.util.StringUtil;
53  import com.liferay.portal.kernel.util.Validator;
54  import com.liferay.portal.model.Company;
55  import com.liferay.portal.model.CompanyConstants;
56  import com.liferay.portal.model.User;
57  import com.liferay.portal.security.auth.PrincipalException;
58  import com.liferay.portal.service.UserLocalServiceUtil;
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  
66  import javax.portlet.ActionRequest;
67  import javax.portlet.ActionResponse;
68  import javax.portlet.PortletConfig;
69  import javax.portlet.RenderRequest;
70  import javax.portlet.RenderResponse;
71  
72  import javax.servlet.http.HttpServletRequest;
73  import javax.servlet.http.HttpServletResponse;
74  import javax.servlet.http.HttpSession;
75  
76  import org.apache.struts.action.ActionForm;
77  import org.apache.struts.action.ActionForward;
78  import org.apache.struts.action.ActionMapping;
79  
80  /**
81   * <a href="AddUserAction.java.html"><b><i>View Source</i></b></a>
82   *
83   * @author Brian Wing Shun Chan
84   * @author Amos Fong
85   */
86  public class AddUserAction extends PortletAction {
87  
88      public void processAction(
89              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
90              ActionRequest actionRequest, ActionResponse actionResponse)
91          throws Exception {
92  
93          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
94  
95          try {
96              if (cmd.equals(Constants.ADD)) {
97                  addUser(actionRequest, actionResponse);
98              }
99          }
100         catch (Exception e) {
101             if (e instanceof AddressCityException ||
102                 e instanceof AddressStreetException ||
103                 e instanceof AddressZipException ||
104                 e instanceof CaptchaTextException ||
105                 e instanceof ContactFirstNameException ||
106                 e instanceof ContactFullNameException ||
107                 e instanceof ContactLastNameException ||
108                 e instanceof DuplicateUserEmailAddressException ||
109                 e instanceof DuplicateUserScreenNameException ||
110                 e instanceof EmailAddressException ||
111                 e instanceof NoSuchCountryException ||
112                 e instanceof NoSuchListTypeException ||
113                 e instanceof NoSuchOrganizationException ||
114                 e instanceof NoSuchRegionException ||
115                 e instanceof OrganizationParentException ||
116                 e instanceof PhoneNumberException ||
117                 e instanceof RequiredFieldException ||
118                 e instanceof RequiredUserException ||
119                 e instanceof ReservedUserEmailAddressException ||
120                 e instanceof ReservedUserScreenNameException ||
121                 e instanceof TermsOfUseException ||
122                 e instanceof UserEmailAddressException ||
123                 e instanceof UserIdException ||
124                 e instanceof UserPasswordException ||
125                 e instanceof UserScreenNameException ||
126                 e instanceof UserSmsException ||
127                 e instanceof WebsiteURLException) {
128 
129                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
130             }
131             else {
132                 throw e;
133             }
134         }
135     }
136 
137     public ActionForward render(
138             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
139             RenderRequest renderRequest, RenderResponse renderResponse)
140         throws Exception {
141 
142         Company company = PortalUtil.getCompany(renderRequest);
143 
144         if (!company.isStrangers()) {
145             throw new PrincipalException();
146         }
147 
148         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
149             WebKeys.THEME_DISPLAY);
150 
151         renderResponse.setTitle(
152             LanguageUtil.get(themeDisplay.getLocale(), "create-account"));
153 
154         return mapping.findForward("portlet.login.create_account");
155     }
156 
157     protected void addUser(
158             ActionRequest actionRequest, ActionResponse actionResponse)
159         throws Exception {
160 
161         HttpServletRequest request = PortalUtil.getHttpServletRequest(
162             actionRequest);
163         HttpSession session = request.getSession();
164 
165         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
166             WebKeys.THEME_DISPLAY);
167 
168         Company company = themeDisplay.getCompany();
169 
170         boolean autoPassword = true;
171         String password1 = null;
172         String password2 = null;
173         boolean autoScreenName = isAutoScreenName();
174         String screenName = ParamUtil.getString(actionRequest, "screenName");
175         String emailAddress = ParamUtil.getString(
176             actionRequest, "emailAddress");
177         String firstName = ParamUtil.getString(actionRequest, "firstName");
178         String middleName = ParamUtil.getString(actionRequest, "middleName");
179         String lastName = ParamUtil.getString(actionRequest, "lastName");
180         int prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
181         int suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
182         boolean male = ParamUtil.get(actionRequest, "male", true);
183         int birthdayMonth = ParamUtil.getInteger(
184             actionRequest, "birthdayMonth");
185         int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
186         int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
187         String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
188         long[] organizationIds = StringUtil.split(
189             ParamUtil.getString(actionRequest, "organizationIds"),  0L);
190         boolean sendEmail = true;
191 
192         if (PropsValues.LOGIN_CREATE_ACCOUNT_ALLOW_CUSTOM_PASSWORD) {
193             autoPassword = false;
194 
195             password1 = ParamUtil.getString(actionRequest, "password1");
196             password2 = ParamUtil.getString(actionRequest, "password2");
197         }
198 
199         String openId = ParamUtil.getString(actionRequest, "openId");
200         boolean openIdAuth = false;
201 
202         Boolean openIdLoginPending = (Boolean)session.getAttribute(
203             WebKeys.OPEN_ID_LOGIN_PENDING);
204 
205         if ((openIdLoginPending != null) &&
206                 (openIdLoginPending.booleanValue()) &&
207                     (Validator.isNotNull(openId))) {
208 
209             sendEmail = false;
210             openIdAuth = true;
211         }
212 
213         if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
214             CaptchaUtil.check(actionRequest);
215         }
216 
217         User user = UserServiceUtil.addUser(
218             company.getCompanyId(), autoPassword, password1, password2,
219             autoScreenName, screenName, emailAddress, themeDisplay.getLocale(),
220             firstName, middleName, lastName, prefixId, suffixId, male,
221             birthdayMonth, birthdayDay, birthdayYear, jobTitle, organizationIds,
222             sendEmail);
223 
224         if (openIdAuth) {
225             UserLocalServiceUtil.updateOpenId(user.getUserId(), openId);
226 
227             session.setAttribute(
228                 WebKeys.OPEN_ID_LOGIN, new Long(user.getUserId()));
229 
230             session.removeAttribute(WebKeys.OPEN_ID_LOGIN_PENDING);
231         }
232         else {
233 
234             // Session messages
235 
236             SessionMessages.add(request, "user_added", user.getEmailAddress());
237             SessionMessages.add(
238                 request, "user_added_password", user.getPasswordUnencrypted());
239         }
240 
241         // Send redirect
242 
243         String login = null;
244 
245         if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
246             login = String.valueOf(user.getUserId());
247         }
248         else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
249             login = user.getScreenName();
250         }
251         else {
252             login = user.getEmailAddress();
253         }
254 
255         sendRedirect(
256             actionRequest, actionResponse, themeDisplay, login,
257             user.getPasswordUnencrypted());
258     }
259 
260     protected boolean isAutoScreenName() {
261         return _AUTO_SCREEN_NAME;
262     }
263 
264     protected void sendRedirect(
265             ActionRequest actionRequest, ActionResponse actionResponse,
266             ThemeDisplay themeDisplay, String login, String password)
267         throws Exception {
268 
269         HttpServletRequest request = PortalUtil.getHttpServletRequest(
270             actionRequest);
271 
272         String redirect = PortalUtil.escapeRedirect(
273             ParamUtil.getString(actionRequest, "redirect"));
274 
275         if (Validator.isNotNull(redirect)) {
276             HttpServletResponse response = PortalUtil.getHttpServletResponse(
277                 actionResponse);
278 
279             LoginAction.login(request, response, login, password, false);
280         }
281         else {
282             redirect = HttpUtil.addParameter(
283                 themeDisplay.getURLSignIn(), "login", login);
284         }
285 
286         actionResponse.sendRedirect(redirect);
287     }
288 
289     protected boolean isCheckMethodOnProcessAction() {
290         return _CHECK_METHOD_ON_PROCESS_ACTION;
291     }
292 
293     private static final boolean _AUTO_SCREEN_NAME = false;
294 
295     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
296 
297 }