1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.login.action;
21  
22  import com.liferay.portal.ContactFirstNameException;
23  import com.liferay.portal.ContactLastNameException;
24  import com.liferay.portal.DuplicateUserEmailAddressException;
25  import com.liferay.portal.DuplicateUserScreenNameException;
26  import com.liferay.portal.NoSuchLayoutException;
27  import com.liferay.portal.NoSuchOrganizationException;
28  import com.liferay.portal.OrganizationParentException;
29  import com.liferay.portal.RequiredUserException;
30  import com.liferay.portal.ReservedUserEmailAddressException;
31  import com.liferay.portal.ReservedUserScreenNameException;
32  import com.liferay.portal.UserEmailAddressException;
33  import com.liferay.portal.UserIdException;
34  import com.liferay.portal.UserPasswordException;
35  import com.liferay.portal.UserScreenNameException;
36  import com.liferay.portal.UserSmsException;
37  import com.liferay.portal.kernel.captcha.CaptchaTextException;
38  import com.liferay.portal.kernel.captcha.CaptchaUtil;
39  import com.liferay.portal.kernel.language.LanguageUtil;
40  import com.liferay.portal.kernel.servlet.SessionErrors;
41  import com.liferay.portal.kernel.servlet.SessionMessages;
42  import com.liferay.portal.kernel.util.Constants;
43  import com.liferay.portal.kernel.util.ParamUtil;
44  import com.liferay.portal.kernel.util.Validator;
45  import com.liferay.portal.model.Company;
46  import com.liferay.portal.model.CompanyConstants;
47  import com.liferay.portal.model.Layout;
48  import com.liferay.portal.model.User;
49  import com.liferay.portal.security.auth.PrincipalException;
50  import com.liferay.portal.service.LayoutLocalServiceUtil;
51  import com.liferay.portal.service.ServiceContext;
52  import com.liferay.portal.service.ServiceContextFactory;
53  import com.liferay.portal.service.UserServiceUtil;
54  import com.liferay.portal.struts.PortletAction;
55  import com.liferay.portal.theme.ThemeDisplay;
56  import com.liferay.portal.util.PortalUtil;
57  import com.liferay.portal.util.PropsValues;
58  import com.liferay.portal.util.WebKeys;
59  import com.liferay.portlet.login.util.LoginUtil;
60  
61  import javax.portlet.ActionRequest;
62  import javax.portlet.ActionResponse;
63  import javax.portlet.PortletConfig;
64  import javax.portlet.PortletURL;
65  import javax.portlet.RenderRequest;
66  import javax.portlet.RenderResponse;
67  
68  import javax.servlet.http.HttpServletRequest;
69  import javax.servlet.http.HttpSession;
70  
71  import org.apache.struts.action.ActionForm;
72  import org.apache.struts.action.ActionForward;
73  import org.apache.struts.action.ActionMapping;
74  
75  /**
76   * <a href="CreateAccountAction.java.html"><b><i>View Source</i></b></a>
77   *
78   * @author Brian Wing Shun Chan
79   *
80   */
81  public class CreateAccountAction extends PortletAction {
82  
83      public void processAction(
84              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
85              ActionRequest actionRequest, ActionResponse actionResponse)
86          throws Exception {
87  
88          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
89  
90          try {
91              if (cmd.equals(Constants.ADD)) {
92                  addUser(actionRequest, actionResponse);
93              }
94          }
95          catch (Exception e) {
96              if (e instanceof CaptchaTextException ||
97                  e instanceof ContactFirstNameException ||
98                  e instanceof ContactLastNameException ||
99                  e instanceof DuplicateUserEmailAddressException ||
100                 e instanceof DuplicateUserScreenNameException ||
101                 e instanceof NoSuchOrganizationException ||
102                 e instanceof OrganizationParentException ||
103                 e instanceof RequiredUserException ||
104                 e instanceof ReservedUserEmailAddressException ||
105                 e instanceof ReservedUserScreenNameException ||
106                 e instanceof UserEmailAddressException ||
107                 e instanceof UserIdException ||
108                 e instanceof UserPasswordException ||
109                 e instanceof UserScreenNameException ||
110                 e instanceof UserSmsException) {
111 
112                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
113             }
114             else {
115                 throw e;
116             }
117         }
118 
119         if (Validator.isNull(PropsValues.COMPANY_SECURITY_STRANGERS_URL)) {
120             return;
121         }
122 
123         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
124             WebKeys.THEME_DISPLAY);
125 
126         try {
127             Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
128                 themeDisplay.getScopeGroupId(), false,
129                 PropsValues.COMPANY_SECURITY_STRANGERS_URL);
130 
131             String redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
132 
133             sendRedirect(actionRequest, actionResponse, redirect);
134         }
135         catch (NoSuchLayoutException nsle) {
136         }
137     }
138 
139     public ActionForward render(
140             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
141             RenderRequest renderRequest, RenderResponse renderResponse)
142         throws Exception {
143 
144         Company company = PortalUtil.getCompany(renderRequest);
145 
146         if (!company.isStrangers()) {
147             throw new PrincipalException();
148         }
149 
150         ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
151             WebKeys.THEME_DISPLAY);
152 
153         renderResponse.setTitle(
154             LanguageUtil.get(
155                 themeDisplay.getCompanyId(), themeDisplay.getLocale(),
156                 "create-account"));
157 
158         return mapping.findForward("portlet.login.create_account");
159     }
160 
161     protected void addUser(
162             ActionRequest actionRequest, ActionResponse actionResponse)
163         throws Exception {
164 
165         HttpServletRequest request = PortalUtil.getHttpServletRequest(
166             actionRequest);
167         HttpSession session = request.getSession();
168 
169         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
170             WebKeys.THEME_DISPLAY);
171 
172         Company company = themeDisplay.getCompany();
173 
174         boolean autoPassword = true;
175         String password1 = null;
176         String password2 = null;
177         boolean autoScreenName = false;
178         String screenName = ParamUtil.getString(actionRequest, "screenName");
179         String emailAddress = ParamUtil.getString(
180             actionRequest, "emailAddress");
181         String openId = ParamUtil.getString(actionRequest, "openId");
182         String firstName = ParamUtil.getString(actionRequest, "firstName");
183         String middleName = ParamUtil.getString(actionRequest, "middleName");
184         String lastName = ParamUtil.getString(actionRequest, "lastName");
185         int prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
186         int suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
187         boolean male = ParamUtil.get(actionRequest, "male", true);
188         int birthdayMonth = ParamUtil.getInteger(
189             actionRequest, "birthdayMonth");
190         int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
191         int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
192         String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
193         long[] groupIds = null;
194         long[] organizationIds = null;
195         long[] roleIds = null;
196         long[] userGroupIds = null;
197         boolean sendEmail = true;
198 
199         ServiceContext serviceContext = ServiceContextFactory.getInstance(
200             User.class.getName(), actionRequest);
201 
202         if (PropsValues.LOGIN_CREATE_ACCOUNT_ALLOW_CUSTOM_PASSWORD) {
203             autoPassword = false;
204 
205             password1 = ParamUtil.getString(actionRequest, "password1");
206             password2 = ParamUtil.getString(actionRequest, "password2");
207         }
208 
209         boolean openIdPending = false;
210 
211         Boolean openIdLoginPending = (Boolean)session.getAttribute(
212             WebKeys.OPEN_ID_LOGIN_PENDING);
213 
214         if ((openIdLoginPending != null) &&
215             (openIdLoginPending.booleanValue()) &&
216             (Validator.isNotNull(openId))) {
217 
218             sendEmail = false;
219             openIdPending = true;
220         }
221 
222         if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
223             CaptchaUtil.check(actionRequest);
224         }
225 
226         User user = UserServiceUtil.addUser(
227             company.getCompanyId(), autoPassword, password1, password2,
228             autoScreenName, screenName, emailAddress, openId,
229             themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
230             suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
231             groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
232             serviceContext);
233 
234         if (openIdPending) {
235             session.setAttribute(
236                 WebKeys.OPEN_ID_LOGIN, new Long(user.getUserId()));
237 
238             session.removeAttribute(WebKeys.OPEN_ID_LOGIN_PENDING);
239         }
240         else {
241 
242             // Session messages
243 
244             SessionMessages.add(request, "user_added", user.getEmailAddress());
245             SessionMessages.add(
246                 request, "user_added_password", user.getPasswordUnencrypted());
247         }
248 
249         // Send redirect
250 
251         String login = null;
252 
253         if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
254             login = String.valueOf(user.getUserId());
255         }
256         else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
257             login = user.getScreenName();
258         }
259         else {
260             login = user.getEmailAddress();
261         }
262 
263         PortletURL loginURL = LoginUtil.getLoginURL(
264             request, themeDisplay.getPlid());
265 
266         loginURL.setParameter("login", login);
267 
268         String redirect = loginURL.toString();
269 
270         actionResponse.sendRedirect(redirect);
271     }
272 
273     protected boolean isCheckMethodOnProcessAction() {
274         return _CHECK_METHOD_ON_PROCESS_ACTION;
275     }
276 
277     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
278 
279 }