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.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.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 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                    Company company = themeDisplay.getCompany();
172    
173                    if (!company.isStrangers()) {
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, "userAdded", user.getEmailAddress());
239                    SessionMessages.add(
240                            request, "userAddedPassword", user.getPasswordUnencrypted());
241            }
242    
243            @Override
244            protected void addSuccessMessage(
245                    ActionRequest actionRequest, ActionResponse actionResponse) {
246    
247                    String portletId = (String)actionRequest.getAttribute(
248                            WebKeys.PORTLET_ID);
249    
250                    if (!portletId.equals(PortletKeys.FAST_LOGIN)) {
251                            super.addSuccessMessage(actionRequest, actionResponse);
252                    }
253            }
254    
255            @Override
256            protected boolean isCheckMethodOnProcessAction() {
257                    return _CHECK_METHOD_ON_PROCESS_ACTION;
258            }
259    
260            protected JSONObject updateIncompleteUser(
261                            ActionRequest actionRequest, ActionResponse actionResponse)
262                    throws Exception {
263    
264                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
265                            WebKeys.THEME_DISPLAY);
266    
267                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
268                            User.class.getName(), actionRequest);
269    
270                    boolean autoPassword = true;
271                    String password1 = null;
272                    String password2 = null;
273                    boolean autoScreenName = false;
274                    String screenName = null;
275                    String emailAddress = ParamUtil.getString(
276                            actionRequest, "emailAddress");
277                    long facebookId = 0;
278                    String openId = null;
279                    String firstName = null;
280                    String middleName = null;
281                    String lastName = null;
282                    int prefixId = 0;
283                    int suffixId = 0;
284                    boolean male = true;
285                    int birthdayMonth = 0;
286                    int birthdayDay = 1;
287                    int birthdayYear = 1970;
288                    String jobTitle = null;
289                    boolean updateUserInformation = false;
290                    boolean sendEmail = true;
291    
292                    User user = UserServiceUtil.updateIncompleteUser(
293                            themeDisplay.getCompanyId(), autoPassword, password1, password2,
294                            autoScreenName, screenName, emailAddress, facebookId, openId,
295                            themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
296                            suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
297                            updateUserInformation, sendEmail, serviceContext);
298    
299                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
300    
301                    if (user.getStatus() == WorkflowConstants.STATUS_APPROVED) {
302                            jsonObject.put("userStatus", "user_added");
303                    }
304                    else {
305                            jsonObject.put("userStatus", "user_pending");
306                    }
307    
308                    return jsonObject;
309            }
310    
311            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
312    
313            private static Log _log = LogFactoryUtil.getLog(
314                    CreateAnonymousAccountAction.class);
315    
316    }