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