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