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