001    /**
002     * Copyright (c) 2000-present 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.ContactNameException;
019    import com.liferay.portal.EmailAddressException;
020    import com.liferay.portal.GroupFriendlyURLException;
021    import com.liferay.portal.UserEmailAddressException;
022    import com.liferay.portal.kernel.captcha.CaptchaConfigurationException;
023    import com.liferay.portal.kernel.captcha.CaptchaTextException;
024    import com.liferay.portal.kernel.captcha.CaptchaUtil;
025    import com.liferay.portal.kernel.json.JSONFactoryUtil;
026    import com.liferay.portal.kernel.json.JSONObject;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.portlet.LiferayWindowState;
030    import com.liferay.portal.kernel.servlet.SessionErrors;
031    import com.liferay.portal.kernel.servlet.SessionMessages;
032    import com.liferay.portal.kernel.util.Constants;
033    import com.liferay.portal.kernel.util.ParamUtil;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.workflow.WorkflowConstants;
036    import com.liferay.portal.model.Company;
037    import com.liferay.portal.model.User;
038    import com.liferay.portal.security.auth.PrincipalException;
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 actionMapping, ActionForm actionForm,
073                            PortletConfig portletConfig, ActionRequest actionRequest,
074                            ActionResponse actionResponse)
075                    throws Exception {
076    
077                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
078                            WebKeys.THEME_DISPLAY);
079    
080                    Company company = themeDisplay.getCompany();
081    
082                    if (!company.isStrangers()) {
083                            throw new PrincipalException();
084                    }
085    
086                    String portletName = portletConfig.getPortletName();
087    
088                    if (!portletName.equals(PortletKeys.FAST_LOGIN)) {
089                            throw new PrincipalException();
090                    }
091    
092                    if (actionRequest.getRemoteUser() != null) {
093                            actionResponse.sendRedirect(themeDisplay.getPathMain());
094    
095                            return;
096                    }
097    
098                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
099    
100                    String emailAddress = ParamUtil.getString(
101                            actionRequest, "emailAddress");
102    
103                    PortletURL portletURL = PortletURLFactoryUtil.create(
104                            actionRequest, PortletKeys.FAST_LOGIN, themeDisplay.getPlid(),
105                            PortletRequest.RENDER_PHASE);
106    
107                    portletURL.setParameter("struts_action", "/login/login_redirect");
108                    portletURL.setParameter("emailAddress", emailAddress);
109                    portletURL.setParameter("anonymousUser", Boolean.TRUE.toString());
110                    portletURL.setWindowState(LiferayWindowState.POP_UP);
111    
112                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
113    
114                    try {
115                            if (cmd.equals(Constants.ADD)) {
116                                    addAnonymousUser(actionRequest, actionResponse);
117    
118                                    sendRedirect(
119                                            actionRequest, actionResponse, portletURL.toString());
120                            }
121                            else if (cmd.equals(Constants.UPDATE)) {
122                                    jsonObject = updateIncompleteUser(
123                                            actionRequest, actionResponse);
124    
125                                    writeJSON(actionRequest, actionResponse, jsonObject);
126                            }
127                    }
128                    catch (Exception e) {
129                            if (cmd.equals(Constants.UPDATE)) {
130                                    jsonObject.putException(e);
131    
132                                    writeJSON(actionRequest, actionResponse, jsonObject);
133                            }
134                            else if (e instanceof CaptchaConfigurationException ||
135                                             e instanceof CaptchaTextException ||
136                                             e instanceof CompanyMaxUsersException ||
137                                             e instanceof ContactNameException ||
138                                             e instanceof EmailAddressException ||
139                                             e instanceof GroupFriendlyURLException ||
140                                             e instanceof UserEmailAddressException) {
141    
142                                    SessionErrors.add(actionRequest, e.getClass(), e);
143                            }
144                            else if (e instanceof
145                                                    UserEmailAddressException.MustNotBeDuplicate) {
146    
147                                    User user = UserLocalServiceUtil.getUserByEmailAddress(
148                                            themeDisplay.getCompanyId(), emailAddress);
149    
150                                    if (user.getStatus() != WorkflowConstants.STATUS_INCOMPLETE) {
151                                            SessionErrors.add(actionRequest, e.getClass());
152                                    }
153                                    else {
154                                            sendRedirect(
155                                                    actionRequest, actionResponse, portletURL.toString());
156                                    }
157                            }
158                            else {
159                                    _log.error("Unable to create anonymous account", e);
160    
161                                    PortalUtil.sendError(e, actionRequest, actionResponse);
162                            }
163                    }
164            }
165    
166            @Override
167            public ActionForward render(
168                            ActionMapping actionMapping, ActionForm actionForm,
169                            PortletConfig portletConfig, RenderRequest renderRequest,
170                            RenderResponse renderResponse)
171                    throws Exception {
172    
173                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
174                            WebKeys.THEME_DISPLAY);
175    
176                    Company company = themeDisplay.getCompany();
177    
178                    if (!company.isStrangers()) {
179                            return actionMapping.findForward("portlet.login.login");
180                    }
181    
182                    String portletName = portletConfig.getPortletName();
183    
184                    if (!portletName.equals(PortletKeys.FAST_LOGIN)) {
185                            return actionMapping.findForward("portlet.login.login");
186                    }
187    
188                    renderResponse.setTitle(themeDisplay.translate("anonymous-account"));
189    
190                    return actionMapping.findForward(
191                            "portlet.login.create_anonymous_account");
192            }
193    
194            protected void addAnonymousUser(
195                            ActionRequest actionRequest, ActionResponse actionResponse)
196                    throws Exception {
197    
198                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
199                            actionRequest);
200    
201                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
202                            WebKeys.THEME_DISPLAY);
203    
204                    boolean autoPassword = true;
205                    String password1 = null;
206                    String password2 = null;
207                    boolean autoScreenName = true;
208                    String screenName = null;
209                    String emailAddress = ParamUtil.getString(
210                            actionRequest, "emailAddress");
211                    long facebookId = 0;
212                    String openId = StringPool.BLANK;
213                    String firstName = ParamUtil.getString(actionRequest, "firstName");
214                    String lastName = ParamUtil.getString(actionRequest, "lastName");
215                    long prefixId = 0;
216                    long suffixId = 0;
217                    boolean male = true;
218                    int birthdayMonth = 0;
219                    int birthdayDay = 1;
220                    int birthdayYear = 1970;
221                    String jobTitle = null;
222                    long[] groupIds = null;
223                    long[] organizationIds = null;
224                    long[] roleIds = null;
225                    long[] userGroupIds = null;
226                    boolean sendEmail = false;
227    
228                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
229                            User.class.getName(), actionRequest);
230    
231                    serviceContext.setAttribute("anonymousUser", true);
232    
233                    if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
234                            CaptchaUtil.check(actionRequest);
235                    }
236    
237                    serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
238    
239                    User user = UserServiceUtil.addUser(
240                            themeDisplay.getCompanyId(), autoPassword, password1, password2,
241                            autoScreenName, screenName, emailAddress, facebookId, openId,
242                            themeDisplay.getLocale(), firstName, null, lastName, prefixId,
243                            suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
244                            groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
245                            serviceContext);
246    
247                    UserLocalServiceUtil.updateStatus(
248                            user.getUserId(), WorkflowConstants.STATUS_INCOMPLETE,
249                            new ServiceContext());
250    
251                    // Session messages
252    
253                    SessionMessages.add(request, "userAdded", user.getEmailAddress());
254                    SessionMessages.add(
255                            request, "userAddedPassword", user.getPasswordUnencrypted());
256            }
257    
258            @Override
259            protected void addSuccessMessage(
260                    ActionRequest actionRequest, ActionResponse actionResponse) {
261    
262                    String portletId = (String)actionRequest.getAttribute(
263                            WebKeys.PORTLET_ID);
264    
265                    if (!portletId.equals(PortletKeys.FAST_LOGIN)) {
266                            super.addSuccessMessage(actionRequest, actionResponse);
267                    }
268            }
269    
270            @Override
271            protected boolean isCheckMethodOnProcessAction() {
272                    return _CHECK_METHOD_ON_PROCESS_ACTION;
273            }
274    
275            protected JSONObject updateIncompleteUser(
276                            ActionRequest actionRequest, ActionResponse actionResponse)
277                    throws Exception {
278    
279                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
280                            WebKeys.THEME_DISPLAY);
281    
282                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
283                            User.class.getName(), actionRequest);
284    
285                    boolean autoPassword = true;
286                    String password1 = null;
287                    String password2 = null;
288                    boolean autoScreenName = false;
289                    String screenName = null;
290                    String emailAddress = ParamUtil.getString(
291                            actionRequest, "emailAddress");
292                    long facebookId = 0;
293                    String openId = null;
294                    String firstName = null;
295                    String middleName = null;
296                    String lastName = null;
297                    long prefixId = 0;
298                    long suffixId = 0;
299                    boolean male = true;
300                    int birthdayMonth = 0;
301                    int birthdayDay = 1;
302                    int birthdayYear = 1970;
303                    String jobTitle = null;
304                    boolean updateUserInformation = false;
305                    boolean sendEmail = true;
306    
307                    User user = UserServiceUtil.updateIncompleteUser(
308                            themeDisplay.getCompanyId(), autoPassword, password1, password2,
309                            autoScreenName, screenName, emailAddress, facebookId, openId,
310                            themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
311                            suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
312                            updateUserInformation, sendEmail, serviceContext);
313    
314                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
315    
316                    if (user.getStatus() == WorkflowConstants.STATUS_APPROVED) {
317                            jsonObject.put("userStatus", "user_added");
318                    }
319                    else {
320                            jsonObject.put("userStatus", "user_pending");
321                    }
322    
323                    return jsonObject;
324            }
325    
326            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
327    
328            private static final Log _log = LogFactoryUtil.getLog(
329                    CreateAnonymousAccountAction.class);
330    
331    }