001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.myaccount.action;
022:
023: import com.liferay.portal.ContactFirstNameException;
024: import com.liferay.portal.ContactLastNameException;
025: import com.liferay.portal.DuplicateUserEmailAddressException;
026: import com.liferay.portal.DuplicateUserScreenNameException;
027: import com.liferay.portal.NoSuchOrganizationException;
028: import com.liferay.portal.OrganizationParentException;
029: import com.liferay.portal.RequiredUserException;
030: import com.liferay.portal.ReservedUserEmailAddressException;
031: import com.liferay.portal.UserEmailAddressException;
032: import com.liferay.portal.UserIdException;
033: import com.liferay.portal.UserPasswordException;
034: import com.liferay.portal.UserScreenNameException;
035: import com.liferay.portal.UserSmsException;
036: import com.liferay.portal.captcha.CaptchaTextException;
037: import com.liferay.portal.captcha.CaptchaUtil;
038: import com.liferay.portal.kernel.language.LanguageUtil;
039: import com.liferay.portal.kernel.util.Constants;
040: import com.liferay.portal.kernel.util.ParamUtil;
041: import com.liferay.portal.kernel.util.StringUtil;
042: import com.liferay.portal.model.Company;
043: import com.liferay.portal.model.User;
044: import com.liferay.portal.model.impl.CompanyImpl;
045: import com.liferay.portal.security.auth.PrincipalException;
046: import com.liferay.portal.service.UserServiceUtil;
047: import com.liferay.portal.struts.PortletAction;
048: import com.liferay.portal.theme.ThemeDisplay;
049: import com.liferay.portal.util.PortalUtil;
050: import com.liferay.portal.util.WebKeys;
051: import com.liferay.util.servlet.SessionErrors;
052: import com.liferay.util.servlet.SessionMessages;
053:
054: import javax.portlet.ActionRequest;
055: import javax.portlet.ActionResponse;
056: import javax.portlet.PortletConfig;
057: import javax.portlet.RenderRequest;
058: import javax.portlet.RenderResponse;
059:
060: import javax.servlet.http.HttpServletRequest;
061:
062: import org.apache.struts.action.ActionForm;
063: import org.apache.struts.action.ActionForward;
064: import org.apache.struts.action.ActionMapping;
065:
066: /**
067: * <a href="AddUserAction.java.html"><b><i>View Source</i></b></a>
068: *
069: * @author Brian Wing Shun Chan
070: *
071: */
072: public class AddUserAction extends PortletAction {
073:
074: public void processAction(ActionMapping mapping, ActionForm form,
075: PortletConfig config, ActionRequest req, ActionResponse res)
076: throws Exception {
077:
078: String cmd = ParamUtil.getString(req, Constants.CMD);
079:
080: try {
081: if (cmd.equals(Constants.ADD)) {
082: addUser(req, res);
083: }
084: } catch (Exception e) {
085: if (e instanceof CaptchaTextException
086: || e instanceof ContactFirstNameException
087: || e instanceof ContactLastNameException
088: || e instanceof DuplicateUserEmailAddressException
089: || e instanceof DuplicateUserScreenNameException
090: || e instanceof NoSuchOrganizationException
091: || e instanceof OrganizationParentException
092: || e instanceof RequiredUserException
093: || e instanceof ReservedUserEmailAddressException
094: || e instanceof UserEmailAddressException
095: || e instanceof UserIdException
096: || e instanceof UserPasswordException
097: || e instanceof UserScreenNameException
098: || e instanceof UserSmsException) {
099:
100: SessionErrors.add(req, e.getClass().getName(), e);
101: } else {
102: throw e;
103: }
104: }
105: }
106:
107: public ActionForward render(ActionMapping mapping, ActionForm form,
108: PortletConfig config, RenderRequest req, RenderResponse res)
109: throws Exception {
110:
111: Company company = PortalUtil.getCompany(req);
112:
113: if (!company.isStrangers()) {
114: throw new PrincipalException();
115: }
116:
117: ThemeDisplay themeDisplay = (ThemeDisplay) req
118: .getAttribute(WebKeys.THEME_DISPLAY);
119:
120: res.setTitle(LanguageUtil.get(themeDisplay.getCompanyId(),
121: themeDisplay.getLocale(), "create-account"));
122:
123: return mapping.findForward("portlet.my_account.create_account");
124: }
125:
126: protected void addUser(ActionRequest req, ActionResponse res)
127: throws Exception {
128:
129: ThemeDisplay themeDisplay = (ThemeDisplay) req
130: .getAttribute(WebKeys.THEME_DISPLAY);
131:
132: Company company = themeDisplay.getCompany();
133:
134: boolean autoPassword = true;
135: String password1 = null;
136: String password2 = null;
137: boolean autoScreenName = false;
138: String screenName = ParamUtil.getString(req, "screenName");
139: String emailAddress = ParamUtil.getString(req, "emailAddress");
140: String firstName = ParamUtil.getString(req, "firstName");
141: String middleName = ParamUtil.getString(req, "middleName");
142: String lastName = ParamUtil.getString(req, "lastName");
143: int prefixId = ParamUtil.getInteger(req, "prefixId");
144: int suffixId = ParamUtil.getInteger(req, "suffixId");
145: boolean male = ParamUtil.get(req, "male", true);
146: int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
147: int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
148: int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
149: String jobTitle = ParamUtil.getString(req, "jobTitle");
150: long[] organizationIds = StringUtil.split(ParamUtil.getString(
151: req, "organizationIds"), 0L);
152: boolean sendEmail = true;
153:
154: CaptchaUtil.check(req);
155:
156: User user = UserServiceUtil.addUser(company.getCompanyId(),
157: autoPassword, password1, password2, autoScreenName,
158: screenName, emailAddress, themeDisplay.getLocale(),
159: firstName, middleName, lastName, prefixId, suffixId,
160: male, birthdayMonth, birthdayDay, birthdayYear,
161: jobTitle, organizationIds, sendEmail);
162:
163: // Session messages
164:
165: HttpServletRequest httpReq = PortalUtil
166: .getHttpServletRequest(req);
167:
168: SessionMessages.add(httpReq, "user_added", user
169: .getEmailAddress());
170: SessionMessages.add(httpReq, "user_added_password", user
171: .getPasswordUnencrypted());
172:
173: // Send redirect
174:
175: String redirect = themeDisplay.getPathMain()
176: + "/portal/login?login=";
177:
178: if (company.getAuthType().equals(CompanyImpl.AUTH_TYPE_ID)) {
179: redirect += user.getUserId();
180: } else if (company.getAuthType().equals(
181: CompanyImpl.AUTH_TYPE_SN)) {
182: redirect += user.getScreenName();
183: } else {
184: redirect += user.getEmailAddress();
185: }
186:
187: res.sendRedirect(redirect);
188: }
189:
190: protected boolean isCheckMethodOnProcessAction() {
191: return _CHECK_METHOD_ON_PROCESS_ACTION;
192: }
193:
194: private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
195:
196: }
|