1   /**
2    * Copyright (c) 2000-2008 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.NoSuchUserException;
30  import com.liferay.portal.RequiredUserException;
31  import com.liferay.portal.ReservedUserEmailAddressException;
32  import com.liferay.portal.ReservedUserScreenNameException;
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.kernel.util.Constants;
39  import com.liferay.portal.kernel.util.HttpUtil;
40  import com.liferay.portal.kernel.util.ParamUtil;
41  import com.liferay.portal.kernel.util.StringPool;
42  import com.liferay.portal.kernel.util.StringUtil;
43  import com.liferay.portal.kernel.util.Validator;
44  import com.liferay.portal.model.Group;
45  import com.liferay.portal.model.Layout;
46  import com.liferay.portal.model.User;
47  import com.liferay.portal.security.auth.PrincipalException;
48  import com.liferay.portal.service.UserServiceUtil;
49  import com.liferay.portal.struts.PortletAction;
50  import com.liferay.portal.theme.ThemeDisplay;
51  import com.liferay.portal.util.PortalUtil;
52  import com.liferay.portal.util.WebKeys;
53  import com.liferay.portlet.InvokerPortlet;
54  import com.liferay.portlet.admin.util.AdminUtil;
55  import com.liferay.portlet.announcements.model.impl.AnnouncementsEntryImpl;
56  import com.liferay.portlet.announcements.service.AnnouncementsDeliveryServiceUtil;
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(Constants.DEACTIVATE) ||
100                      cmd.equals(Constants.DELETE) ||
101                      cmd.equals(Constants.RESTORE)) {
102 
103                 deleteUsers(req);
104             }
105             else if (cmd.equals("deleteRole")) {
106                 deleteRole(req);
107             }
108             else if (cmd.equals("unlock")) {
109                 user = updateLockout(req);
110             }
111 
112             String redirect = ParamUtil.getString(req, "redirect");
113 
114             if (user != null) {
115                 if (Validator.isNotNull(oldScreenName)) {
116 
117                     // This will fix the redirect if the user is on his personal
118                     // my account page and changes his screen name. A redirect
119                     // that references the old screen name no longer points to a
120                     // valid screen name and therefore needs to be updated.
121 
122                     ThemeDisplay themeDisplay =
123                         (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
124 
125                     Group group = user.getGroup();
126 
127                     if (group.getGroupId() ==
128                             themeDisplay.getPortletGroupId()) {
129 
130                         Layout layout = themeDisplay.getLayout();
131 
132                         String friendlyURLPath = group.getPathFriendlyURL(
133                             layout.isPrivateLayout(), themeDisplay);
134 
135                         String oldPath =
136                             friendlyURLPath + StringPool.SLASH + oldScreenName;
137                         String newPath =
138                             friendlyURLPath + StringPool.SLASH +
139                                 user.getScreenName();
140 
141                         redirect = StringUtil.replace(
142                             redirect, oldPath, newPath);
143 
144                         redirect = StringUtil.replace(
145                             redirect, HttpUtil.encodeURL(oldPath),
146                             HttpUtil.encodeURL(newPath));
147                     }
148                 }
149 
150                 redirect += user.getUserId();
151             }
152 
153             sendRedirect(req, res, redirect);
154         }
155         catch (Exception e) {
156             if (e instanceof NoSuchUserException ||
157                 e instanceof PrincipalException) {
158 
159                 SessionErrors.add(req, e.getClass().getName());
160 
161                 setForward(req, "portlet.enterprise_admin.error");
162             }
163             else if (e instanceof ContactFirstNameException ||
164                      e instanceof ContactLastNameException ||
165                      e instanceof DuplicateUserEmailAddressException ||
166                      e instanceof DuplicateUserScreenNameException ||
167                      e instanceof RequiredUserException ||
168                      e instanceof ReservedUserEmailAddressException ||
169                      e instanceof ReservedUserScreenNameException ||
170                      e instanceof UserEmailAddressException ||
171                      e instanceof UserIdException ||
172                      e instanceof UserPasswordException ||
173                      e instanceof UserScreenNameException ||
174                      e instanceof UserSmsException) {
175 
176                 SessionErrors.add(req, e.getClass().getName(), e);
177 
178                 if (e instanceof RequiredUserException) {
179                     res.sendRedirect(ParamUtil.getString(req, "redirect"));
180                 }
181             }
182             else {
183                 throw e;
184             }
185         }
186     }
187 
188     public ActionForward render(
189             ActionMapping mapping, ActionForm form, PortletConfig config,
190             RenderRequest req, RenderResponse res)
191         throws Exception {
192 
193         try {
194             PortalUtil.getSelectedUser(req);
195         }
196         catch (Exception e) {
197             if (e instanceof PrincipalException) {
198                 SessionErrors.add(req, e.getClass().getName());
199 
200                 return mapping.findForward("portlet.enterprise_admin.error");
201             }
202             else {
203                 throw e;
204             }
205         }
206 
207         return mapping.findForward(
208             getForward(req, "portlet.enterprise_admin.edit_user"));
209     }
210 
211     protected void deleteRole(ActionRequest req) throws Exception {
212         User user = PortalUtil.getSelectedUser(req);
213 
214         long roleId = ParamUtil.getLong(req, "roleId");
215 
216         UserServiceUtil.deleteRoleUser(roleId, user.getUserId());
217     }
218 
219     protected void deleteUsers(ActionRequest req) throws Exception {
220         String cmd = ParamUtil.getString(req, Constants.CMD);
221 
222         long[] deleteUserIds = StringUtil.split(
223             ParamUtil.getString(req, "deleteUserIds"), 0L);
224 
225         for (int i = 0; i < deleteUserIds.length; i++) {
226             if (cmd.equals(Constants.DEACTIVATE) ||
227                 cmd.equals(Constants.RESTORE)) {
228 
229                 boolean active = !cmd.equals(Constants.DEACTIVATE);
230 
231                 UserServiceUtil.updateActive(deleteUserIds[i], active);
232             }
233             else {
234                 UserServiceUtil.deleteUser(deleteUserIds[i]);
235             }
236         }
237     }
238 
239     protected User updateLockout(ActionRequest req) throws Exception {
240         User user = PortalUtil.getSelectedUser(req);
241 
242         UserServiceUtil.updateLockout(user.getUserId(), false);
243 
244         return user;
245     }
246 
247     protected Object[] updateUser(ActionRequest req) throws Exception {
248         String cmd = ParamUtil.getString(req, Constants.CMD);
249 
250         ThemeDisplay themeDisplay =
251             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
252 
253         boolean autoPassword = ParamUtil.getBoolean(req, "autoPassword", true);
254         String password1 = ParamUtil.getString(req, "password1");
255         String password2 = ParamUtil.getString(req, "password2");
256         boolean autoScreenName = ParamUtil.getBoolean(req, "autoScreenName");
257         String screenName = ParamUtil.getString(req, "screenName");
258         String emailAddress = ParamUtil.getString(req, "emailAddress");
259         String languageId = ParamUtil.getString(req, "languageId");
260         String timeZoneId = ParamUtil.getString(req, "timeZoneId");
261         String greeting = ParamUtil.getString(req, "greeting");
262         String firstName = ParamUtil.getString(req, "firstName");
263         String middleName = ParamUtil.getString(req, "middleName");
264         String lastName = ParamUtil.getString(req, "lastName");
265         int prefixId = ParamUtil.getInteger(req, "prefixId");
266         int suffixId = ParamUtil.getInteger(req, "suffixId");
267         boolean male = ParamUtil.getBoolean(req, "male", true);
268         int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
269         int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
270         int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
271         String comments = ParamUtil.getString(req, "comments");
272         String smsSn = ParamUtil.getString(req, "smsSn");
273         String aimSn = ParamUtil.getString(req, "aimSn");
274         String icqSn = ParamUtil.getString(req, "icqSn");
275         String jabberSn = ParamUtil.getString(req, "jabberSn");
276         String msnSn = ParamUtil.getString(req, "msnSn");
277         String skypeSn = ParamUtil.getString(req, "skypeSn");
278         String ymSn = ParamUtil.getString(req, "ymSn");
279         String jobTitle = ParamUtil.getString(req, "jobTitle");
280         long[] organizationIds = StringUtil.split(
281             ParamUtil.getString(req, "organizationIds"),  0L);
282         boolean sendEmail = true;
283 
284         User user = null;
285         String oldScreenName = StringPool.BLANK;
286 
287         if (cmd.equals(Constants.ADD)) {
288 
289             // Add user
290 
291             user = UserServiceUtil.addUser(
292                 themeDisplay.getCompanyId(), autoPassword, password1, password2,
293                 autoScreenName, screenName, emailAddress,
294                 themeDisplay.getLocale(), firstName, middleName, lastName,
295                 prefixId, suffixId, male, birthdayMonth, birthdayDay,
296                 birthdayYear, jobTitle, organizationIds, sendEmail);
297         }
298         else {
299 
300             // Update user
301 
302             user = PortalUtil.getSelectedUser(req);
303 
304             String oldPassword = AdminUtil.getUpdateUserPassword(
305                 req, user.getUserId());
306             String newPassword1 = ParamUtil.getString(req, "password1");
307             String newPassword2 = ParamUtil.getString(req, "password2");
308             boolean passwordReset = ParamUtil.getBoolean(req, "passwordReset");
309 
310             String tempOldScreenName = user.getScreenName();
311 
312             user = UserServiceUtil.updateUser(
313                 user.getUserId(), oldPassword, newPassword1, newPassword2,
314                 passwordReset, screenName, emailAddress, languageId, timeZoneId,
315                 greeting, comments, firstName, middleName, lastName, prefixId,
316                 suffixId, male, birthdayMonth, birthdayDay, birthdayYear, smsSn,
317                 aimSn, icqSn, jabberSn, msnSn, skypeSn, ymSn, jobTitle,
318                 organizationIds);
319 
320             for (String type : AnnouncementsEntryImpl.TYPES) {
321                 boolean email = ParamUtil.getBoolean(
322                     req, "announcementsType" + type + "Email");
323                 boolean sms = ParamUtil.getBoolean(
324                     req, "announcementsType" + type + "Sms");
325                 boolean website = ParamUtil.getBoolean(
326                     req, "announcementsType" + type + "Website");
327 
328                 AnnouncementsDeliveryServiceUtil.updateDelivery(
329                     user.getUserId(), type, email, sms, website);
330             }
331 
332             if (!tempOldScreenName.equals(user.getScreenName())) {
333                 oldScreenName = tempOldScreenName;
334             }
335 
336             if (user.getUserId() == themeDisplay.getUserId()) {
337 
338                 // Reset the locale
339 
340                 HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(
341                     req);
342                 HttpSession httpSes = httpReq.getSession();
343 
344                 httpSes.removeAttribute(Globals.LOCALE_KEY);
345 
346                 // Clear cached portlet responses
347 
348                 PortletSession ses = req.getPortletSession();
349 
350                 InvokerPortlet.clearResponses(ses);
351 
352                 // Password
353 
354                 if (Validator.isNotNull(newPassword1)) {
355                     ses.setAttribute(
356                         WebKeys.USER_PASSWORD, newPassword1,
357                         PortletSession.APPLICATION_SCOPE);
358                 }
359             }
360         }
361 
362         return new Object[] {user, oldScreenName};
363     }
364 
365 }