1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.servlet.SessionErrors;
39  import com.liferay.portal.kernel.util.Constants;
40  import com.liferay.portal.kernel.util.HttpUtil;
41  import com.liferay.portal.kernel.util.LocaleUtil;
42  import com.liferay.portal.kernel.util.ParamUtil;
43  import com.liferay.portal.kernel.util.StringPool;
44  import com.liferay.portal.kernel.util.StringUtil;
45  import com.liferay.portal.kernel.util.Validator;
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.InvokerPortletImpl;
56  import com.liferay.portlet.admin.util.AdminUtil;
57  import com.liferay.portlet.announcements.model.impl.AnnouncementsEntryImpl;
58  import com.liferay.portlet.announcements.service.AnnouncementsDeliveryServiceUtil;
59  
60  import javax.portlet.ActionRequest;
61  import javax.portlet.ActionResponse;
62  import javax.portlet.PortletConfig;
63  import javax.portlet.PortletSession;
64  import javax.portlet.RenderRequest;
65  import javax.portlet.RenderResponse;
66  
67  import javax.servlet.http.HttpServletRequest;
68  import javax.servlet.http.HttpSession;
69  
70  import org.apache.struts.Globals;
71  import org.apache.struts.action.ActionForm;
72  import org.apache.struts.action.ActionForward;
73  import org.apache.struts.action.ActionMapping;
74  
75  /**
76   * <a href="EditUserAction.java.html"><b><i>View Source</i></b></a>
77   *
78   * @author Brian Wing Shun Chan
79   */
80  public class EditUserAction extends PortletAction {
81  
82      public void processAction(
83              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
84              ActionRequest actionRequest, ActionResponse actionResponse)
85          throws Exception {
86  
87          String cmd = ParamUtil.getString(actionRequest, 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(actionRequest);
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(actionRequest);
104             }
105             else if (cmd.equals("deleteRole")) {
106                 deleteRole(actionRequest);
107             }
108             else if (cmd.equals("unlock")) {
109                 user = updateLockout(actionRequest);
110             }
111 
112             String redirect = ParamUtil.getString(actionRequest, "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)actionRequest.getAttribute(
124                             WebKeys.THEME_DISPLAY);
125 
126                     Group group = user.getGroup();
127 
128                     if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
129                         Layout layout = themeDisplay.getLayout();
130 
131                         String friendlyURLPath = group.getPathFriendlyURL(
132                             layout.isPrivateLayout(), themeDisplay);
133 
134                         String oldPath =
135                             friendlyURLPath + StringPool.SLASH + oldScreenName;
136                         String newPath =
137                             friendlyURLPath + StringPool.SLASH +
138                                 user.getScreenName();
139 
140                         redirect = StringUtil.replace(
141                             redirect, oldPath, newPath);
142 
143                         redirect = StringUtil.replace(
144                             redirect, HttpUtil.encodeURL(oldPath),
145                             HttpUtil.encodeURL(newPath));
146                     }
147                 }
148 
149                 redirect += user.getUserId();
150             }
151 
152             sendRedirect(actionRequest, actionResponse, redirect);
153         }
154         catch (Exception e) {
155             if (e instanceof NoSuchUserException ||
156                 e instanceof PrincipalException) {
157 
158                 SessionErrors.add(actionRequest, e.getClass().getName());
159 
160                 setForward(actionRequest, "portlet.enterprise_admin.error");
161             }
162             else if (e instanceof ContactFirstNameException ||
163                      e instanceof ContactLastNameException ||
164                      e instanceof DuplicateUserEmailAddressException ||
165                      e instanceof DuplicateUserScreenNameException ||
166                      e instanceof RequiredUserException ||
167                      e instanceof ReservedUserEmailAddressException ||
168                      e instanceof ReservedUserScreenNameException ||
169                      e instanceof UserEmailAddressException ||
170                      e instanceof UserIdException ||
171                      e instanceof UserPasswordException ||
172                      e instanceof UserScreenNameException ||
173                      e instanceof UserSmsException) {
174 
175                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
176 
177                 if (e instanceof RequiredUserException) {
178                     actionResponse.sendRedirect(
179                         ParamUtil.getString(actionRequest, "redirect"));
180                 }
181             }
182             else {
183                 throw e;
184             }
185         }
186     }
187 
188     public ActionForward render(
189             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
190             RenderRequest renderRequest, RenderResponse renderResponse)
191         throws Exception {
192 
193         try {
194             PortalUtil.getSelectedUser(renderRequest);
195         }
196         catch (Exception e) {
197             if (e instanceof PrincipalException) {
198                 SessionErrors.add(renderRequest, 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(renderRequest, "portlet.enterprise_admin.edit_user"));
209     }
210 
211     protected void deleteRole(ActionRequest actionRequest) throws Exception {
212         User user = PortalUtil.getSelectedUser(actionRequest);
213 
214         long roleId = ParamUtil.getLong(actionRequest, "roleId");
215 
216         UserServiceUtil.deleteRoleUser(roleId, user.getUserId());
217     }
218 
219     protected void deleteUsers(ActionRequest actionRequest) throws Exception {
220         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
221 
222         long[] deleteUserIds = StringUtil.split(
223             ParamUtil.getString(actionRequest, "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 actionRequest) throws Exception {
240         User user = PortalUtil.getSelectedUser(actionRequest);
241 
242         UserServiceUtil.updateLockout(user.getUserId(), false);
243 
244         return user;
245     }
246 
247     protected Object[] updateUser(ActionRequest actionRequest)
248         throws Exception {
249 
250         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
251 
252         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
253             WebKeys.THEME_DISPLAY);
254 
255         boolean autoPassword = ParamUtil.getBoolean(
256             actionRequest, "autoPassword", true);
257         String password1 = ParamUtil.getString(actionRequest, "password1");
258         String password2 = ParamUtil.getString(actionRequest, "password2");
259         boolean autoScreenName = ParamUtil.getBoolean(
260             actionRequest, "autoScreenName");
261         String screenName = ParamUtil.getString(actionRequest, "screenName");
262         String emailAddress = ParamUtil.getString(
263             actionRequest, "emailAddress");
264         String languageId = ParamUtil.getString(actionRequest, "languageId");
265         String timeZoneId = ParamUtil.getString(actionRequest, "timeZoneId");
266         String greeting = ParamUtil.getString(actionRequest, "greeting");
267         String firstName = ParamUtil.getString(actionRequest, "firstName");
268         String middleName = ParamUtil.getString(actionRequest, "middleName");
269         String lastName = ParamUtil.getString(actionRequest, "lastName");
270         int prefixId = ParamUtil.getInteger(actionRequest, "prefixId");
271         int suffixId = ParamUtil.getInteger(actionRequest, "suffixId");
272         boolean male = ParamUtil.getBoolean(actionRequest, "male", true);
273         int birthdayMonth = ParamUtil.getInteger(
274             actionRequest, "birthdayMonth");
275         int birthdayDay = ParamUtil.getInteger(actionRequest, "birthdayDay");
276         int birthdayYear = ParamUtil.getInteger(actionRequest, "birthdayYear");
277         String comments = ParamUtil.getString(actionRequest, "comments");
278         String smsSn = ParamUtil.getString(actionRequest, "smsSn");
279         String aimSn = ParamUtil.getString(actionRequest, "aimSn");
280         String facebookSn = ParamUtil.getString(actionRequest, "facebookSn");
281         String icqSn = ParamUtil.getString(actionRequest, "icqSn");
282         String jabberSn = ParamUtil.getString(actionRequest, "jabberSn");
283         String msnSn = ParamUtil.getString(actionRequest, "msnSn");
284         String mySpaceSn = ParamUtil.getString(actionRequest, "mySpaceSn");
285         String skypeSn = ParamUtil.getString(actionRequest, "skypeSn");
286         String twitterSn = ParamUtil.getString(actionRequest, "twitterSn");
287         String ymSn = ParamUtil.getString(actionRequest, "ymSn");
288         String jobTitle = ParamUtil.getString(actionRequest, "jobTitle");
289         long[] organizationIds = StringUtil.split(
290             ParamUtil.getString(actionRequest, "organizationIds"),  0L);
291         boolean sendEmail = true;
292 
293         User user = null;
294         String oldScreenName = StringPool.BLANK;
295 
296         if (cmd.equals(Constants.ADD)) {
297 
298             // Add user
299 
300             user = UserServiceUtil.addUser(
301                 themeDisplay.getCompanyId(), autoPassword, password1, password2,
302                 autoScreenName, screenName, emailAddress,
303                 LocaleUtil.getDefault(), firstName, middleName, lastName,
304                 prefixId, suffixId, male, birthdayMonth, birthdayDay,
305                 birthdayYear, jobTitle, organizationIds, sendEmail);
306         }
307         else {
308 
309             // Update user
310 
311             user = PortalUtil.getSelectedUser(actionRequest);
312 
313             String oldPassword = AdminUtil.getUpdateUserPassword(
314                 actionRequest, user.getUserId());
315             String newPassword1 = ParamUtil.getString(
316                 actionRequest, "password1");
317             String newPassword2 = ParamUtil.getString(
318                 actionRequest, "password2");
319             boolean passwordReset = ParamUtil.getBoolean(
320                 actionRequest, "passwordReset");
321 
322             String tempOldScreenName = user.getScreenName();
323 
324             user = UserServiceUtil.updateUser(
325                 user.getUserId(), oldPassword, newPassword1, newPassword2,
326                 passwordReset, screenName, emailAddress, languageId, timeZoneId,
327                 greeting, comments, firstName, middleName, lastName, prefixId,
328                 suffixId, male, birthdayMonth, birthdayDay, birthdayYear, smsSn,
329                 aimSn, facebookSn, icqSn, jabberSn, msnSn, mySpaceSn, skypeSn,
330                 twitterSn, ymSn, jobTitle, organizationIds);
331 
332             String openId = ParamUtil.getString(actionRequest, "openId");
333 
334             if (!openId.equals(user.getOpenId())) {
335                 UserServiceUtil.updateOpenId(user.getUserId(), openId);
336             }
337 
338             for (String type : AnnouncementsEntryImpl.TYPES) {
339                 boolean email = ParamUtil.getBoolean(
340                     actionRequest, "announcementsType" + type + "Email");
341                 boolean sms = ParamUtil.getBoolean(
342                     actionRequest, "announcementsType" + type + "Sms");
343                 boolean website = ParamUtil.getBoolean(
344                     actionRequest, "announcementsType" + type + "Website");
345 
346                 AnnouncementsDeliveryServiceUtil.updateDelivery(
347                     user.getUserId(), type, email, sms, website);
348             }
349 
350             if (!tempOldScreenName.equals(user.getScreenName())) {
351                 oldScreenName = tempOldScreenName;
352             }
353 
354             if (user.getUserId() == themeDisplay.getUserId()) {
355 
356                 // Reset the locale
357 
358                 HttpServletRequest request = PortalUtil.getHttpServletRequest(
359                     actionRequest);
360                 HttpSession session = request.getSession();
361 
362                 session.removeAttribute(Globals.LOCALE_KEY);
363 
364                 // Clear cached portlet responses
365 
366                 PortletSession portletSession =
367                     actionRequest.getPortletSession();
368 
369                 InvokerPortletImpl.clearResponses(portletSession);
370 
371                 // Password
372 
373                 if (Validator.isNotNull(newPassword1)) {
374                     portletSession.setAttribute(
375                         WebKeys.USER_PASSWORD, newPassword1,
376                         PortletSession.APPLICATION_SCOPE);
377                 }
378             }
379         }
380 
381         return new Object[] {user, oldScreenName};
382     }
383 
384 }