001    /**
002     * Copyright (c) 2000-2012 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.CompanyMaxUsersException;
018    import com.liferay.portal.ContactFirstNameException;
019    import com.liferay.portal.ContactFullNameException;
020    import com.liferay.portal.ContactLastNameException;
021    import com.liferay.portal.DuplicateUserEmailAddressException;
022    import com.liferay.portal.EmailAddressException;
023    import com.liferay.portal.GroupFriendlyURLException;
024    import com.liferay.portal.ReservedUserEmailAddressException;
025    import com.liferay.portal.UserEmailAddressException;
026    import com.liferay.portal.kernel.captcha.CaptchaTextException;
027    import com.liferay.portal.kernel.captcha.CaptchaUtil;
028    import com.liferay.portal.kernel.json.JSONFactoryUtil;
029    import com.liferay.portal.kernel.json.JSONObject;
030    import com.liferay.portal.kernel.log.Log;
031    import com.liferay.portal.kernel.log.LogFactoryUtil;
032    import com.liferay.portal.kernel.portlet.LiferayWindowState;
033    import com.liferay.portal.kernel.servlet.SessionErrors;
034    import com.liferay.portal.kernel.servlet.SessionMessages;
035    import com.liferay.portal.kernel.util.Constants;
036    import com.liferay.portal.kernel.util.ParamUtil;
037    import com.liferay.portal.kernel.util.StringPool;
038    import com.liferay.portal.kernel.workflow.WorkflowConstants;
039    import com.liferay.portal.model.Company;
040    import com.liferay.portal.model.User;
041    import com.liferay.portal.security.auth.PrincipalException;
042    import com.liferay.portal.service.ServiceContext;
043    import com.liferay.portal.service.ServiceContextFactory;
044    import com.liferay.portal.service.UserLocalServiceUtil;
045    import com.liferay.portal.service.UserServiceUtil;
046    import com.liferay.portal.struts.PortletAction;
047    import com.liferay.portal.theme.ThemeDisplay;
048    import com.liferay.portal.util.PortalUtil;
049    import com.liferay.portal.util.PortletKeys;
050    import com.liferay.portal.util.PropsValues;
051    import com.liferay.portal.util.WebKeys;
052    import com.liferay.portlet.PortletURLFactoryUtil;
053    
054    import javax.portlet.ActionRequest;
055    import javax.portlet.ActionResponse;
056    import javax.portlet.PortletConfig;
057    import javax.portlet.PortletRequest;
058    import javax.portlet.PortletURL;
059    import javax.portlet.RenderRequest;
060    import javax.portlet.RenderResponse;
061    
062    import javax.servlet.http.HttpServletRequest;
063    
064    import org.apache.struts.action.ActionForm;
065    import org.apache.struts.action.ActionForward;
066    import org.apache.struts.action.ActionMapping;
067    
068    /**
069     * @author Sergio González
070     */
071    public class CreateAnonymousAccountAction extends PortletAction {
072    
073            @Override
074            public void processAction(
075                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
076                            ActionRequest actionRequest, ActionResponse actionResponse)
077                    throws Exception {
078    
079                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
080                            WebKeys.THEME_DISPLAY);
081    
082                    Company company = themeDisplay.getCompany();
083    
084                    if (!company.isStrangers()) {
085                            throw new PrincipalException();
086                    }
087    
088                    if (actionRequest.getRemoteUser() != null) {
089                            actionResponse.sendRedirect(themeDisplay.getPathMain());
090    
091                            return;
092                    }
093    
094                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
095    
096                    String emailAddress = ParamUtil.getString(
097                            actionRequest, "emailAddress");
098    
099                    PortletURL portletURL = PortletURLFactoryUtil.create(
100                            actionRequest, PortletKeys.LOGIN, themeDisplay.getPlid(),
101                            PortletRequest.RENDER_PHASE);
102    
103                    portletURL.setWindowState(LiferayWindowState.POP_UP);
104    
105                    portletURL.setParameter("struts_action", "/login/login_redirect");
106                    portletURL.setParameter("emailAddress", emailAddress);
107                    portletURL.setParameter("anonymousUser", Boolean.TRUE.toString());
108    
109                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
110    
111                    try {
112                            if (cmd.equals(Constants.ADD)) {
113                                    addAnonymousUser(actionRequest, actionResponse);
114    
115                                    sendRedirect(
116                                            actionRequest, actionResponse, portletURL.toString());
117                            }
118                            else if (cmd.equals(Constants.UPDATE)) {
119                                    jsonObject = updateIncompleteUser(
120                                            actionRequest, actionResponse);
121    
122                                    writeJSON(actionRequest, actionResponse, jsonObject);
123                            }
124                    }
125                    catch (Exception e) {
126                            if (cmd.equals(Constants.UPDATE)) {
127                                    jsonObject.putException(e);
128    
129                                    writeJSON(actionRequest, actionResponse, jsonObject);
130                            }
131                            else if (e instanceof DuplicateUserEmailAddressException) {
132                                    User user = UserLocalServiceUtil.getUserByEmailAddress(
133                                            themeDisplay.getCompanyId(), emailAddress);
134    
135                                    if (user.getStatus() != WorkflowConstants.STATUS_INCOMPLETE) {
136                                            SessionErrors.add(actionRequest, e.getClass());
137                                    }
138                                    else {
139                                            sendRedirect(
140                                                    actionRequest, actionResponse, portletURL.toString());
141                                    }
142                            }
143                            else if (e instanceof CaptchaTextException ||
144                                             e instanceof CompanyMaxUsersException ||
145                                             e instanceof ContactFirstNameException ||
146                                             e instanceof ContactFullNameException ||
147                                             e instanceof ContactLastNameException ||
148                                             e instanceof EmailAddressException ||
149                                             e instanceof GroupFriendlyURLException ||
150                                             e instanceof ReservedUserEmailAddressException ||
151                                             e instanceof UserEmailAddressException) {
152    
153                                    SessionErrors.add(actionRequest, e.getClass(), e);
154                            }
155                            else {
156                                    _log.error("Unable to create anonymous account", e);
157    
158                                    PortalUtil.sendError(e, actionRequest, actionResponse);
159                            }
160                    }
161            }
162    
163            @Override
164            public ActionForward render(
165                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
166                            RenderRequest renderRequest, RenderResponse renderResponse)
167                    throws Exception {
168    
169                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
170                            WebKeys.THEME_DISPLAY);
171    
172                    Company company = themeDisplay.getCompany();
173    
174                    if (!company.isStrangers()) {
175                            return mapping.findForward("portlet.login.login");
176                    }
177    
178                    renderResponse.setTitle(themeDisplay.translate("anonymous-account"));
179    
180                    return mapping.findForward("portlet.login.create_anonymous_account");
181            }
182    
183            protected void addAnonymousUser(
184                            ActionRequest actionRequest, ActionResponse actionResponse)
185                    throws Exception {
186    
187                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
188                            actionRequest);
189    
190                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
191                            WebKeys.THEME_DISPLAY);
192    
193                    boolean autoPassword = true;
194                    String password1 = null;
195                    String password2 = null;
196                    boolean autoScreenName = true;
197                    String screenName = null;
198                    String emailAddress = ParamUtil.getString(
199                            actionRequest, "emailAddress");
200                    long facebookId = 0;
201                    String openId = StringPool.BLANK;
202                    String firstName = ParamUtil.getString(actionRequest, "firstName");
203                    String lastName = ParamUtil.getString(actionRequest, "lastName");
204                    int prefixId = 0;
205                    int suffixId = 0;
206                    boolean male = true;
207                    int birthdayMonth = 0;
208                    int birthdayDay = 1;
209                    int birthdayYear = 1970;
210                    String jobTitle = null;
211                    long[] groupIds = null;
212                    long[] organizationIds = null;
213                    long[] roleIds = null;
214                    long[] userGroupIds = null;
215                    boolean sendEmail = false;
216    
217                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
218                            User.class.getName(), actionRequest);
219    
220                    serviceContext.setAttribute("anonymousUser", true);
221    
222                    if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
223                            CaptchaUtil.check(actionRequest);
224                    }
225    
226                    User user = UserServiceUtil.addUser(
227                            themeDisplay.getCompanyId(), autoPassword, password1, password2,
228                            autoScreenName, screenName, emailAddress, facebookId, openId,
229                            themeDisplay.getLocale(), firstName, null, lastName, prefixId,
230                            suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
231                            groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
232                            serviceContext);
233    
234                    UserLocalServiceUtil.updateStatus(
235                            user.getUserId(), WorkflowConstants.STATUS_INCOMPLETE);
236    
237                    // Session messages
238    
239                    SessionMessages.add(request, "user_added", user.getEmailAddress());
240                    SessionMessages.add(
241                            request, "user_added_password", user.getPasswordUnencrypted());
242            }
243    
244            @Override
245            protected void addSuccessMessage(
246                    ActionRequest actionRequest, ActionResponse actionResponse) {
247    
248                    String portletId = (String)actionRequest.getAttribute(
249                            WebKeys.PORTLET_ID);
250    
251                    if (!portletId.equals(PortletKeys.FAST_LOGIN)) {
252                            super.addSuccessMessage(actionRequest, actionResponse);
253                    }
254            }
255    
256            @Override
257            protected boolean isCheckMethodOnProcessAction() {
258                    return _CHECK_METHOD_ON_PROCESS_ACTION;
259            }
260    
261            protected JSONObject updateIncompleteUser(
262                            ActionRequest actionRequest, ActionResponse actionResponse)
263                    throws Exception {
264    
265                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
266                            WebKeys.THEME_DISPLAY);
267    
268                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
269                            User.class.getName(), actionRequest);
270    
271                    boolean autoPassword = true;
272                    String password1 = null;
273                    String password2 = null;
274                    boolean autoScreenName = false;
275                    String screenName = null;
276                    String emailAddress = ParamUtil.getString(
277                            actionRequest, "emailAddress");
278                    long facebookId = 0;
279                    String openId = null;
280                    String firstName = null;
281                    String middleName = null;
282                    String lastName = null;
283                    int prefixId = 0;
284                    int suffixId = 0;
285                    boolean male = true;
286                    int birthdayMonth = 0;
287                    int birthdayDay = 1;
288                    int birthdayYear = 1970;
289                    String jobTitle = null;
290                    boolean updateUserInformation = false;
291                    boolean sendEmail = true;
292    
293                    User user = UserServiceUtil.updateIncompleteUser(
294                            themeDisplay.getCompanyId(), autoPassword, password1, password2,
295                            autoScreenName, screenName, emailAddress, facebookId, openId,
296                            themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
297                            suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
298                            updateUserInformation, sendEmail, serviceContext);
299    
300                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
301    
302                    if (user.getStatus() == WorkflowConstants.STATUS_APPROVED) {
303                            jsonObject.put("userStatus", "user_added");
304                    }
305                    else {
306                            jsonObject.put("userStatus", "user_pending");
307                    }
308    
309                    return jsonObject;
310            }
311    
312            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
313    
314            private static Log _log = LogFactoryUtil.getLog(
315                    CreateAnonymousAccountAction.class);
316    
317    }