001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.login.action;
016    
017    import com.liferay.portal.AddressCityException;
018    import com.liferay.portal.AddressStreetException;
019    import com.liferay.portal.AddressZipException;
020    import com.liferay.portal.CompanyMaxUsersException;
021    import com.liferay.portal.ContactFirstNameException;
022    import com.liferay.portal.ContactFullNameException;
023    import com.liferay.portal.ContactLastNameException;
024    import com.liferay.portal.DuplicateUserEmailAddressException;
025    import com.liferay.portal.DuplicateUserScreenNameException;
026    import com.liferay.portal.EmailAddressException;
027    import com.liferay.portal.NoSuchCountryException;
028    import com.liferay.portal.NoSuchLayoutException;
029    import com.liferay.portal.NoSuchListTypeException;
030    import com.liferay.portal.NoSuchOrganizationException;
031    import com.liferay.portal.NoSuchRegionException;
032    import com.liferay.portal.OrganizationParentException;
033    import com.liferay.portal.PhoneNumberException;
034    import com.liferay.portal.RequiredFieldException;
035    import com.liferay.portal.RequiredUserException;
036    import com.liferay.portal.ReservedUserEmailAddressException;
037    import com.liferay.portal.ReservedUserScreenNameException;
038    import com.liferay.portal.TermsOfUseException;
039    import com.liferay.portal.UserEmailAddressException;
040    import com.liferay.portal.UserIdException;
041    import com.liferay.portal.UserPasswordException;
042    import com.liferay.portal.UserScreenNameException;
043    import com.liferay.portal.UserSmsException;
044    import com.liferay.portal.WebsiteURLException;
045    import com.liferay.portal.kernel.captcha.CaptchaTextException;
046    import com.liferay.portal.kernel.captcha.CaptchaUtil;
047    import com.liferay.portal.kernel.servlet.SessionErrors;
048    import com.liferay.portal.kernel.servlet.SessionMessages;
049    import com.liferay.portal.kernel.util.Constants;
050    import com.liferay.portal.kernel.util.ParamUtil;
051    import com.liferay.portal.kernel.util.Validator;
052    import com.liferay.portal.model.Company;
053    import com.liferay.portal.model.CompanyConstants;
054    import com.liferay.portal.model.Layout;
055    import com.liferay.portal.model.User;
056    import com.liferay.portal.security.auth.PrincipalException;
057    import com.liferay.portal.service.LayoutLocalServiceUtil;
058    import com.liferay.portal.service.ServiceContext;
059    import com.liferay.portal.service.ServiceContextFactory;
060    import com.liferay.portal.service.UserServiceUtil;
061    import com.liferay.portal.struts.PortletAction;
062    import com.liferay.portal.theme.ThemeDisplay;
063    import com.liferay.portal.util.PortalUtil;
064    import com.liferay.portal.util.PropsValues;
065    import com.liferay.portal.util.WebKeys;
066    import com.liferay.portlet.login.util.LoginUtil;
067    
068    import javax.portlet.ActionRequest;
069    import javax.portlet.ActionResponse;
070    import javax.portlet.PortletConfig;
071    import javax.portlet.PortletURL;
072    import javax.portlet.RenderRequest;
073    import javax.portlet.RenderResponse;
074    
075    import javax.servlet.http.HttpServletRequest;
076    import javax.servlet.http.HttpServletResponse;
077    import javax.servlet.http.HttpSession;
078    
079    import org.apache.struts.action.ActionForm;
080    import org.apache.struts.action.ActionForward;
081    import org.apache.struts.action.ActionMapping;
082    
083    /**
084     * @author Brian Wing Shun Chan
085     * @author Amos Fong
086     */
087    public class CreateAccountAction extends PortletAction {
088    
089            public void processAction(
090                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
091                            ActionRequest actionRequest, ActionResponse actionResponse)
092                    throws Exception {
093    
094                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
095    
096                    try {
097                            if (cmd.equals(Constants.ADD)) {
098                                    addUser(actionRequest, actionResponse);
099                            }
100                    }
101                    catch (Exception e) {
102                            if (e instanceof AddressCityException ||
103                                    e instanceof AddressStreetException ||
104                                    e instanceof AddressZipException ||
105                                    e instanceof CaptchaTextException ||
106                                    e instanceof CompanyMaxUsersException ||
107                                    e instanceof ContactFirstNameException ||
108                                    e instanceof ContactFullNameException ||
109                                    e instanceof ContactLastNameException ||
110                                    e instanceof DuplicateUserEmailAddressException ||
111                                    e instanceof DuplicateUserScreenNameException ||
112                                    e instanceof EmailAddressException ||
113                                    e instanceof NoSuchCountryException ||
114                                    e instanceof NoSuchListTypeException ||
115                                    e instanceof NoSuchOrganizationException ||
116                                    e instanceof NoSuchRegionException ||
117                                    e instanceof OrganizationParentException ||
118                                    e instanceof PhoneNumberException ||
119                                    e instanceof RequiredFieldException ||
120                                    e instanceof RequiredUserException ||
121                                    e instanceof ReservedUserEmailAddressException ||
122                                    e instanceof ReservedUserScreenNameException ||
123                                    e instanceof TermsOfUseException ||
124                                    e instanceof UserEmailAddressException ||
125                                    e instanceof UserIdException ||
126                                    e instanceof UserPasswordException ||
127                                    e instanceof UserScreenNameException ||
128                                    e instanceof UserSmsException ||
129                                    e instanceof WebsiteURLException) {
130    
131                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
132                            }
133                            else {
134                                    throw e;
135                            }
136                    }
137    
138                    if (Validator.isNull(PropsValues.COMPANY_SECURITY_STRANGERS_URL)) {
139                            return;
140                    }
141    
142                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
143                            WebKeys.THEME_DISPLAY);
144    
145                    try {
146                            Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(
147                                    themeDisplay.getScopeGroupId(), false,
148                                    PropsValues.COMPANY_SECURITY_STRANGERS_URL);
149    
150                            String redirect = PortalUtil.getLayoutURL(layout, themeDisplay);
151    
152                            sendRedirect(actionRequest, actionResponse, redirect);
153                    }
154                    catch (NoSuchLayoutException nsle) {
155                    }
156            }
157    
158            public ActionForward render(
159                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
160                            RenderRequest renderRequest, RenderResponse renderResponse)
161                    throws Exception {
162    
163                    Company company = PortalUtil.getCompany(renderRequest);
164    
165                    if (!company.isStrangers()) {
166                            throw new PrincipalException();
167                    }
168    
169                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
170                            WebKeys.THEME_DISPLAY);
171    
172                    renderResponse.setTitle(themeDisplay.translate("create-account"));
173    
174                    return mapping.findForward("portlet.login.create_account");
175            }
176    
177            protected void addUser(
178                            ActionRequest actionRequest, ActionResponse actionResponse)
179                    throws Exception {
180    
181                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
182                            actionRequest);
183                    HttpSession session = request.getSession();
184    
185                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
186                            WebKeys.THEME_DISPLAY);
187    
188                    Company company = themeDisplay.getCompany();
189    
190                    boolean autoPassword = true;
191                    String password1 = null;
192                    String password2 = null;
193                    boolean autoScreenName = isAutoScreenName();
194                    String screenName = ParamUtil.getString(actionRequest, "screenName");
195                    String emailAddress = ParamUtil.getString(
196                            actionRequest, "emailAddress");
197                    long facebookId = ParamUtil.getLong(actionRequest, "facebookId");
198                    String openId = ParamUtil.getString(actionRequest, "openId");
199                    String firstName = ParamUtil.getString(actionRequest, "firstName");
200                    String middleName = ParamUtil.getString(actionRequest, "middleName");
201                    String lastName = ParamUtil.getString(actionRequest, "lastName");
202                    int prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
203                    int suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
204                    boolean male = ParamUtil.get(actionRequest, "male", true);
205                    int birthdayMonth = ParamUtil.getInteger(
206                            actionRequest, "birthdayMonth");
207                    int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
208                    int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
209                    String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
210                    long[] groupIds = null;
211                    long[] organizationIds = null;
212                    long[] roleIds = null;
213                    long[] userGroupIds = null;
214                    boolean sendEmail = true;
215    
216                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
217                            User.class.getName(), actionRequest);
218    
219                    if (PropsValues.LOGIN_CREATE_ACCOUNT_ALLOW_CUSTOM_PASSWORD) {
220                            autoPassword = false;
221    
222                            password1 = ParamUtil.getString(actionRequest, "password1");
223                            password2 = ParamUtil.getString(actionRequest, "password2");
224                    }
225    
226                    boolean openIdPending = false;
227    
228                    Boolean openIdLoginPending = (Boolean)session.getAttribute(
229                            WebKeys.OPEN_ID_LOGIN_PENDING);
230    
231                    if ((openIdLoginPending != null) &&
232                            (openIdLoginPending.booleanValue()) &&
233                            (Validator.isNotNull(openId))) {
234    
235                            sendEmail = false;
236                            openIdPending = true;
237                    }
238    
239                    if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
240                            CaptchaUtil.check(actionRequest);
241                    }
242    
243                    User user = UserServiceUtil.addUser(
244                            company.getCompanyId(), autoPassword, password1, password2,
245                            autoScreenName, screenName, emailAddress, facebookId, openId,
246                            themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
247                            suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
248                            groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
249                            serviceContext);
250    
251                    if (openIdPending) {
252                            session.setAttribute(
253                                    WebKeys.OPEN_ID_LOGIN, new Long(user.getUserId()));
254    
255                            session.removeAttribute(WebKeys.OPEN_ID_LOGIN_PENDING);
256                    }
257                    else {
258    
259                            // Session messages
260    
261                            SessionMessages.add(request, "user_added", user.getEmailAddress());
262                            SessionMessages.add(
263                                    request, "user_added_password", user.getPasswordUnencrypted());
264                    }
265    
266                    // Send redirect
267    
268                    String login = null;
269    
270                    if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
271                            login = String.valueOf(user.getUserId());
272                    }
273                    else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
274                            login = user.getScreenName();
275                    }
276                    else {
277                            login = user.getEmailAddress();
278                    }
279    
280                    sendRedirect(
281                            actionRequest, actionResponse, themeDisplay, login,
282                            user.getPasswordUnencrypted());
283            }
284    
285            protected boolean isAutoScreenName() {
286                    return _AUTO_SCREEN_NAME;
287            }
288    
289            protected void sendRedirect(
290                            ActionRequest actionRequest, ActionResponse actionResponse,
291                            ThemeDisplay themeDisplay, String login, String password)
292                    throws Exception {
293    
294                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
295                            actionRequest);
296    
297                    String redirect = PortalUtil.escapeRedirect(
298                            ParamUtil.getString(actionRequest, "redirect"));
299    
300                    if (Validator.isNotNull(redirect)) {
301                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
302                                    actionResponse);
303    
304                            LoginUtil.login(request, response, login, password, false, null);
305                    }
306                    else {
307                            PortletURL loginURL = LoginUtil.getLoginURL(
308                                    request, themeDisplay.getPlid());
309    
310                            loginURL.setParameter("login", login);
311    
312                            redirect = loginURL.toString();
313                    }
314    
315                    actionResponse.sendRedirect(redirect);
316            }
317    
318            protected boolean isCheckMethodOnProcessAction() {
319                    return _CHECK_METHOD_ON_PROCESS_ACTION;
320            }
321    
322            private static final boolean _AUTO_SCREEN_NAME = false;
323    
324            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
325    
326    }