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