1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.myaccount.action;
24  
25  import com.liferay.portal.ContactFirstNameException;
26  import com.liferay.portal.ContactLastNameException;
27  import com.liferay.portal.DuplicateUserEmailAddressException;
28  import com.liferay.portal.DuplicateUserScreenNameException;
29  import com.liferay.portal.NoSuchOrganizationException;
30  import com.liferay.portal.OrganizationParentException;
31  import com.liferay.portal.RequiredUserException;
32  import com.liferay.portal.ReservedUserEmailAddressException;
33  import com.liferay.portal.UserEmailAddressException;
34  import com.liferay.portal.UserIdException;
35  import com.liferay.portal.UserPasswordException;
36  import com.liferay.portal.UserScreenNameException;
37  import com.liferay.portal.UserSmsException;
38  import com.liferay.portal.captcha.CaptchaTextException;
39  import com.liferay.portal.captcha.CaptchaUtil;
40  import com.liferay.portal.kernel.language.LanguageUtil;
41  import com.liferay.portal.kernel.servlet.SessionErrors;
42  import com.liferay.portal.kernel.servlet.SessionMessages;
43  import com.liferay.portal.kernel.util.Constants;
44  import com.liferay.portal.kernel.util.HttpUtil;
45  import com.liferay.portal.kernel.util.ParamUtil;
46  import com.liferay.portal.kernel.util.StringUtil;
47  import com.liferay.portal.kernel.util.Validator;
48  import com.liferay.portal.model.Company;
49  import com.liferay.portal.model.CompanyConstants;
50  import com.liferay.portal.model.User;
51  import com.liferay.portal.security.auth.PrincipalException;
52  import com.liferay.portal.service.UserLocalServiceUtil;
53  import com.liferay.portal.service.UserServiceUtil;
54  import com.liferay.portal.struts.PortletAction;
55  import com.liferay.portal.theme.ThemeDisplay;
56  import com.liferay.portal.util.PortalUtil;
57  import com.liferay.portal.util.PropsValues;
58  import com.liferay.portal.util.WebKeys;
59  
60  import javax.portlet.ActionRequest;
61  import javax.portlet.ActionResponse;
62  import javax.portlet.PortletConfig;
63  import javax.portlet.RenderRequest;
64  import javax.portlet.RenderResponse;
65  
66  import javax.servlet.http.HttpServletRequest;
67  import javax.servlet.http.HttpSession;
68  
69  import org.apache.struts.action.ActionForm;
70  import org.apache.struts.action.ActionForward;
71  import org.apache.struts.action.ActionMapping;
72  
73  /**
74   * <a href="AddUserAction.java.html"><b><i>View Source</i></b></a>
75   *
76   * @author Brian Wing Shun Chan
77   *
78   */
79  public class AddUserAction extends PortletAction {
80  
81      public void processAction(
82              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
83              ActionRequest actionRequest, ActionResponse actionResponse)
84          throws Exception {
85  
86          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
87  
88          try {
89              if (cmd.equals(Constants.ADD)) {
90                  addUser(actionRequest, actionResponse);
91              }
92          }
93          catch (Exception e) {
94              if (e instanceof CaptchaTextException ||
95                  e instanceof ContactFirstNameException ||
96                  e instanceof ContactLastNameException ||
97                  e instanceof DuplicateUserEmailAddressException ||
98                  e instanceof DuplicateUserScreenNameException ||
99                  e instanceof NoSuchOrganizationException ||
100                 e instanceof OrganizationParentException ||
101                 e instanceof RequiredUserException ||
102                 e instanceof ReservedUserEmailAddressException ||
103                 e instanceof UserEmailAddressException ||
104                 e instanceof UserIdException ||
105                 e instanceof UserPasswordException ||
106                 e instanceof UserScreenNameException ||
107                 e instanceof UserSmsException) {
108 
109                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
110             }
111             else {
112                 throw e;
113             }
114         }
115     }
116 
117     public ActionForward render(
118             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
119             RenderRequest renderRequest, RenderResponse renderResponse)
120         throws Exception {
121 
122         Company company = PortalUtil.getCompany(renderRequest);
123 
124         if (!company.isStrangers()) {
125             throw new PrincipalException();
126         }
127 
128         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
129             WebKeys.THEME_DISPLAY);
130 
131         renderResponse.setTitle(
132             LanguageUtil.get(
133                 themeDisplay.getCompanyId(), themeDisplay.getLocale(),
134                 "create-account"));
135 
136         return mapping.findForward("portlet.my_account.create_account");
137     }
138 
139     protected void addUser(
140             ActionRequest actionRequest, ActionResponse actionResponse)
141         throws Exception {
142 
143         HttpServletRequest request = PortalUtil.getHttpServletRequest(
144             actionRequest);
145         HttpSession session = request.getSession();
146 
147         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
148             WebKeys.THEME_DISPLAY);
149 
150         Company company = themeDisplay.getCompany();
151 
152         boolean autoPassword = true;
153         String password1 = null;
154         String password2 = null;
155         boolean autoScreenName = false;
156         String screenName = ParamUtil.getString(actionRequest, "screenName");
157         String emailAddress = ParamUtil.getString(
158             actionRequest, "emailAddress");
159         String firstName = ParamUtil.getString(actionRequest, "firstName");
160         String middleName = ParamUtil.getString(actionRequest, "middleName");
161         String lastName = ParamUtil.getString(actionRequest, "lastName");
162         int prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
163         int suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
164         boolean male = ParamUtil.get(actionRequest, "male", true);
165         int birthdayMonth = ParamUtil.getInteger(
166             actionRequest, "birthdayMonth");
167         int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
168         int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
169         String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
170         long[] organizationIds = StringUtil.split(
171             ParamUtil.getString(actionRequest, "organizationIds"),  0L);
172         boolean sendEmail = true;
173 
174         String openId = ParamUtil.getString(actionRequest, "openId");
175         boolean openIdAuth = false;
176 
177         Boolean openIdLoginPending = (Boolean)session.getAttribute(
178             WebKeys.OPEN_ID_LOGIN_PENDING);
179 
180         if ((openIdLoginPending != null) &&
181                 (openIdLoginPending.booleanValue()) &&
182                     (Validator.isNotNull(openId))) {
183 
184             sendEmail = false;
185             openIdAuth = true;
186         }
187 
188         if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
189             CaptchaUtil.check(actionRequest);
190         }
191 
192         User user = UserServiceUtil.addUser(
193             company.getCompanyId(), autoPassword, password1, password2,
194             autoScreenName, screenName, emailAddress, themeDisplay.getLocale(),
195             firstName, middleName, lastName, prefixId, suffixId, male,
196             birthdayMonth, birthdayDay, birthdayYear, jobTitle, organizationIds,
197             sendEmail);
198 
199         if (openIdAuth) {
200             UserLocalServiceUtil.updateOpenId(user.getUserId(), openId);
201 
202             session.setAttribute(
203                 WebKeys.OPEN_ID_LOGIN, new Long(user.getUserId()));
204 
205             session.removeAttribute(WebKeys.OPEN_ID_LOGIN_PENDING);
206         }
207         else {
208 
209             // Session messages
210 
211             SessionMessages.add(request, "user_added", user.getEmailAddress());
212             SessionMessages.add(
213                 request, "user_added_password", user.getPasswordUnencrypted());
214         }
215 
216         // Send redirect
217 
218         String login = null;
219 
220         if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
221             login = String.valueOf(user.getUserId());
222         }
223         else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
224             login = user.getScreenName();
225         }
226         else {
227             login = user.getEmailAddress();
228         }
229 
230         String redirect = HttpUtil.addParameter(
231             themeDisplay.getURLSignIn(), "login", login);
232 
233         actionResponse.sendRedirect(redirect);
234     }
235 
236     protected boolean isCheckMethodOnProcessAction() {
237         return _CHECK_METHOD_ON_PROCESS_ACTION;
238     }
239 
240     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
241 
242 }