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.enterpriseadmin.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.NoSuchUserException;
028: import com.liferay.portal.RequiredUserException;
029: import com.liferay.portal.ReservedUserEmailAddressException;
030: import com.liferay.portal.ReservedUserScreenNameException;
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.kernel.util.Constants;
037: import com.liferay.portal.kernel.util.ParamUtil;
038: import com.liferay.portal.kernel.util.StringPool;
039: import com.liferay.portal.kernel.util.StringUtil;
040: import com.liferay.portal.kernel.util.Validator;
041: import com.liferay.portal.model.Group;
042: import com.liferay.portal.model.Layout;
043: import com.liferay.portal.model.User;
044: import com.liferay.portal.security.auth.PrincipalException;
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.WebKeys;
050: import com.liferay.portlet.CachePortlet;
051: import com.liferay.portlet.admin.util.AdminUtil;
052: import com.liferay.util.HttpUtil;
053: import com.liferay.util.servlet.SessionErrors;
054:
055: import javax.portlet.ActionRequest;
056: import javax.portlet.ActionResponse;
057: import javax.portlet.PortletConfig;
058: import javax.portlet.PortletSession;
059: import javax.portlet.RenderRequest;
060: import javax.portlet.RenderResponse;
061:
062: import javax.servlet.http.HttpServletRequest;
063: import javax.servlet.http.HttpSession;
064:
065: import org.apache.struts.Globals;
066: import org.apache.struts.action.ActionForm;
067: import org.apache.struts.action.ActionForward;
068: import org.apache.struts.action.ActionMapping;
069:
070: /**
071: * <a href="EditUserAction.java.html"><b><i>View Source</i></b></a>
072: *
073: * @author Brian Wing Shun Chan
074: *
075: */
076: public class EditUserAction extends PortletAction {
077:
078: public void processAction(ActionMapping mapping, ActionForm form,
079: PortletConfig config, ActionRequest req, ActionResponse res)
080: throws Exception {
081:
082: String cmd = ParamUtil.getString(req, Constants.CMD);
083:
084: try {
085: User user = null;
086: String oldScreenName = StringPool.BLANK;
087:
088: if (cmd.equals(Constants.ADD)
089: || cmd.equals(Constants.UPDATE)) {
090: Object[] returnValue = updateUser(req);
091:
092: user = (User) returnValue[0];
093: oldScreenName = ((String) returnValue[1]);
094: } else if (cmd.equals(Constants.DEACTIVATE)
095: || cmd.equals(Constants.DELETE)
096: || cmd.equals(Constants.RESTORE)) {
097:
098: deleteUsers(req);
099: } else if (cmd.equals("deleteRole")) {
100: deleteRole(req);
101: } else if (cmd.equals("unlock")) {
102: user = updateLockout(req);
103: }
104:
105: String redirect = ParamUtil.getString(req, "redirect");
106:
107: if (user != null) {
108: if (Validator.isNotNull(oldScreenName)) {
109:
110: // This will fix the redirect if the user is on his personal
111: // my account page and changes his screen name. A redirect
112: // that references the old screen name no longer points to a
113: // valid screen name and therefore needs to be updated.
114:
115: ThemeDisplay themeDisplay = (ThemeDisplay) req
116: .getAttribute(WebKeys.THEME_DISPLAY);
117:
118: Group group = user.getGroup();
119:
120: if (group.getGroupId() == themeDisplay
121: .getPortletGroupId()) {
122:
123: Layout layout = themeDisplay.getLayout();
124:
125: String friendlyURLPath = group
126: .getPathFriendlyURL(layout
127: .isPrivateLayout(),
128: themeDisplay);
129:
130: String oldPath = friendlyURLPath
131: + StringPool.SLASH + oldScreenName;
132: String newPath = friendlyURLPath
133: + StringPool.SLASH
134: + user.getScreenName();
135:
136: redirect = StringUtil.replace(redirect,
137: oldPath, newPath);
138:
139: redirect = StringUtil.replace(redirect,
140: HttpUtil.encodeURL(oldPath), HttpUtil
141: .encodeURL(newPath));
142: }
143: }
144:
145: redirect += user.getUserId();
146: }
147:
148: sendRedirect(req, res, redirect);
149: } catch (Exception e) {
150: if (e instanceof NoSuchUserException
151: || e instanceof PrincipalException) {
152:
153: SessionErrors.add(req, e.getClass().getName());
154:
155: setForward(req, "portlet.enterprise_admin.error");
156: } else if (e instanceof ContactFirstNameException
157: || e instanceof ContactLastNameException
158: || e instanceof DuplicateUserEmailAddressException
159: || e instanceof DuplicateUserScreenNameException
160: || e instanceof RequiredUserException
161: || e instanceof ReservedUserEmailAddressException
162: || e instanceof ReservedUserScreenNameException
163: || e instanceof UserEmailAddressException
164: || e instanceof UserIdException
165: || e instanceof UserPasswordException
166: || e instanceof UserScreenNameException
167: || e instanceof UserSmsException) {
168:
169: SessionErrors.add(req, e.getClass().getName(), e);
170:
171: if (e instanceof RequiredUserException) {
172: res.sendRedirect(ParamUtil.getString(req,
173: "redirect"));
174: }
175: } else {
176: throw e;
177: }
178: }
179: }
180:
181: public ActionForward render(ActionMapping mapping, ActionForm form,
182: PortletConfig config, RenderRequest req, RenderResponse res)
183: throws Exception {
184:
185: try {
186: PortalUtil.getSelectedUser(req);
187: } catch (Exception e) {
188: if (e instanceof PrincipalException) {
189: SessionErrors.add(req, e.getClass().getName());
190:
191: return mapping
192: .findForward("portlet.enterprise_admin.error");
193: } else {
194: throw e;
195: }
196: }
197:
198: return mapping.findForward(getForward(req,
199: "portlet.enterprise_admin.edit_user"));
200: }
201:
202: protected void deleteRole(ActionRequest req) throws Exception {
203: User user = PortalUtil.getSelectedUser(req);
204:
205: long roleId = ParamUtil.getLong(req, "roleId");
206:
207: UserServiceUtil.deleteRoleUser(roleId, user.getUserId());
208: }
209:
210: protected void deleteUsers(ActionRequest req) throws Exception {
211: String cmd = ParamUtil.getString(req, Constants.CMD);
212:
213: long[] deleteUserIds = StringUtil.split(ParamUtil.getString(
214: req, "deleteUserIds"), 0L);
215:
216: for (int i = 0; i < deleteUserIds.length; i++) {
217: if (cmd.equals(Constants.DEACTIVATE)
218: || cmd.equals(Constants.RESTORE)) {
219:
220: boolean active = !cmd.equals(Constants.DEACTIVATE);
221:
222: UserServiceUtil.updateActive(deleteUserIds[i], active);
223: } else {
224: UserServiceUtil.deleteUser(deleteUserIds[i]);
225: }
226: }
227: }
228:
229: protected User updateLockout(ActionRequest req) throws Exception {
230: User user = PortalUtil.getSelectedUser(req);
231:
232: UserServiceUtil.updateLockout(user.getUserId(), false);
233:
234: return user;
235: }
236:
237: protected Object[] updateUser(ActionRequest req) throws Exception {
238: String cmd = ParamUtil.getString(req, Constants.CMD);
239:
240: ThemeDisplay themeDisplay = (ThemeDisplay) req
241: .getAttribute(WebKeys.THEME_DISPLAY);
242:
243: boolean autoPassword = ParamUtil.getBoolean(req,
244: "autoPassword", true);
245: String password1 = ParamUtil.getString(req, "password1");
246: String password2 = ParamUtil.getString(req, "password2");
247: boolean autoScreenName = ParamUtil.getBoolean(req,
248: "autoScreenName");
249: String screenName = ParamUtil.getString(req, "screenName");
250: String emailAddress = ParamUtil.getString(req, "emailAddress");
251: String languageId = ParamUtil.getString(req, "languageId");
252: String timeZoneId = ParamUtil.getString(req, "timeZoneId");
253: String greeting = ParamUtil.getString(req, "greeting");
254: String firstName = ParamUtil.getString(req, "firstName");
255: String middleName = ParamUtil.getString(req, "middleName");
256: String lastName = ParamUtil.getString(req, "lastName");
257: int prefixId = ParamUtil.getInteger(req, "prefixId");
258: int suffixId = ParamUtil.getInteger(req, "suffixId");
259: boolean male = ParamUtil.getBoolean(req, "male", true);
260: int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
261: int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
262: int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
263: String comments = ParamUtil.getString(req, "comments");
264: String smsSn = ParamUtil.getString(req, "smsSn");
265: String aimSn = ParamUtil.getString(req, "aimSn");
266: String icqSn = ParamUtil.getString(req, "icqSn");
267: String jabberSn = ParamUtil.getString(req, "jabberSn");
268: String msnSn = ParamUtil.getString(req, "msnSn");
269: String skypeSn = ParamUtil.getString(req, "skypeSn");
270: String ymSn = ParamUtil.getString(req, "ymSn");
271: String jobTitle = ParamUtil.getString(req, "jobTitle");
272: long[] organizationIds = StringUtil.split(ParamUtil.getString(
273: req, "organizationIds"), 0L);
274: boolean sendEmail = true;
275:
276: User user = null;
277: String oldScreenName = StringPool.BLANK;
278:
279: if (cmd.equals(Constants.ADD)) {
280:
281: // Add user
282:
283: user = UserServiceUtil.addUser(themeDisplay.getCompanyId(),
284: autoPassword, password1, password2, autoScreenName,
285: screenName, emailAddress, themeDisplay.getLocale(),
286: firstName, middleName, lastName, prefixId,
287: suffixId, male, birthdayMonth, birthdayDay,
288: birthdayYear, jobTitle, organizationIds, sendEmail);
289: } else {
290:
291: // Update user
292:
293: user = PortalUtil.getSelectedUser(req);
294:
295: String oldPassword = AdminUtil.getUpdateUserPassword(req,
296: user.getUserId());
297: String newPassword1 = ParamUtil.getString(req, "password1");
298: String newPassword2 = ParamUtil.getString(req, "password2");
299: boolean passwordReset = ParamUtil.getBoolean(req,
300: "passwordReset");
301:
302: String tempOldScreenName = user.getScreenName();
303:
304: user = UserServiceUtil.updateUser(user.getUserId(),
305: oldPassword, newPassword1, newPassword2,
306: passwordReset, screenName, emailAddress,
307: languageId, timeZoneId, greeting, comments,
308: firstName, middleName, lastName, prefixId,
309: suffixId, male, birthdayMonth, birthdayDay,
310: birthdayYear, smsSn, aimSn, icqSn, jabberSn, msnSn,
311: skypeSn, ymSn, jobTitle, organizationIds);
312:
313: if (!tempOldScreenName.equals(user.getScreenName())) {
314: oldScreenName = tempOldScreenName;
315: }
316:
317: if (user.getUserId() == themeDisplay.getUserId()) {
318:
319: // Reset the locale
320:
321: HttpServletRequest httpReq = PortalUtil
322: .getHttpServletRequest(req);
323: HttpSession httpSes = httpReq.getSession();
324:
325: httpSes.removeAttribute(Globals.LOCALE_KEY);
326:
327: // Clear cached portlet responses
328:
329: PortletSession ses = req.getPortletSession();
330:
331: CachePortlet.clearResponses(ses);
332:
333: // Password
334:
335: if (Validator.isNotNull(newPassword1)) {
336: ses.setAttribute(WebKeys.USER_PASSWORD,
337: newPassword1,
338: PortletSession.APPLICATION_SCOPE);
339: }
340: }
341:
342: }
343:
344: return new Object[] { user, oldScreenName };
345: }
346:
347: }
|