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