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