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.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
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 String portletName = portletConfig.getPortletName();
084
085 if (!portletName.equals(PortletKeys.FAST_LOGIN)) {
086 throw new PrincipalException();
087 }
088
089 if (actionRequest.getRemoteUser() != null) {
090 actionResponse.sendRedirect(themeDisplay.getPathMain());
091
092 return;
093 }
094
095 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
096
097 String emailAddress = ParamUtil.getString(
098 actionRequest, "emailAddress");
099
100 PortletURL portletURL = PortletURLFactoryUtil.create(
101 actionRequest, PortletKeys.FAST_LOGIN, themeDisplay.getPlid(),
102 PortletRequest.RENDER_PHASE);
103
104 portletURL.setParameter("struts_action", "/login/login_redirect");
105 portletURL.setParameter("emailAddress", emailAddress);
106 portletURL.setParameter("anonymousUser", Boolean.TRUE.toString());
107 portletURL.setWindowState(LiferayWindowState.POP_UP);
108
109 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
110
111 try {
112 if (cmd.equals(Constants.ADD)) {
113 addAnonymousUser(actionRequest, actionResponse);
114
115 sendRedirect(
116 actionRequest, actionResponse, portletURL.toString());
117 }
118 else if (cmd.equals(Constants.UPDATE)) {
119 Company company = themeDisplay.getCompany();
120
121 if (!company.isStrangers()) {
122 throw new PrincipalException();
123 }
124
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 String portletName = portletConfig.getPortletName();
180
181 if (!portletName.equals(PortletKeys.FAST_LOGIN)) {
182 return actionMapping.findForward("portlet.login.login");
183 }
184
185 renderResponse.setTitle(themeDisplay.translate("anonymous-account"));
186
187 return actionMapping.findForward(
188 "portlet.login.create_anonymous_account");
189 }
190
191 protected void addAnonymousUser(
192 ActionRequest actionRequest, ActionResponse actionResponse)
193 throws Exception {
194
195 HttpServletRequest request = PortalUtil.getHttpServletRequest(
196 actionRequest);
197
198 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
199 WebKeys.THEME_DISPLAY);
200
201 boolean autoPassword = true;
202 String password1 = null;
203 String password2 = null;
204 boolean autoScreenName = true;
205 String screenName = null;
206 String emailAddress = ParamUtil.getString(
207 actionRequest, "emailAddress");
208 long facebookId = 0;
209 String openId = StringPool.BLANK;
210 String firstName = ParamUtil.getString(actionRequest, "firstName");
211 String lastName = ParamUtil.getString(actionRequest, "lastName");
212 int prefixId = 0;
213 int suffixId = 0;
214 boolean male = true;
215 int birthdayMonth = 0;
216 int birthdayDay = 1;
217 int birthdayYear = 1970;
218 String jobTitle = null;
219 long[] groupIds = null;
220 long[] organizationIds = null;
221 long[] roleIds = null;
222 long[] userGroupIds = null;
223 boolean sendEmail = false;
224
225 ServiceContext serviceContext = ServiceContextFactory.getInstance(
226 User.class.getName(), actionRequest);
227
228 serviceContext.setAttribute("anonymousUser", true);
229
230 if (PropsValues.CAPTCHA_CHECK_PORTAL_CREATE_ACCOUNT) {
231 CaptchaUtil.check(actionRequest);
232 }
233
234 serviceContext.setWorkflowAction(WorkflowConstants.ACTION_SAVE_DRAFT);
235
236 User user = UserServiceUtil.addUser(
237 themeDisplay.getCompanyId(), autoPassword, password1, password2,
238 autoScreenName, screenName, emailAddress, facebookId, openId,
239 themeDisplay.getLocale(), firstName, null, lastName, prefixId,
240 suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
241 groupIds, organizationIds, roleIds, userGroupIds, sendEmail,
242 serviceContext);
243
244 UserLocalServiceUtil.updateStatus(
245 user.getUserId(), WorkflowConstants.STATUS_INCOMPLETE);
246
247
248
249 SessionMessages.add(request, "userAdded", user.getEmailAddress());
250 SessionMessages.add(
251 request, "userAddedPassword", user.getPasswordUnencrypted());
252 }
253
254 @Override
255 protected void addSuccessMessage(
256 ActionRequest actionRequest, ActionResponse actionResponse) {
257
258 String portletId = (String)actionRequest.getAttribute(
259 WebKeys.PORTLET_ID);
260
261 if (!portletId.equals(PortletKeys.FAST_LOGIN)) {
262 super.addSuccessMessage(actionRequest, actionResponse);
263 }
264 }
265
266 @Override
267 protected boolean isCheckMethodOnProcessAction() {
268 return _CHECK_METHOD_ON_PROCESS_ACTION;
269 }
270
271 protected JSONObject updateIncompleteUser(
272 ActionRequest actionRequest, ActionResponse actionResponse)
273 throws Exception {
274
275 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
276 WebKeys.THEME_DISPLAY);
277
278 ServiceContext serviceContext = ServiceContextFactory.getInstance(
279 User.class.getName(), actionRequest);
280
281 boolean autoPassword = true;
282 String password1 = null;
283 String password2 = null;
284 boolean autoScreenName = false;
285 String screenName = null;
286 String emailAddress = ParamUtil.getString(
287 actionRequest, "emailAddress");
288 long facebookId = 0;
289 String openId = null;
290 String firstName = null;
291 String middleName = null;
292 String lastName = null;
293 int prefixId = 0;
294 int suffixId = 0;
295 boolean male = true;
296 int birthdayMonth = 0;
297 int birthdayDay = 1;
298 int birthdayYear = 1970;
299 String jobTitle = null;
300 boolean updateUserInformation = false;
301 boolean sendEmail = true;
302
303 User user = UserServiceUtil.updateIncompleteUser(
304 themeDisplay.getCompanyId(), autoPassword, password1, password2,
305 autoScreenName, screenName, emailAddress, facebookId, openId,
306 themeDisplay.getLocale(), firstName, middleName, lastName, prefixId,
307 suffixId, male, birthdayMonth, birthdayDay, birthdayYear, jobTitle,
308 updateUserInformation, sendEmail, serviceContext);
309
310 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
311
312 if (user.getStatus() == WorkflowConstants.STATUS_APPROVED) {
313 jsonObject.put("userStatus", "user_added");
314 }
315 else {
316 jsonObject.put("userStatus", "user_pending");
317 }
318
319 return jsonObject;
320 }
321
322 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
323
324 private static Log _log = LogFactoryUtil.getLog(
325 CreateAnonymousAccountAction.class);
326
327 }