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