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