1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.servlet.SessionErrors;
43  import com.liferay.portal.kernel.servlet.SessionMessages;
44  import com.liferay.portal.kernel.util.Constants;
45  import com.liferay.portal.kernel.util.ParamUtil;
46  import com.liferay.portal.kernel.util.Validator;
47  import com.liferay.portal.model.Company;
48  import com.liferay.portal.model.CompanyConstants;
49  import com.liferay.portal.model.Layout;
50  import com.liferay.portal.model.User;
51  import com.liferay.portal.security.auth.PrincipalException;
52  import com.liferay.portal.service.LayoutLocalServiceUtil;
53  import com.liferay.portal.service.ServiceContext;
54  import com.liferay.portal.service.ServiceContextFactory;
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  public class CreateAccountAction extends PortletAction {
83  
84      public void processAction(
85              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
86              ActionRequest actionRequest, ActionResponse actionResponse)
87          throws Exception {
88  
89          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
90  
91          try {
92              if (cmd.equals(Constants.ADD)) {
93                  addUser(actionRequest, actionResponse);
94              }
95          }
96          catch (Exception e) {
97              if (e instanceof CaptchaTextException ||
98                  e instanceof ContactFirstNameException ||
99                  e instanceof ContactLastNameException ||
100                 e instanceof DuplicateUserEmailAddressException ||
101                 e instanceof DuplicateUserScreenNameException ||
102                 e instanceof NoSuchOrganizationException ||
103                 e instanceof OrganizationParentException ||
104                 e instanceof RequiredUserException ||
105                 e instanceof ReservedUserEmailAddressException ||
106                 e instanceof ReservedUserScreenNameException ||
107                 e instanceof UserEmailAddressException ||
108                 e instanceof UserIdException ||
109                 e instanceof UserPasswordException ||
110                 e instanceof UserScreenNameException ||
111                 e instanceof UserSmsException) {
112 
113                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
114             }
115             else {
116                 throw e;
117             }
118         }
119 
120         if (Validator.isNull(PropsValues.COMPANY_SECURITY_STRANGERS_URL)) {
121             return;
122         }
123 
124         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
125             WebKeys.THEME_DISPLAY);
126 
127         try {
128             Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
129                 themeDisplay.getScopeGroupId(), false,
130                 PropsValues.COMPANY_SECURITY_STRANGERS_URL);
131 
132             String redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
133 
134             sendRedirect(actionRequest, actionResponse, redirect);
135         }
136         catch (NoSuchLayoutException nsle) {
137         }
138     }
139 
140     public ActionForward render(
141             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
142             RenderRequest renderRequest, RenderResponse renderResponse)
143         throws Exception {
144 
145         Company company = PortalUtil.getCompany(renderRequest);
146 
147         if (!company.isStrangers()) {
148             throw new PrincipalException();
149         }
150 
151         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
152             WebKeys.THEME_DISPLAY);
153 
154         renderResponse.setTitle(themeDisplay.translate("create-account"));
155 
156         return mapping.findForward("portlet.login.create_account");
157     }
158 
159     protected void addUser(
160             ActionRequest actionRequest, ActionResponse actionResponse)
161         throws Exception {
162 
163         HttpServletRequest request = PortalUtil.getHttpServletRequest(
164             actionRequest);
165         HttpSession session = request.getSession();
166 
167         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
168             WebKeys.THEME_DISPLAY);
169 
170         Company company = themeDisplay.getCompany();
171 
172         boolean autoPassword = true;
173         String password1 = null;
174         String password2 = null;
175         boolean autoScreenName = false;
176         String screenName = ParamUtil.getString(actionRequest, "screenName");
177         String emailAddress = ParamUtil.getString(
178             actionRequest, "emailAddress");
179         String openId = ParamUtil.getString(actionRequest, "openId");
180         String firstName = ParamUtil.getString(actionRequest, "firstName");
181         String middleName = ParamUtil.getString(actionRequest, "middleName");
182         String lastName = ParamUtil.getString(actionRequest, "lastName");
183         int prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
184         int suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
185         boolean male = ParamUtil.get(actionRequest, "male", true);
186         int birthdayMonth = ParamUtil.getInteger(
187             actionRequest, "birthdayMonth");
188         int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
189         int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
190         String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
191         long[] groupIds = null;
192         long[] organizationIds = null;
193         long[] roleIds = null;
194         long[] userGroupIds = null;
195         boolean sendEmail = true;
196 
197         ServiceContext serviceContext = ServiceContextFactory.getInstance(
198             User.class.getName(), actionRequest);
199 
200         if (PropsValues.LOGIN_CREATE_ACCOUNT_ALLOW_CUSTOM_PASSWORD) {
201             autoPassword = false;
202 
203             password1 = ParamUtil.getString(actionRequest, "password1");
204             password2 = ParamUtil.getString(actionRequest, "password2");
205         }
206 
207         boolean openIdPending = false;
208 
209         Boolean openIdLoginPending = (Boolean)session.getAttribute(
210             WebKeys.OPEN_ID_LOGIN_PENDING);
211 
212         if ((openIdLoginPending != null) &&
213             (openIdLoginPending.booleanValue()) &&
214             (Validator.isNotNull(openId))) {
215 
216             sendEmail = false;
217             openIdPending = true;
218         }
219 
220         if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
221             CaptchaUtil.check(actionRequest);
222         }
223 
224         User user = UserServiceUtil.addUser(
225             company.getCompanyId(), autoPassword, password1, password2,
226             autoScreenName, screenName, emailAddress, openId,
227             themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
228             suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
229             groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
230             serviceContext);
231 
232         if (openIdPending) {
233             session.setAttribute(
234                 WebKeys.OPEN_ID_LOGIN, new Long(user.getUserId()));
235 
236             session.removeAttribute(WebKeys.OPEN_ID_LOGIN_PENDING);
237         }
238         else {
239 
240             // Session messages
241 
242             SessionMessages.add(request, "user_added", user.getEmailAddress());
243             SessionMessages.add(
244                 request, "user_added_password", user.getPasswordUnencrypted());
245         }
246 
247         // Send redirect
248 
249         String login = null;
250 
251         if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
252             login = String.valueOf(user.getUserId());
253         }
254         else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
255             login = user.getScreenName();
256         }
257         else {
258             login = user.getEmailAddress();
259         }
260 
261         PortletURL loginURL = LoginUtil.getLoginURL(
262             request, themeDisplay.getPlid());
263 
264         loginURL.setParameter("login", login);
265 
266         String redirect = loginURL.toString();
267 
268         actionResponse.sendRedirect(redirect);
269     }
270 
271     protected boolean isCheckMethodOnProcessAction() {
272         return _CHECK_METHOD_ON_PROCESS_ACTION;
273     }
274 
275     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
276 
277 }