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