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