PageRenderTime 107ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/apps/users-admin/users-admin-web/src/main/java/com/liferay/users/admin/web/internal/portlet/action/UpdateUserRolesMVCActionCommand.java

http://github.com/liferay/liferay-portal
Java | 275 lines | 196 code | 55 blank | 24 comment | 19 complexity | 5782206e8bfe74f0bfddedd8263d8b5c MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.users.admin.web.internal.portlet.action;
  15. import com.liferay.petra.string.StringPool;
  16. import com.liferay.portal.kernel.exception.ContactNameException;
  17. import com.liferay.portal.kernel.exception.NoSuchUserException;
  18. import com.liferay.portal.kernel.exception.RequiredRoleException;
  19. import com.liferay.portal.kernel.exception.UserEmailAddressException;
  20. import com.liferay.portal.kernel.exception.UserScreenNameException;
  21. import com.liferay.portal.kernel.model.Contact;
  22. import com.liferay.portal.kernel.model.Role;
  23. import com.liferay.portal.kernel.model.User;
  24. import com.liferay.portal.kernel.model.UserGroupRole;
  25. import com.liferay.portal.kernel.model.role.RoleConstants;
  26. import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
  27. import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
  28. import com.liferay.portal.kernel.security.auth.PrincipalException;
  29. import com.liferay.portal.kernel.security.membershippolicy.MembershipPolicyException;
  30. import com.liferay.portal.kernel.security.permission.ActionKeys;
  31. import com.liferay.portal.kernel.security.permission.PermissionChecker;
  32. import com.liferay.portal.kernel.security.permission.PermissionCheckerFactoryUtil;
  33. import com.liferay.portal.kernel.service.RoleService;
  34. import com.liferay.portal.kernel.service.ServiceContext;
  35. import com.liferay.portal.kernel.service.ServiceContextFactory;
  36. import com.liferay.portal.kernel.service.UserService;
  37. import com.liferay.portal.kernel.service.permission.OrganizationPermissionUtil;
  38. import com.liferay.portal.kernel.service.permission.PortletPermissionUtil;
  39. import com.liferay.portal.kernel.servlet.SessionErrors;
  40. import com.liferay.portal.kernel.util.ArrayUtil;
  41. import com.liferay.portal.kernel.util.CalendarFactoryUtil;
  42. import com.liferay.portal.kernel.util.GetterUtil;
  43. import com.liferay.portal.kernel.util.Http;
  44. import com.liferay.portal.kernel.util.JavaConstants;
  45. import com.liferay.portal.kernel.util.ParamUtil;
  46. import com.liferay.portal.kernel.util.Portal;
  47. import com.liferay.portal.kernel.util.Validator;
  48. import com.liferay.users.admin.constants.UsersAdminPortletKeys;
  49. import com.liferay.users.admin.kernel.util.UsersAdmin;
  50. import java.util.Calendar;
  51. import java.util.List;
  52. import java.util.Map;
  53. import javax.portlet.ActionRequest;
  54. import javax.portlet.ActionResponse;
  55. import javax.portlet.PortletConfig;
  56. import javax.portlet.PortletRequest;
  57. import javax.portlet.PortletURL;
  58. import javax.servlet.http.HttpServletRequest;
  59. import org.osgi.service.component.annotations.Component;
  60. import org.osgi.service.component.annotations.Reference;
  61. /**
  62. * @author Pei-Jung Lan
  63. */
  64. @Component(
  65. immediate = true,
  66. property = {
  67. "javax.portlet.name=" + UsersAdminPortletKeys.MY_ACCOUNT,
  68. "javax.portlet.name=" + UsersAdminPortletKeys.MY_ORGANIZATIONS,
  69. "javax.portlet.name=" + UsersAdminPortletKeys.USERS_ADMIN,
  70. "mvc.command.name=/users_admin/update_user_roles"
  71. },
  72. service = MVCActionCommand.class
  73. )
  74. public class UpdateUserRolesMVCActionCommand extends BaseMVCActionCommand {
  75. @Override
  76. protected void doProcessAction(
  77. ActionRequest actionRequest, ActionResponse actionResponse)
  78. throws Exception {
  79. try {
  80. User user = _portal.getSelectedUser(actionRequest);
  81. Contact contact = user.getContact();
  82. Calendar birthdayCal = CalendarFactoryUtil.getCalendar();
  83. birthdayCal.setTime(user.getBirthday());
  84. long[] roleIds = _usersAdmin.getRoleIds(actionRequest);
  85. _validate(user, roleIds);
  86. List<UserGroupRole> userGroupRoles = null;
  87. String addGroupRolesGroupIds = actionRequest.getParameter(
  88. "addGroupRolesGroupIds");
  89. String addGroupRolesRoleIds = actionRequest.getParameter(
  90. "addGroupRolesRoleIds");
  91. String deleteGroupRolesGroupIds = actionRequest.getParameter(
  92. "deleteGroupRolesGroupIds");
  93. String deleteGroupRolesRoleIds = actionRequest.getParameter(
  94. "deleteGroupRolesRoleIds");
  95. if ((addGroupRolesGroupIds != null) ||
  96. (addGroupRolesRoleIds != null) ||
  97. (deleteGroupRolesGroupIds != null) ||
  98. (deleteGroupRolesRoleIds != null)) {
  99. userGroupRoles = _usersAdmin.getUserGroupRoles(actionRequest);
  100. }
  101. ServiceContext serviceContext = ServiceContextFactory.getInstance(
  102. User.class.getName(), actionRequest);
  103. serviceContext.setAssetCategoryIds(null);
  104. serviceContext.setAssetTagNames(null);
  105. user = _userService.updateUser(
  106. user.getUserId(), user.getPassword(), null, null,
  107. user.isPasswordReset(), null, null, user.getScreenName(),
  108. user.getEmailAddress(), user.getLanguageId(),
  109. user.getTimeZoneId(), user.getGreeting(), user.getComments(),
  110. user.getFirstName(), user.getMiddleName(), user.getLastName(),
  111. contact.getPrefixId(), contact.getSuffixId(), user.isMale(),
  112. birthdayCal.get(Calendar.MONTH), birthdayCal.get(Calendar.DATE),
  113. birthdayCal.get(Calendar.YEAR), contact.getSmsSn(),
  114. contact.getFacebookSn(), contact.getJabberSn(),
  115. contact.getSkypeSn(), contact.getTwitterSn(),
  116. user.getJobTitle(), null, user.getOrganizationIds(), roleIds,
  117. userGroupRoles, user.getUserGroupIds(), serviceContext);
  118. User currentUser = _userService.getCurrentUser();
  119. if (currentUser.getUserId() == user.getUserId()) {
  120. String redirect = _getRedirect(actionRequest, currentUser);
  121. if (Validator.isNotNull(redirect)) {
  122. sendRedirect(actionRequest, actionResponse, redirect);
  123. }
  124. }
  125. }
  126. catch (Exception exception) {
  127. if (exception instanceof ContactNameException ||
  128. exception instanceof NoSuchUserException ||
  129. exception instanceof PrincipalException ||
  130. exception instanceof
  131. RequiredRoleException.MustNotRemoveLastAdministator ||
  132. exception instanceof UserEmailAddressException ||
  133. exception instanceof UserScreenNameException) {
  134. SessionErrors.add(actionRequest, exception.getClass());
  135. actionResponse.setRenderParameter("mvcPath", "/error.jsp");
  136. }
  137. else if (exception instanceof MembershipPolicyException) {
  138. SessionErrors.add(
  139. actionRequest, exception.getClass(), exception);
  140. actionResponse.setRenderParameter("mvcPath", "/edit_user.jsp");
  141. }
  142. else {
  143. throw exception;
  144. }
  145. }
  146. }
  147. private String _getRedirect(ActionRequest actionRequest, User currentUser)
  148. throws Exception {
  149. PortletConfig portletConfig = (PortletConfig)actionRequest.getAttribute(
  150. JavaConstants.JAVAX_PORTLET_CONFIG);
  151. String portletName = portletConfig.getPortletName();
  152. PermissionChecker permissionChecker =
  153. PermissionCheckerFactoryUtil.create(currentUser);
  154. if (!PortletPermissionUtil.contains(
  155. permissionChecker, portletName,
  156. ActionKeys.ACCESS_IN_CONTROL_PANEL)) {
  157. return _portal.getHomeURL(
  158. _portal.getHttpServletRequest(actionRequest));
  159. }
  160. if (portletName.equals(UsersAdminPortletKeys.MY_ORGANIZATIONS)) {
  161. HttpServletRequest httpServletRequest =
  162. _portal.getHttpServletRequest(actionRequest);
  163. String backURL = null;
  164. long organizationId = 0;
  165. String portletNamespace = _portal.getPortletNamespace(
  166. UsersAdminPortletKeys.MY_ORGANIZATIONS);
  167. String redirect = ParamUtil.getString(
  168. httpServletRequest, "redirect");
  169. if (Validator.isNotNull(redirect)) {
  170. Map<String, String[]> parameterMap = _http.getParameterMap(
  171. redirect);
  172. backURL = parameterMap.get(portletNamespace + "backURL")[0];
  173. }
  174. if (Validator.isNotNull(backURL)) {
  175. Map<String, String[]> parameterMap = _http.getParameterMap(
  176. backURL);
  177. organizationId = GetterUtil.getLong(
  178. parameterMap.get(portletNamespace + "organizationId")[0]);
  179. }
  180. if ((organizationId > 0) &&
  181. !OrganizationPermissionUtil.contains(
  182. permissionChecker, organizationId, ActionKeys.VIEW)) {
  183. PortletURL portletURL = _portal.getControlPanelPortletURL(
  184. httpServletRequest, portletName,
  185. PortletRequest.RENDER_PHASE);
  186. return portletURL.toString();
  187. }
  188. }
  189. return StringPool.BLANK;
  190. }
  191. private void _validate(User user, long[] roleIds) throws Exception {
  192. // This is a unique case where we should throw an exception in the
  193. // portlet action. The service implementation already guards against
  194. // removing the last administrator, but it does so by quietly readding
  195. // the administrator role to the roleIds array. We're already safe in
  196. // regards to data integrity. However, the goal is to provide the user
  197. // feedback as to why the administrator role was not removed. Putting
  198. // this check in UserServiceImpl is useless because UsersAdmin readds
  199. // the role.
  200. Role administratorRole = _roleService.getRole(
  201. user.getCompanyId(), RoleConstants.ADMINISTRATOR);
  202. long[] administratorUserIds = _userService.getRoleUserIds(
  203. administratorRole.getRoleId());
  204. if ((administratorUserIds.length == 1) &&
  205. ArrayUtil.contains(administratorUserIds, user.getUserId()) &&
  206. !ArrayUtil.contains(roleIds, administratorRole.getRoleId())) {
  207. throw new RequiredRoleException.MustNotRemoveLastAdministator();
  208. }
  209. }
  210. @Reference
  211. private Http _http;
  212. @Reference
  213. private Portal _portal;
  214. @Reference
  215. private RoleService _roleService;
  216. @Reference
  217. private UsersAdmin _usersAdmin;
  218. @Reference
  219. private UserService _userService;
  220. }