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