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.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 mapping, ActionForm form, PortletConfig portletConfig,
075                            ActionRequest actionRequest, ActionResponse actionResponse)
076                    throws Exception {
077    
078                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
079                            WebKeys.THEME_DISPLAY);
080    
081                    String portletName = portletConfig.getPortletName();
082    
083                    if (!portletName.equals(PortletKeys.FAST_LOGIN)) {
084                            throw new PrincipalException();
085                    }
086    
087                    if (actionRequest.getRemoteUser() != null) {
088                            actionResponse.sendRedirect(themeDisplay.getPathMain());
089    
090                            return;
091                    }
092    
093                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
094    
095                    String emailAddress = ParamUtil.getString(
096                            actionRequest, "emailAddress");
097    
098                    PortletURL portletURL = PortletURLFactoryUtil.create(
099                            actionRequest, PortletKeys.LOGIN, themeDisplay.getPlid(),
100                            PortletRequest.RENDER_PHASE);
101    
102                    portletURL.setWindowState(LiferayWindowState.POP_UP);
103    
104                    portletURL.setParameter("struts_action", "/login/login_redirect");
105                    portletURL.setParameter("emailAddress", emailAddress);
106                    portletURL.setParameter("anonymousUser", Boolean.TRUE.toString());
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().getName());
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().getName(), 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 mapping, ActionForm form, PortletConfig portletConfig,
165                            RenderRequest renderRequest, RenderResponse renderResponse)
166                    throws Exception {
167    
168                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
169                            WebKeys.THEME_DISPLAY);
170    
171                    String portletName = portletConfig.getPortletName();
172    
173                    if (!portletName.equals(PortletKeys.FAST_LOGIN)) {
174                            return mapping.findForward("portlet.login.login");
175                    }
176    
177                    renderResponse.setTitle(themeDisplay.translate("anonymous-account"));
178    
179                    return mapping.findForward("portlet.login.create_anonymous_account");
180            }
181    
182            protected void addAnonymousUser(
183                            ActionRequest actionRequest, ActionResponse actionResponse)
184                    throws Exception {
185    
186                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
187                            actionRequest);
188    
189                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
190                            WebKeys.THEME_DISPLAY);
191    
192                    boolean autoPassword = true;
193                    String password1 = null;
194                    String password2 = null;
195                    boolean autoScreenName = true;
196                    String screenName = null;
197                    String emailAddress = ParamUtil.getString(
198                            actionRequest, "emailAddress");
199                    long facebookId = 0;
200                    String openId = StringPool.BLANK;
201                    String firstName = ParamUtil.getString(actionRequest, "firstName");
202                    String lastName = ParamUtil.getString(actionRequest, "lastName");
203                    int prefixId = 0;
204                    int suffixId = 0;
205                    boolean male = true;
206                    int birthdayMonth = 0;
207                    int birthdayDay = 1;
208                    int birthdayYear = 1970;
209                    String jobTitle = null;
210                    long[] groupIds = null;
211                    long[] organizationIds = null;
212                    long[] roleIds = null;
213                    long[] userGroupIds = null;
214                    boolean sendEmail = false;
215    
216                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
217                            User.class.getName(), actionRequest);
218    
219                    serviceContext.setAttribute("anonymousUser", true);
220    
221                    if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
222                            CaptchaUtil.check(actionRequest);
223                    }
224    
225                    User user = UserServiceUtil.addUser(
226                            themeDisplay.getCompanyId(), autoPassword, password1, password2,
227                            autoScreenName, screenName, emailAddress, facebookId, openId,
228                            themeDisplay.getLocale(), firstName, null, lastName, prefixId,
229                            suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
230                            groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
231                            serviceContext);
232    
233                    UserLocalServiceUtil.updateStatus(
234                            user.getUserId(), WorkflowConstants.STATUS_INCOMPLETE);
235    
236                    // Session messages
237    
238                    SessionMessages.add(request, "user_added", user.getEmailAddress());
239                    SessionMessages.add(
240                            request, "user_added_password", user.getPasswordUnencrypted());
241            }
242    
243            @Override
244            protected boolean isCheckMethodOnProcessAction() {
245                    return _CHECK_METHOD_ON_PROCESS_ACTION;
246            }
247    
248            protected JSONObject updateIncompleteUser(
249                            ActionRequest actionRequest, ActionResponse actionResponse)
250                    throws Exception {
251    
252                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
253                            WebKeys.THEME_DISPLAY);
254    
255                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
256                            User.class.getName(), actionRequest);
257    
258                    boolean autoPassword = true;
259                    String password1 = null;
260                    String password2 = null;
261                    boolean autoScreenName = false;
262                    String screenName = null;
263                    String emailAddress = ParamUtil.getString(
264                            actionRequest, "emailAddress");
265                    long facebookId = 0;
266                    String openId = null;
267                    String firstName = null;
268                    String middleName = null;
269                    String lastName = null;
270                    int prefixId = 0;
271                    int suffixId = 0;
272                    boolean male = true;
273                    int birthdayMonth = 0;
274                    int birthdayDay = 1;
275                    int birthdayYear = 1970;
276                    String jobTitle = null;
277                    boolean updateUserInformation = false;
278                    boolean sendEmail = true;
279    
280                    User user = UserServiceUtil.updateIncompleteUser(
281                            themeDisplay.getCompanyId(), autoPassword, password1, password2,
282                            autoScreenName, screenName, emailAddress, facebookId, openId,
283                            themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
284                            suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
285                            updateUserInformation, sendEmail, serviceContext);
286    
287                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
288    
289                    if (user.getStatus() == WorkflowConstants.STATUS_APPROVED) {
290                            jsonObject.put("userStatus", "user_added");
291                    }
292                    else {
293                            jsonObject.put("userStatus", "user_pending");
294                    }
295    
296                    return jsonObject;
297            }
298    
299            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
300    
301            private static Log _log = LogFactoryUtil.getLog(
302                    CreateAnonymousAccountAction.class);
303    
304    }