1
22
23 package com.liferay.portlet.myaccount.action;
24
25 import com.liferay.portal.ContactFirstNameException;
26 import com.liferay.portal.ContactLastNameException;
27 import com.liferay.portal.DuplicateUserEmailAddressException;
28 import com.liferay.portal.DuplicateUserScreenNameException;
29 import com.liferay.portal.NoSuchOrganizationException;
30 import com.liferay.portal.OrganizationParentException;
31 import com.liferay.portal.RequiredUserException;
32 import com.liferay.portal.ReservedUserEmailAddressException;
33 import com.liferay.portal.UserEmailAddressException;
34 import com.liferay.portal.UserIdException;
35 import com.liferay.portal.UserPasswordException;
36 import com.liferay.portal.UserScreenNameException;
37 import com.liferay.portal.UserSmsException;
38 import com.liferay.portal.captcha.CaptchaTextException;
39 import com.liferay.portal.captcha.CaptchaUtil;
40 import com.liferay.portal.kernel.language.LanguageUtil;
41 import com.liferay.portal.kernel.util.Constants;
42 import com.liferay.portal.kernel.util.HttpUtil;
43 import com.liferay.portal.kernel.util.ParamUtil;
44 import com.liferay.portal.kernel.util.StringUtil;
45 import com.liferay.portal.model.Company;
46 import com.liferay.portal.model.CompanyConstants;
47 import com.liferay.portal.model.User;
48 import com.liferay.portal.security.auth.PrincipalException;
49 import com.liferay.portal.service.UserServiceUtil;
50 import com.liferay.portal.struts.PortletAction;
51 import com.liferay.portal.theme.ThemeDisplay;
52 import com.liferay.portal.util.PortalUtil;
53 import com.liferay.portal.util.WebKeys;
54 import com.liferay.util.servlet.SessionErrors;
55 import com.liferay.util.servlet.SessionMessages;
56
57 import javax.portlet.ActionRequest;
58 import javax.portlet.ActionResponse;
59 import javax.portlet.PortletConfig;
60 import javax.portlet.RenderRequest;
61 import javax.portlet.RenderResponse;
62
63 import javax.servlet.http.HttpServletRequest;
64
65 import org.apache.struts.action.ActionForm;
66 import org.apache.struts.action.ActionForward;
67 import org.apache.struts.action.ActionMapping;
68
69
75 public class AddUserAction extends PortletAction {
76
77 public void processAction(
78 ActionMapping mapping, ActionForm form, PortletConfig config,
79 ActionRequest req, ActionResponse res)
80 throws Exception {
81
82 String cmd = ParamUtil.getString(req, Constants.CMD);
83
84 try {
85 if (cmd.equals(Constants.ADD)) {
86 addUser(req, res);
87 }
88 }
89 catch (Exception e) {
90 if (e instanceof CaptchaTextException ||
91 e instanceof ContactFirstNameException ||
92 e instanceof ContactLastNameException ||
93 e instanceof DuplicateUserEmailAddressException ||
94 e instanceof DuplicateUserScreenNameException ||
95 e instanceof NoSuchOrganizationException ||
96 e instanceof OrganizationParentException ||
97 e instanceof RequiredUserException ||
98 e instanceof ReservedUserEmailAddressException ||
99 e instanceof UserEmailAddressException ||
100 e instanceof UserIdException ||
101 e instanceof UserPasswordException ||
102 e instanceof UserScreenNameException ||
103 e instanceof UserSmsException) {
104
105 SessionErrors.add(req, e.getClass().getName(), e);
106 }
107 else {
108 throw e;
109 }
110 }
111 }
112
113 public ActionForward render(
114 ActionMapping mapping, ActionForm form, PortletConfig config,
115 RenderRequest req, RenderResponse res)
116 throws Exception {
117
118 Company company = PortalUtil.getCompany(req);
119
120 if (!company.isStrangers()) {
121 throw new PrincipalException();
122 }
123
124 ThemeDisplay themeDisplay =
125 (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
126
127 res.setTitle(
128 LanguageUtil.get(
129 themeDisplay.getCompanyId(), themeDisplay.getLocale(),
130 "create-account"));
131
132 return mapping.findForward("portlet.my_account.create_account");
133 }
134
135 protected void addUser(ActionRequest req, ActionResponse res)
136 throws Exception {
137
138 ThemeDisplay themeDisplay =
139 (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
140
141 Company company = themeDisplay.getCompany();
142
143 boolean autoPassword = true;
144 String password1 = null;
145 String password2 = null;
146 boolean autoScreenName = false;
147 String screenName = ParamUtil.getString(req, "screenName");
148 String emailAddress = ParamUtil.getString(req, "emailAddress");
149 String firstName = ParamUtil.getString(req, "firstName");
150 String middleName = ParamUtil.getString(req, "middleName");
151 String lastName = ParamUtil.getString(req, "lastName");
152 int prefixId = ParamUtil.getInteger(req, "prefixId");
153 int suffixId = ParamUtil.getInteger(req, "suffixId");
154 boolean male = ParamUtil.get(req, "male", true);
155 int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
156 int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
157 int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
158 String jobTitle = ParamUtil.getString(req, "jobTitle");
159 long[] organizationIds = StringUtil.split(
160 ParamUtil.getString(req, "organizationIds"), 0L);
161 boolean sendEmail = true;
162
163 CaptchaUtil.check(req);
164
165 User user = UserServiceUtil.addUser(
166 company.getCompanyId(), autoPassword, password1, password2,
167 autoScreenName, screenName, emailAddress, themeDisplay.getLocale(),
168 firstName, middleName, lastName, prefixId, suffixId, male,
169 birthdayMonth, birthdayDay, birthdayYear, jobTitle, organizationIds,
170 sendEmail);
171
172
174 HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
175
176 SessionMessages.add(httpReq, "user_added", user.getEmailAddress());
177 SessionMessages.add(
178 httpReq, "user_added_password", user.getPasswordUnencrypted());
179
180
182 String login = null;
183
184 if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_ID)) {
185 login = String.valueOf(user.getUserId());
186 }
187 else if (company.getAuthType().equals(CompanyConstants.AUTH_TYPE_SN)) {
188 login = user.getScreenName();
189 }
190 else {
191 login = user.getEmailAddress();
192 }
193
194 String redirect = HttpUtil.addParameter(
195 themeDisplay.getURLSignIn(), "login", login);
196
197 res.sendRedirect(redirect);
198 }
199
200 protected boolean isCheckMethodOnProcessAction() {
201 return _CHECK_METHOD_ON_PROCESS_ACTION;
202 }
203
204 private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
205
206 }