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.util.Constants;
42  import com.liferay.portal.kernel.util.ParamUtil;
43  import com.liferay.portal.kernel.util.StringUtil;
44  import com.liferay.portal.model.Company;
45  import com.liferay.portal.model.User;
46  import com.liferay.portal.model.impl.CompanyImpl;
47  import com.liferay.portal.security.auth.PrincipalException;
48  import com.liferay.portal.service.UserServiceUtil;
49  import com.liferay.portal.struts.PortletAction;
50  import com.liferay.portal.theme.ThemeDisplay;
51  import com.liferay.portal.util.PortalUtil;
52  import com.liferay.portal.util.WebKeys;
53  import com.liferay.util.servlet.SessionErrors;
54  import com.liferay.util.servlet.SessionMessages;
55  
56  import javax.portlet.ActionRequest;
57  import javax.portlet.ActionResponse;
58  import javax.portlet.PortletConfig;
59  import javax.portlet.RenderRequest;
60  import javax.portlet.RenderResponse;
61  
62  import javax.servlet.http.HttpServletRequest;
63  
64  import org.apache.struts.action.ActionForm;
65  import org.apache.struts.action.ActionForward;
66  import org.apache.struts.action.ActionMapping;
67  
68  /**
69   * <a href="AddUserAction.java.html"><b><i>View Source</i></b></a>
70   *
71   * @author Brian Wing Shun Chan
72   *
73   */
74  public class AddUserAction extends PortletAction {
75  
76      public void processAction(
77              ActionMapping mapping, ActionForm form, PortletConfig config,
78              ActionRequest req, ActionResponse res)
79          throws Exception {
80  
81          String cmd = ParamUtil.getString(req, Constants.CMD);
82  
83          try {
84              if (cmd.equals(Constants.ADD)) {
85                  addUser(req, res);
86              }
87          }
88          catch (Exception e) {
89              if (e instanceof CaptchaTextException ||
90                  e instanceof ContactFirstNameException ||
91                  e instanceof ContactLastNameException ||
92                  e instanceof DuplicateUserEmailAddressException ||
93                  e instanceof DuplicateUserScreenNameException ||
94                  e instanceof NoSuchOrganizationException ||
95                  e instanceof OrganizationParentException ||
96                  e instanceof RequiredUserException ||
97                  e instanceof ReservedUserEmailAddressException ||
98                  e instanceof UserEmailAddressException ||
99                  e instanceof UserIdException ||
100                 e instanceof UserPasswordException ||
101                 e instanceof UserScreenNameException ||
102                 e instanceof UserSmsException) {
103 
104                 SessionErrors.add(req, e.getClass().getName(), e);
105             }
106             else {
107                 throw e;
108             }
109         }
110     }
111 
112     public ActionForward render(
113             ActionMapping mapping, ActionForm form, PortletConfig config,
114             RenderRequest req, RenderResponse res)
115         throws Exception {
116 
117         Company company = PortalUtil.getCompany(req);
118 
119         if (!company.isStrangers()) {
120             throw new PrincipalException();
121         }
122 
123         ThemeDisplay themeDisplay =
124             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
125 
126         res.setTitle(
127             LanguageUtil.get(
128                 themeDisplay.getCompanyId(), themeDisplay.getLocale(),
129                 "create-account"));
130 
131         return mapping.findForward("portlet.my_account.create_account");
132     }
133 
134     protected void addUser(ActionRequest req, ActionResponse res)
135         throws Exception {
136 
137         ThemeDisplay themeDisplay =
138             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
139 
140         Company company = themeDisplay.getCompany();
141 
142         boolean autoPassword = true;
143         String password1 = null;
144         String password2 = null;
145         boolean autoScreenName = false;
146         String screenName = ParamUtil.getString(req, "screenName");
147         String emailAddress = ParamUtil.getString(req, "emailAddress");
148         String firstName = ParamUtil.getString(req, "firstName");
149         String middleName = ParamUtil.getString(req, "middleName");
150         String lastName = ParamUtil.getString(req, "lastName");
151         int prefixId = ParamUtil.getInteger(req, "prefixId");
152         int suffixId = ParamUtil.getInteger(req, "suffixId");
153         boolean male = ParamUtil.get(req, "male", true);
154         int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
155         int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
156         int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
157         String jobTitle = ParamUtil.getString(req, "jobTitle");
158         long[] organizationIds = StringUtil.split(
159             ParamUtil.getString(req, "organizationIds"),  0L);
160         boolean sendEmail = true;
161 
162         CaptchaUtil.check(req);
163 
164         User user = UserServiceUtil.addUser(
165             company.getCompanyId(), autoPassword, password1, password2,
166             autoScreenName, screenName, emailAddress, themeDisplay.getLocale(),
167             firstName, middleName, lastName, prefixId, suffixId, male,
168             birthdayMonth, birthdayDay, birthdayYear, jobTitle, organizationIds,
169             sendEmail);
170 
171         // Session messages
172 
173         HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
174 
175         SessionMessages.add(httpReq, "user_added", user.getEmailAddress());
176         SessionMessages.add(
177             httpReq, "user_added_password", user.getPasswordUnencrypted());
178 
179         // Send redirect
180 
181         String redirect = themeDisplay.getPathMain() + "/portal/login?login=";
182 
183         if (company.getAuthType().equals(CompanyImpl.AUTH_TYPE_ID)) {
184             redirect += user.getUserId();
185         }
186         else if (company.getAuthType().equals(CompanyImpl.AUTH_TYPE_SN)) {
187             redirect += user.getScreenName();
188         }
189         else {
190             redirect += user.getEmailAddress();
191         }
192 
193         res.sendRedirect(redirect);
194     }
195 
196     protected boolean isCheckMethodOnProcessAction() {
197         return _CHECK_METHOD_ON_PROCESS_ACTION;
198     }
199 
200     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
201 
202 }