1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.enterpriseadmin.action;
21  
22  import com.liferay.portal.ContactFirstNameException;
23  import com.liferay.portal.ContactLastNameException;
24  import com.liferay.portal.DuplicateUserEmailAddressException;
25  import com.liferay.portal.DuplicateUserScreenNameException;
26  import com.liferay.portal.NoSuchUserException;
27  import com.liferay.portal.RequiredUserException;
28  import com.liferay.portal.ReservedUserEmailAddressException;
29  import com.liferay.portal.ReservedUserScreenNameException;
30  import com.liferay.portal.UserEmailAddressException;
31  import com.liferay.portal.UserIdException;
32  import com.liferay.portal.UserPasswordException;
33  import com.liferay.portal.UserScreenNameException;
34  import com.liferay.portal.UserSmsException;
35  import com.liferay.portal.kernel.servlet.SessionErrors;
36  import com.liferay.portal.kernel.util.Constants;
37  import com.liferay.portal.kernel.util.HttpUtil;
38  import com.liferay.portal.kernel.util.ParamUtil;
39  import com.liferay.portal.kernel.util.StringPool;
40  import com.liferay.portal.kernel.util.StringUtil;
41  import com.liferay.portal.kernel.util.Validator;
42  import com.liferay.portal.model.Group;
43  import com.liferay.portal.model.Layout;
44  import com.liferay.portal.model.User;
45  import com.liferay.portal.security.auth.PrincipalException;
46  import com.liferay.portal.service.UserServiceUtil;
47  import com.liferay.portal.struts.PortletAction;
48  import com.liferay.portal.theme.ThemeDisplay;
49  import com.liferay.portal.util.PortalUtil;
50  import com.liferay.portal.util.WebKeys;
51  import com.liferay.portlet.InvokerPortletImpl;
52  import com.liferay.portlet.admin.util.AdminUtil;
53  import com.liferay.portlet.announcements.model.impl.AnnouncementsEntryImpl;
54  import com.liferay.portlet.announcements.service.AnnouncementsDeliveryServiceUtil;
55  
56  import javax.portlet.ActionRequest;
57  import javax.portlet.ActionResponse;
58  import javax.portlet.PortletConfig;
59  import javax.portlet.PortletSession;
60  import javax.portlet.RenderRequest;
61  import javax.portlet.RenderResponse;
62  
63  import javax.servlet.http.HttpServletRequest;
64  import javax.servlet.http.HttpSession;
65  
66  import org.apache.struts.Globals;
67  import org.apache.struts.action.ActionForm;
68  import org.apache.struts.action.ActionForward;
69  import org.apache.struts.action.ActionMapping;
70  
71  /**
72   * <a href="EditUserAction.java.html"><b><i>View Source</i></b></a>
73   *
74   * @author Brian Wing Shun Chan
75   *
76   */
77  public class EditUserAction extends PortletAction {
78  
79      public void processAction(
80              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
81              ActionRequest actionRequest, ActionResponse actionResponse)
82          throws Exception {
83  
84          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
85  
86          try {
87              User user = null;
88              String oldScreenName = StringPool.BLANK;
89  
90              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
91                  Object[] returnValue = updateUser(actionRequest);
92  
93                  user = (User)returnValue[0];
94                  oldScreenName = ((String)returnValue[1]);
95              }
96              else if (cmd.equals(Constants.DEACTIVATE) ||
97                       cmd.equals(Constants.DELETE) ||
98                       cmd.equals(Constants.RESTORE)) {
99  
100                 deleteUsers(actionRequest);
101             }
102             else if (cmd.equals("deleteRole")) {
103                 deleteRole(actionRequest);
104             }
105             else if (cmd.equals("unlock")) {
106                 user = updateLockout(actionRequest);
107             }
108 
109             String redirect = ParamUtil.getString(actionRequest, "redirect");
110 
111             if (user != null) {
112                 if (Validator.isNotNull(oldScreenName)) {
113 
114                     // This will fix the redirect if the user is on his personal
115                     // my account page and changes his screen name. A redirect
116                     // that references the old screen name no longer points to a
117                     // valid screen name and therefore needs to be updated.
118 
119                     ThemeDisplay themeDisplay =
120                         (ThemeDisplay)actionRequest.getAttribute(
121                             WebKeys.THEME_DISPLAY);
122 
123                     Group group = user.getGroup();
124 
125                     if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
126                         Layout layout = themeDisplay.getLayout();
127 
128                         String friendlyURLPath = group.getPathFriendlyURL(
129                             layout.isPrivateLayout(), themeDisplay);
130 
131                         String oldPath =
132                             friendlyURLPath + StringPool.SLASH + oldScreenName;
133                         String newPath =
134                             friendlyURLPath + StringPool.SLASH +
135                                 user.getScreenName();
136 
137                         redirect = StringUtil.replace(
138                             redirect, oldPath, newPath);
139 
140                         redirect = StringUtil.replace(
141                             redirect, HttpUtil.encodeURL(oldPath),
142                             HttpUtil.encodeURL(newPath));
143                     }
144                 }
145 
146                 redirect += user.getUserId();
147             }
148 
149             sendRedirect(actionRequest, actionResponse, redirect);
150         }
151         catch (Exception e) {
152             if (e instanceof NoSuchUserException ||
153                 e instanceof PrincipalException) {
154 
155                 SessionErrors.add(actionRequest, e.getClass().getName());
156 
157                 setForward(actionRequest, "portlet.enterprise_admin.error");
158             }
159             else if (e instanceof ContactFirstNameException ||
160                      e instanceof ContactLastNameException ||
161                      e instanceof DuplicateUserEmailAddressException ||
162                      e instanceof DuplicateUserScreenNameException ||
163                      e instanceof RequiredUserException ||
164                      e instanceof ReservedUserEmailAddressException ||
165                      e instanceof ReservedUserScreenNameException ||
166                      e instanceof UserEmailAddressException ||
167                      e instanceof UserIdException ||
168                      e instanceof UserPasswordException ||
169                      e instanceof UserScreenNameException ||
170                      e instanceof UserSmsException) {
171 
172                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
173 
174                 if (e instanceof RequiredUserException) {
175                     actionResponse.sendRedirect(
176                         ParamUtil.getString(actionRequest, "redirect"));
177                 }
178             }
179             else {
180                 throw e;
181             }
182         }
183     }
184 
185     public ActionForward render(
186             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
187             RenderRequest renderRequest, RenderResponse renderResponse)
188         throws Exception {
189 
190         try {
191             PortalUtil.getSelectedUser(renderRequest);
192         }
193         catch (Exception e) {
194             if (e instanceof PrincipalException) {
195                 SessionErrors.add(renderRequest, e.getClass().getName());
196 
197                 return mapping.findForward("portlet.enterprise_admin.error");
198             }
199             else {
200                 throw e;
201             }
202         }
203 
204         return mapping.findForward(
205             getForward(renderRequest, "portlet.enterprise_admin.edit_user"));
206     }
207 
208     protected void deleteRole(ActionRequest actionRequest) throws Exception {
209         User user = PortalUtil.getSelectedUser(actionRequest);
210 
211         long roleId = ParamUtil.getLong(actionRequest, "roleId");
212 
213         UserServiceUtil.deleteRoleUser(roleId, user.getUserId());
214     }
215 
216     protected void deleteUsers(ActionRequest actionRequest) throws Exception {
217         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
218 
219         long[] deleteUserIds = StringUtil.split(
220             ParamUtil.getString(actionRequest, "deleteUserIds"), 0L);
221 
222         for (int i = 0; i < deleteUserIds.length; i++) {
223             if (cmd.equals(Constants.DEACTIVATE) ||
224                 cmd.equals(Constants.RESTORE)) {
225 
226                 boolean active = !cmd.equals(Constants.DEACTIVATE);
227 
228                 UserServiceUtil.updateActive(deleteUserIds[i], active);
229             }
230             else {
231                 UserServiceUtil.deleteUser(deleteUserIds[i]);
232             }
233         }
234     }
235 
236     protected User updateLockout(ActionRequest actionRequest) throws Exception {
237         User user = PortalUtil.getSelectedUser(actionRequest);
238 
239         UserServiceUtil.updateLockout(user.getUserId(), false);
240 
241         return user;
242     }
243 
244     protected Object[] updateUser(ActionRequest actionRequest)
245         throws Exception {
246 
247         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
248 
249         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
250             WebKeys.THEME_DISPLAY);
251 
252         boolean autoPassword = ParamUtil.getBoolean(
253             actionRequest, "autoPassword", true);
254         String password1 = ParamUtil.getString(actionRequest, "password1");
255         String password2 = ParamUtil.getString(actionRequest, "password2");
256         boolean autoScreenName = ParamUtil.getBoolean(
257             actionRequest, "autoScreenName");
258         String screenName = ParamUtil.getString(actionRequest, "screenName");
259         String emailAddress = ParamUtil.getString(
260             actionRequest, "emailAddress");
261         String languageId = ParamUtil.getString(actionRequest, "languageId");
262         String timeZoneId = ParamUtil.getString(actionRequest, "timeZoneId");
263         String greeting = ParamUtil.getString(actionRequest, "greeting");
264         String firstName = ParamUtil.getString(actionRequest, "firstName");
265         String middleName = ParamUtil.getString(actionRequest, "middleName");
266         String lastName = ParamUtil.getString(actionRequest, "lastName");
267         int prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
268         int suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
269         boolean male = ParamUtil.getBoolean(actionRequest, "male", true);
270         int birthdayMonth = ParamUtil.getInteger(
271             actionRequest, "birthdayMonth");
272         int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
273         int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
274         String comments = ParamUtil.getString(actionRequest, "comments");
275         String smsSn = ParamUtil.getString(actionRequest, "smsSn");
276         String aimSn = ParamUtil.getString(actionRequest, "aimSn");
277         String facebookSn = ParamUtil.getString(actionRequest, "facebookSn");
278         String icqSn = ParamUtil.getString(actionRequest, "icqSn");
279         String jabberSn = ParamUtil.getString(actionRequest, "jabberSn");
280         String msnSn = ParamUtil.getString(actionRequest, "msnSn");
281         String mySpaceSn = ParamUtil.getString(actionRequest, "mySpaceSn");
282         String skypeSn = ParamUtil.getString(actionRequest, "skypeSn");
283         String twitterSn = ParamUtil.getString(actionRequest, "twitterSn");
284         String ymSn = ParamUtil.getString(actionRequest, "ymSn");
285         String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
286         long[] organizationIds = StringUtil.split(
287             ParamUtil.getString(actionRequest, "organizationIds"),  0L);
288         boolean sendEmail = true;
289 
290         User user = null;
291         String oldScreenName = StringPool.BLANK;
292 
293         if (cmd.equals(Constants.ADD)) {
294 
295             // Add user
296 
297             user = UserServiceUtil.addUser(
298                 themeDisplay.getCompanyId(), autoPassword, password1, password2,
299                 autoScreenName, screenName, emailAddress,
300                 themeDisplay.getLocale(), firstName, middleName, lastName,
301                 prefixId, suffixId, male, birthdayMonth, birthdayDay,
302                 birthdayYear, jobTitle, organizationIds, sendEmail);
303         }
304         else {
305 
306             // Update user
307 
308             user = PortalUtil.getSelectedUser(actionRequest);
309 
310             String oldPassword = AdminUtil.getUpdateUserPassword(
311                 actionRequest, user.getUserId());
312             String newPassword1 = ParamUtil.getString(
313                 actionRequest, "password1");
314             String newPassword2 = ParamUtil.getString(
315                 actionRequest, "password2");
316             boolean passwordReset = ParamUtil.getBoolean(
317                 actionRequest, "passwordReset");
318 
319             String tempOldScreenName = user.getScreenName();
320 
321             user = UserServiceUtil.updateUser(
322                 user.getUserId(), oldPassword, newPassword1, newPassword2,
323                 passwordReset, screenName, emailAddress, languageId, timeZoneId,
324                 greeting, comments, firstName, middleName, lastName, prefixId,
325                 suffixId, male, birthdayMonth, birthdayDay, birthdayYear, smsSn,
326                 aimSn, facebookSn, icqSn, jabberSn, msnSn, mySpaceSn, skypeSn,
327                 twitterSn, ymSn, jobTitle, organizationIds);
328 
329             String openId = ParamUtil.getString(actionRequest, "openId");
330 
331             if (!openId.equals(user.getOpenId())) {
332                 UserServiceUtil.updateOpenId(user.getUserId(), openId);
333             }
334 
335             for (String type : AnnouncementsEntryImpl.TYPES) {
336                 boolean email = ParamUtil.getBoolean(
337                     actionRequest, "announcementsType" + type + "Email");
338                 boolean sms = ParamUtil.getBoolean(
339                     actionRequest, "announcementsType" + type + "Sms");
340                 boolean website = ParamUtil.getBoolean(
341                     actionRequest, "announcementsType" + type + "Website");
342 
343                 AnnouncementsDeliveryServiceUtil.updateDelivery(
344                     user.getUserId(), type, email, sms, website);
345             }
346 
347             if (!tempOldScreenName.equals(user.getScreenName())) {
348                 oldScreenName = tempOldScreenName;
349             }
350 
351             if (user.getUserId() == themeDisplay.getUserId()) {
352 
353                 // Reset the locale
354 
355                 HttpServletRequest request = PortalUtil.getHttpServletRequest(
356                     actionRequest);
357                 HttpSession session = request.getSession();
358 
359                 session.removeAttribute(Globals.LOCALE_KEY);
360 
361                 // Clear cached portlet responses
362 
363                 PortletSession portletSession =
364                     actionRequest.getPortletSession();
365 
366                 InvokerPortletImpl.clearResponses(portletSession);
367 
368                 // Password
369 
370                 if (Validator.isNotNull(newPassword1)) {
371                     portletSession.setAttribute(
372                         WebKeys.USER_PASSWORD, newPassword1,
373                         PortletSession.APPLICATION_SCOPE);
374                 }
375             }
376         }
377 
378         return new Object[] {user, oldScreenName};
379     }
380 
381 }