001
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.JSONPortletResponseUtil;
030 import com.liferay.portal.kernel.portlet.LiferayWindowState;
031 import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
032 import com.liferay.portal.kernel.servlet.SessionErrors;
033 import com.liferay.portal.kernel.servlet.SessionMessages;
034 import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
035 import com.liferay.portal.kernel.util.Constants;
036 import com.liferay.portal.kernel.util.JavaConstants;
037 import com.liferay.portal.kernel.util.ParamUtil;
038 import com.liferay.portal.kernel.util.PropsKeys;
039 import com.liferay.portal.kernel.util.StringPool;
040 import com.liferay.portal.kernel.workflow.WorkflowConstants;
041 import com.liferay.portal.model.Company;
042 import com.liferay.portal.model.User;
043 import com.liferay.portal.security.auth.PrincipalException;
044 import com.liferay.portal.service.ServiceContext;
045 import com.liferay.portal.service.ServiceContextFactory;
046 import com.liferay.portal.service.UserLocalServiceUtil;
047 import com.liferay.portal.service.UserServiceUtil;
048 import com.liferay.portal.theme.ThemeDisplay;
049 import com.liferay.portal.util.PortalUtil;
050 import com.liferay.portal.util.PortletKeys;
051 import com.liferay.portal.util.PropsValues;
052 import com.liferay.portal.util.WebKeys;
053 import com.liferay.portlet.PortletURLFactoryUtil;
054
055 import javax.portlet.ActionRequest;
056 import javax.portlet.ActionResponse;
057 import javax.portlet.PortletConfig;
058 import javax.portlet.PortletRequest;
059 import javax.portlet.PortletURL;
060
061 import javax.servlet.http.HttpServletRequest;
062
063
067
068 @OSGiBeanProperties(
069 property = {
070 "javax.portlet.name=" + PortletKeys.FAST_LOGIN,
071 "javax.portlet.name=" + PortletKeys.LOGIN,
072 "mvc.command.name=/login/create_anonymous_account"
073 }
074 )
075 public class CreateAnonymousAccountMVCActionCommand
076 extends BaseMVCActionCommand {
077
078 protected void addAnonymousUser(
079 ActionRequest actionRequest, ActionResponse actionResponse)
080 throws Exception {
081
082 HttpServletRequest request = PortalUtil.getHttpServletRequest(
083 actionRequest);
084
085 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
086 WebKeys.THEME_DISPLAY);
087
088 boolean autoPassword = true;
089 String password1 = null;
090 String password2 = null;
091 boolean autoScreenName = true;
092 String screenName = null;
093 String emailAddress = ParamUtil.getString(
094 actionRequest, "emailAddress");
095 long facebookId = 0;
096 String openId = StringPool.BLANK;
097 String firstName = ParamUtil.getString(actionRequest, "firstName");
098 String lastName = ParamUtil.getString(actionRequest, "lastName");
099 long prefixId = 0;
100 long suffixId = 0;
101 boolean male = true;
102 int birthdayMonth = 0;
103 int birthdayDay = 1;
104 int birthdayYear = 1970;
105 String jobTitle = null;
106 long[] groupIds = null;
107 long[] organizationIds = null;
108 long[] roleIds = null;
109 long[] userGroupIds = null;
110 boolean sendEmail = false;
111
112 ServiceContext serviceContext = ServiceContextFactory.getInstance(
113 User.class.getName(), actionRequest);
114
115 serviceContext.setAttribute("anonymousUser", true);
116
117 if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
118 CaptchaUtil.check(actionRequest);
119 }
120
121 serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
122
123 User user = UserServiceUtil.addUser(
124 themeDisplay.getCompanyId(), autoPassword, password1, password2,
125 autoScreenName, screenName, emailAddress, facebookId, openId,
126 themeDisplay.getLocale(), firstName, null, lastName, prefixId,
127 suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
128 groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
129 serviceContext);
130
131 UserLocalServiceUtil.updateStatus(
132 user.getUserId(), WorkflowConstants.STATUS_INCOMPLETE,
133 new ServiceContext());
134
135
136
137 SessionMessages.add(request, "userAdded", user.getEmailAddress());
138 SessionMessages.add(
139 request, "userAddedPassword", user.getPasswordUnencrypted());
140 }
141
142 @Override
143 protected void addSuccessMessage(
144 ActionRequest actionRequest, ActionResponse actionResponse) {
145
146 String portletId = (String)actionRequest.getAttribute(
147 WebKeys.PORTLET_ID);
148
149 if (!portletId.equals(PortletKeys.FAST_LOGIN)) {
150 super.addSuccessMessage(actionRequest, actionResponse);
151 }
152 }
153
154 @Override
155 protected void doProcessAction(
156 ActionRequest actionRequest, ActionResponse actionResponse)
157 throws Exception {
158
159 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
160 WebKeys.THEME_DISPLAY);
161
162 Company company = themeDisplay.getCompany();
163
164 if (!company.isStrangers()) {
165 throw new PrincipalException.MustBeEnabled(
166 company.getCompanyId(), PropsKeys.COMPANY_SECURITY_STRANGERS);
167 }
168
169 PortletConfig portletConfig = (PortletConfig)actionRequest.getAttribute(
170 JavaConstants.JAVAX_PORTLET_CONFIG);
171
172 String portletName = portletConfig.getPortletName();
173
174 if (!portletName.equals(PortletKeys.FAST_LOGIN)) {
175 throw new PrincipalException("Unable to create anonymous account");
176 }
177
178 if (actionRequest.getRemoteUser() != null) {
179 actionResponse.sendRedirect(themeDisplay.getPathMain());
180
181 return;
182 }
183
184 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
185
186 String emailAddress = ParamUtil.getString(
187 actionRequest, "emailAddress");
188
189 PortletURL portletURL = PortletURLFactoryUtil.create(
190 actionRequest, PortletKeys.FAST_LOGIN, themeDisplay.getPlid(),
191 PortletRequest.RENDER_PHASE);
192
193 portletURL.setParameter(
194 "mvcRenderCommandName", "/login/login_redirect");
195 portletURL.setParameter("emailAddress", emailAddress);
196 portletURL.setParameter("anonymousUser", Boolean.TRUE.toString());
197 portletURL.setWindowState(LiferayWindowState.POP_UP);
198
199 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
200
201 try {
202 if (cmd.equals(Constants.ADD)) {
203 addAnonymousUser(actionRequest, actionResponse);
204
205 sendRedirect(
206 actionRequest, actionResponse, portletURL.toString());
207 }
208 else if (cmd.equals(Constants.UPDATE)) {
209 jsonObject = updateIncompleteUser(
210 actionRequest, actionResponse);
211
212 JSONPortletResponseUtil.writeJSON(
213 actionRequest, actionResponse, jsonObject);
214 }
215 }
216 catch (Exception e) {
217 if (cmd.equals(Constants.UPDATE)) {
218 jsonObject.putException(e);
219
220 JSONPortletResponseUtil.writeJSON(
221 actionRequest, actionResponse, jsonObject);
222 }
223 else if (e instanceof CaptchaConfigurationException ||
224 e instanceof CaptchaTextException ||
225 e instanceof CompanyMaxUsersException ||
226 e instanceof ContactNameException ||
227 e instanceof EmailAddressException ||
228 e instanceof GroupFriendlyURLException ||
229 e instanceof UserEmailAddressException) {
230
231 SessionErrors.add(actionRequest, e.getClass(), e);
232 }
233 else if (e instanceof
234 UserEmailAddressException.MustNotBeDuplicate) {
235
236 User user = UserLocalServiceUtil.getUserByEmailAddress(
237 themeDisplay.getCompanyId(), emailAddress);
238
239 if (user.getStatus() != WorkflowConstants.STATUS_INCOMPLETE) {
240 SessionErrors.add(actionRequest, e.getClass());
241 }
242 else {
243 sendRedirect(
244 actionRequest, actionResponse, portletURL.toString());
245 }
246 }
247 else {
248 _log.error("Unable to create anonymous account", e);
249
250 PortalUtil.sendError(e, actionRequest, actionResponse);
251 }
252 }
253 }
254
255 protected JSONObject updateIncompleteUser(
256 ActionRequest actionRequest, ActionResponse actionResponse)
257 throws Exception {
258
259 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
260 WebKeys.THEME_DISPLAY);
261
262 ServiceContext serviceContext = ServiceContextFactory.getInstance(
263 User.class.getName(), actionRequest);
264
265 boolean autoPassword = true;
266 String password1 = null;
267 String password2 = null;
268 boolean autoScreenName = false;
269 String screenName = null;
270 String emailAddress = ParamUtil.getString(
271 actionRequest, "emailAddress");
272 long facebookId = 0;
273 String openId = null;
274 String firstName = null;
275 String middleName = null;
276 String lastName = null;
277 long prefixId = 0;
278 long suffixId = 0;
279 boolean male = true;
280 int birthdayMonth = 0;
281 int birthdayDay = 1;
282 int birthdayYear = 1970;
283 String jobTitle = null;
284 boolean updateUserInformation = false;
285 boolean sendEmail = true;
286
287 User user = UserServiceUtil.updateIncompleteUser(
288 themeDisplay.getCompanyId(), autoPassword, password1, password2,
289 autoScreenName, screenName, emailAddress, facebookId, openId,
290 themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
291 suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
292 updateUserInformation, sendEmail, serviceContext);
293
294 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
295
296 if (user.getStatus() == WorkflowConstants.STATUS_APPROVED) {
297 jsonObject.put("userStatus", "user_added");
298 }
299 else {
300 jsonObject.put("userStatus", "user_pending");
301 }
302
303 return jsonObject;
304 }
305
306 private static final Log _log = LogFactoryUtil.getLog(
307 CreateAnonymousAccountMVCActionCommand.class);
308
309 }