1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.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.NoSuchUserException;
31  import com.liferay.portal.OrganizationParentException;
32  import com.liferay.portal.RequiredUserException;
33  import com.liferay.portal.ReservedUserEmailAddressException;
34  import com.liferay.portal.ReservedUserScreenNameException;
35  import com.liferay.portal.UserEmailAddressException;
36  import com.liferay.portal.UserIdException;
37  import com.liferay.portal.UserPasswordException;
38  import com.liferay.portal.UserScreenNameException;
39  import com.liferay.portal.UserSmsException;
40  import com.liferay.portal.kernel.util.Constants;
41  import com.liferay.portal.kernel.util.ParamUtil;
42  import com.liferay.portal.kernel.util.StringPool;
43  import com.liferay.portal.kernel.util.StringUtil;
44  import com.liferay.portal.kernel.util.Validator;
45  import com.liferay.portal.model.Contact;
46  import com.liferay.portal.model.Group;
47  import com.liferay.portal.model.Layout;
48  import com.liferay.portal.model.User;
49  import com.liferay.portal.security.auth.PrincipalException;
50  import com.liferay.portal.service.UserServiceUtil;
51  import com.liferay.portal.struts.PortletAction;
52  import com.liferay.portal.theme.ThemeDisplay;
53  import com.liferay.portal.util.PortalUtil;
54  import com.liferay.portal.util.WebKeys;
55  import com.liferay.portlet.CachePortlet;
56  import com.liferay.portlet.admin.util.AdminUtil;
57  import com.liferay.util.servlet.SessionErrors;
58  
59  import javax.portlet.ActionRequest;
60  import javax.portlet.ActionResponse;
61  import javax.portlet.PortletConfig;
62  import javax.portlet.PortletSession;
63  import javax.portlet.RenderRequest;
64  import javax.portlet.RenderResponse;
65  
66  import javax.servlet.http.HttpServletRequest;
67  import javax.servlet.http.HttpSession;
68  
69  import org.apache.struts.Globals;
70  import org.apache.struts.action.ActionForm;
71  import org.apache.struts.action.ActionForward;
72  import org.apache.struts.action.ActionMapping;
73  
74  /**
75   * <a href="EditUserAction.java.html"><b><i>View Source</i></b></a>
76   *
77   * @author Brian Wing Shun Chan
78   *
79   */
80  public class EditUserAction extends PortletAction {
81  
82      public void processAction(
83              ActionMapping mapping, ActionForm form, PortletConfig config,
84              ActionRequest req, ActionResponse res)
85          throws Exception {
86  
87          String cmd = ParamUtil.getString(req, Constants.CMD);
88  
89          try {
90              User user = null;
91              String oldScreenName = StringPool.BLANK;
92  
93              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
94                  Object[] returnValue = updateUser(req);
95  
96                  user = (User)returnValue[0];
97                  oldScreenName = ((String)returnValue[1]);
98              }
99              else if (cmd.equals("comments")) {
100                 user = updateComments(req);
101             }
102             else if (cmd.equals(Constants.DEACTIVATE) ||
103                      cmd.equals(Constants.DELETE) ||
104                      cmd.equals(Constants.RESTORE)) {
105 
106                 deleteUsers(req);
107             }
108             else if (cmd.equals("deleteRole")) {
109                 deleteRole(req);
110             }
111             else if (cmd.equals("display")) {
112                 user = updateDisplay(req);
113             }
114             else if (cmd.equals("im")) {
115                 user = updateIm(req);
116             }
117             else if (cmd.equals("password")) {
118                 user = updatePassword(req);
119             }
120             else if (cmd.equals("sms")) {
121                 user = updateSms(req);
122             }
123             else if (cmd.equals("unlock")) {
124                 user = updateLockout(req);
125             }
126 
127             String redirect = ParamUtil.getString(req, "redirect");
128 
129             if (user != null) {
130                 if (Validator.isNotNull(oldScreenName)) {
131 
132                     // This will fix the redirect if the user is on his personal
133                     // my account page and changes his screen name. A redirect
134                     // that references the old screen name no longer points to a
135                     // valid screen name and therefore needs to be updated.
136 
137                     ThemeDisplay themeDisplay =
138                         (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
139 
140                     Group group = user.getGroup();
141 
142                     if (group.getGroupId() ==
143                             themeDisplay.getPortletGroupId()) {
144 
145                         Layout layout = themeDisplay.getLayout();
146 
147                         String friendlyURLPath = group.getPathFriendlyURL(
148                             layout.isPrivateLayout(), themeDisplay);
149 
150                         redirect = StringUtil.replace(
151                             redirect,
152                             friendlyURLPath + StringPool.SLASH + oldScreenName,
153                             friendlyURLPath + StringPool.SLASH +
154                                 user.getScreenName());
155                     }
156                 }
157 
158                 redirect += user.getUserId();
159             }
160 
161             sendRedirect(req, res, redirect);
162         }
163         catch (Exception e) {
164             if (e instanceof NoSuchUserException ||
165                 e instanceof PrincipalException) {
166 
167                 SessionErrors.add(req, e.getClass().getName());
168 
169                 setForward(req, "portlet.enterprise_admin.error");
170             }
171             else if (e instanceof ContactFirstNameException ||
172                      e instanceof ContactLastNameException ||
173                      e instanceof DuplicateUserEmailAddressException ||
174                      e instanceof DuplicateUserScreenNameException ||
175                      e instanceof NoSuchOrganizationException ||
176                      e instanceof OrganizationParentException ||
177                      e instanceof RequiredUserException ||
178                      e instanceof ReservedUserEmailAddressException ||
179                      e instanceof ReservedUserScreenNameException ||
180                      e instanceof UserEmailAddressException ||
181                      e instanceof UserIdException ||
182                      e instanceof UserPasswordException ||
183                      e instanceof UserScreenNameException ||
184                      e instanceof UserSmsException) {
185 
186                 SessionErrors.add(req, e.getClass().getName(), e);
187 
188                 if (e instanceof RequiredUserException) {
189                     res.sendRedirect(ParamUtil.getString(req, "redirect"));
190                 }
191             }
192             else {
193                 throw e;
194             }
195         }
196     }
197 
198     public ActionForward render(
199             ActionMapping mapping, ActionForm form, PortletConfig config,
200             RenderRequest req, RenderResponse res)
201         throws Exception {
202 
203         try {
204             PortalUtil.getSelectedUser(req);
205         }
206         catch (Exception e) {
207             if (e instanceof PrincipalException) {
208                 SessionErrors.add(req, e.getClass().getName());
209 
210                 return mapping.findForward("portlet.enterprise_admin.error");
211             }
212             else {
213                 throw e;
214             }
215         }
216 
217         return mapping.findForward(
218             getForward(req, "portlet.enterprise_admin.edit_user"));
219     }
220 
221     protected void deleteRole(ActionRequest req) throws Exception {
222         User user = PortalUtil.getSelectedUser(req);
223 
224         long roleId = ParamUtil.getLong(req, "roleId");
225 
226         UserServiceUtil.deleteRoleUser(roleId, user.getUserId());
227     }
228 
229     protected void deleteUsers(ActionRequest req) throws Exception {
230         String cmd = ParamUtil.getString(req, Constants.CMD);
231 
232         long[] deleteUserIds = StringUtil.split(
233             ParamUtil.getString(req, "deleteUserIds"), 0L);
234 
235         for (int i = 0; i < deleteUserIds.length; i++) {
236             if (cmd.equals(Constants.DEACTIVATE) ||
237                 cmd.equals(Constants.RESTORE)) {
238 
239                 boolean active = !cmd.equals(Constants.DEACTIVATE);
240 
241                 UserServiceUtil.updateActive(deleteUserIds[i], active);
242             }
243             else {
244                 UserServiceUtil.deleteUser(deleteUserIds[i]);
245             }
246         }
247     }
248 
249     protected User updateComments(ActionRequest req) throws Exception {
250         String comments = ParamUtil.getString(req, "comments");
251 
252         User user = PortalUtil.getSelectedUser(req);
253 
254         Contact contact = user.getContact();
255 
256         AdminUtil.updateUser(
257             req, user.getUserId(), user.getScreenName(), user.getEmailAddress(),
258             user.getLanguageId(), user.getTimeZoneId(), user.getGreeting(),
259             comments, contact.getSmsSn(), contact.getAimSn(),
260             contact.getIcqSn(), contact.getJabberSn(), contact.getMsnSn(),
261             contact.getSkypeSn(), contact.getYmSn());
262 
263         return user;
264     }
265 
266     protected User updateDisplay(ActionRequest req) throws Exception {
267         PortletSession ses = req.getPortletSession();
268 
269         String languageId = ParamUtil.getString(req, "languageId");
270         String timeZoneId = ParamUtil.getString(req, "timeZoneId");
271         String greeting = ParamUtil.getString(req, "greeting");
272 
273         User user = PortalUtil.getSelectedUser(req);
274 
275         Contact contact = user.getContact();
276 
277         AdminUtil.updateUser(
278             req, user.getUserId(), user.getScreenName(), user.getEmailAddress(),
279             languageId, timeZoneId, greeting, user.getComments(),
280             contact.getSmsSn(), contact.getAimSn(), contact.getIcqSn(),
281             contact.getJabberSn(), contact.getMsnSn(), contact.getSkypeSn(),
282             contact.getYmSn());
283 
284         // Reset the locale
285 
286         HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
287         HttpSession httpSes = httpReq.getSession();
288 
289         httpSes.removeAttribute(Globals.LOCALE_KEY);
290 
291         // Clear cached portlet responses
292 
293         CachePortlet.clearResponses(ses);
294 
295         return user;
296     }
297 
298     protected User updateIm(ActionRequest req) throws Exception {
299         String aimSn = ParamUtil.getString(req, "aimSn");
300         String icqSn = ParamUtil.getString(req, "icqSn");
301         String jabberSn = ParamUtil.getString(req, "jabberSn");
302         String msnSn = ParamUtil.getString(req, "msnSn");
303         String skypeSn = ParamUtil.getString(req, "skypeSn");
304         String ymSn = ParamUtil.getString(req, "ymSn");
305 
306         User user = PortalUtil.getSelectedUser(req);
307 
308         Contact contact = user.getContact();
309 
310         AdminUtil.updateUser(
311             req, user.getUserId(), user.getScreenName(), user.getEmailAddress(),
312             user.getLanguageId(), user.getTimeZoneId(), user.getGreeting(),
313             user.getComments(), contact.getSmsSn(), aimSn, icqSn, jabberSn,
314             msnSn, skypeSn, ymSn);
315 
316         return user;
317     }
318 
319     protected User updateLockout(ActionRequest req) throws Exception {
320         User user = PortalUtil.getSelectedUser(req);
321 
322         UserServiceUtil.updateLockout(user.getUserId(), false);
323 
324         return user;
325     }
326 
327     protected User updatePassword(ActionRequest req) throws Exception {
328         PortletSession ses = req.getPortletSession();
329 
330         String password1 = ParamUtil.getString(req, "password1");
331         String password2 = ParamUtil.getString(req, "password2");
332         boolean passwordReset = ParamUtil.getBoolean(req, "passwordReset");
333 
334         User user = PortalUtil.getSelectedUser(req);
335 
336         UserServiceUtil.updatePassword(
337             user.getUserId(), password1, password2, passwordReset);
338 
339         if (user.getUserId() == PortalUtil.getUserId(req)) {
340             ses.setAttribute(
341                 WebKeys.USER_PASSWORD, password1,
342                 PortletSession.APPLICATION_SCOPE);
343         }
344 
345         return user;
346     }
347 
348     protected User updateSms(ActionRequest req) throws Exception {
349         String smsSn = ParamUtil.getString(req, "smsSn");
350 
351         User user = PortalUtil.getSelectedUser(req);
352 
353         Contact contact = user.getContact();
354 
355         AdminUtil.updateUser(
356             req, user.getUserId(), user.getScreenName(), user.getEmailAddress(),
357             user.getLanguageId(), user.getTimeZoneId(), user.getGreeting(),
358             user.getComments(), smsSn, contact.getAimSn(), contact.getIcqSn(),
359             contact.getJabberSn(), contact.getMsnSn(), contact.getSkypeSn(),
360             contact.getYmSn());
361 
362         return user;
363     }
364 
365     protected Object[] updateUser(ActionRequest req) throws Exception {
366         String cmd = ParamUtil.getString(req, Constants.CMD);
367 
368         ThemeDisplay themeDisplay =
369             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
370 
371         boolean autoPassword = true;
372         String password1 = null;
373         String password2 = null;
374         boolean autoScreenName = false;
375         String screenName = ParamUtil.getString(req, "screenName");
376         String emailAddress = ParamUtil.getString(req, "emailAddress");
377         String firstName = ParamUtil.getString(req, "firstName");
378         String middleName = ParamUtil.getString(req, "middleName");
379         String lastName = ParamUtil.getString(req, "lastName");
380         int prefixId = ParamUtil.getInteger(req, "prefixId");
381         int suffixId = ParamUtil.getInteger(req, "suffixId");
382         boolean male = ParamUtil.get(req, "male", true);
383         int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
384         int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
385         int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
386         String jobTitle = ParamUtil.getString(req, "jobTitle");
387         long organizationId = ParamUtil.getLong(req, "organizationId");
388         long locationId = ParamUtil.getLong(req, "locationId");
389         boolean sendEmail = true;
390 
391         User user = null;
392         String oldScreenName = StringPool.BLANK;
393 
394         if (cmd.equals(Constants.ADD)) {
395 
396             // Add user
397 
398             user = UserServiceUtil.addUser(
399                 themeDisplay.getCompanyId(), autoPassword, password1, password2,
400                 autoScreenName, screenName, emailAddress,
401                 themeDisplay.getLocale(), firstName, middleName, lastName,
402                 prefixId, suffixId, male, birthdayMonth, birthdayDay,
403                 birthdayYear, jobTitle, organizationId, locationId, sendEmail);
404         }
405         else {
406 
407             // Update user
408 
409             user = PortalUtil.getSelectedUser(req);
410 
411             String password = AdminUtil.getUpdateUserPassword(
412                 req, user.getUserId());
413 
414             Contact contact = user.getContact();
415 
416             String tempOldScreenName = user.getScreenName();
417 
418             user = UserServiceUtil.updateUser(
419                 user.getUserId(), password, screenName, emailAddress,
420                 user.getLanguageId(), user.getTimeZoneId(), user.getGreeting(),
421                 user.getComments(), firstName, middleName, lastName, prefixId,
422                 suffixId, male, birthdayMonth, birthdayDay, birthdayYear,
423                 contact.getSmsSn(), contact.getAimSn(), contact.getIcqSn(),
424                 contact.getJabberSn(), contact.getMsnSn(), contact.getSkypeSn(),
425                 contact.getYmSn(), jobTitle, organizationId, locationId);
426 
427             if (!tempOldScreenName.equals(user.getScreenName())) {
428                 oldScreenName = tempOldScreenName;
429             }
430         }
431 
432         return new Object[] {user, oldScreenName};
433     }
434 
435 }