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