You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoveMemberAction.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.organization.ws;
  21. import org.sonar.api.server.ws.Request;
  22. import org.sonar.api.server.ws.Response;
  23. import org.sonar.api.server.ws.WebService;
  24. import org.sonar.api.server.ws.WebService.NewController;
  25. import org.sonar.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.organization.OrganizationDto;
  28. import org.sonar.db.user.UserDto;
  29. import org.sonar.server.user.UserSession;
  30. import org.sonar.server.user.index.UserIndexer;
  31. import static java.util.Collections.singletonList;
  32. import static org.sonar.api.CoreProperties.DEFAULT_ISSUE_ASSIGNEE;
  33. import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
  34. import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_LOGIN;
  35. import static org.sonar.server.organization.ws.OrganizationsWsSupport.PARAM_ORGANIZATION;
  36. import static org.sonar.server.ws.KeyExamples.KEY_ORG_EXAMPLE_001;
  37. import static org.sonar.server.ws.WsUtils.checkFound;
  38. import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
  39. import static org.sonar.server.ws.WsUtils.checkRequest;
  40. public class RemoveMemberAction implements OrganizationsWsAction {
  41. private final DbClient dbClient;
  42. private final UserSession userSession;
  43. private final UserIndexer userIndexer;
  44. public RemoveMemberAction(DbClient dbClient, UserSession userSession, UserIndexer userIndexer) {
  45. this.dbClient = dbClient;
  46. this.userSession = userSession;
  47. this.userIndexer = userIndexer;
  48. }
  49. @Override
  50. public void define(NewController context) {
  51. WebService.NewAction action = context.createAction("remove_member")
  52. .setDescription("Remove a member from an organization.<br>" +
  53. "Requires 'Administer System' permission on the specified organization.")
  54. .setSince("6.4")
  55. .setPost(true)
  56. .setInternal(true)
  57. .setHandler(this);
  58. action
  59. .createParam(PARAM_ORGANIZATION)
  60. .setDescription("Organization key")
  61. .setRequired(true)
  62. .setExampleValue(KEY_ORG_EXAMPLE_001);
  63. action
  64. .createParam(PARAM_LOGIN)
  65. .setDescription("User login")
  66. .setRequired(true)
  67. .setExampleValue("ray.bradbury");
  68. }
  69. @Override
  70. public void handle(Request request, Response response) throws Exception {
  71. String organizationKey = request.mandatoryParam(PARAM_ORGANIZATION);
  72. String login = request.mandatoryParam(PARAM_LOGIN);
  73. try (DbSession dbSession = dbClient.openSession(false)) {
  74. OrganizationDto organization = checkFoundWithOptional(dbClient.organizationDao().selectByKey(dbSession, organizationKey),
  75. "Organization '%s' is not found", organizationKey);
  76. UserDto user = checkFound(dbClient.userDao().selectActiveUserByLogin(dbSession, login), "User '%s' is not found", login);
  77. userSession.checkPermission(ADMINISTER, organization);
  78. dbClient.organizationMemberDao().select(dbSession, organization.getUuid(), user.getId())
  79. .ifPresent(om -> removeMember(dbSession, organization, user));
  80. }
  81. response.noContent();
  82. }
  83. private void removeMember(DbSession dbSession, OrganizationDto organization, UserDto user) {
  84. ensureLastAdminIsNotRemoved(dbSession, organization, user);
  85. int userId = user.getId();
  86. String organizationUuid = organization.getUuid();
  87. dbClient.userPermissionDao().deleteOrganizationMemberPermissions(dbSession, organizationUuid, userId);
  88. dbClient.permissionTemplateDao().deleteUserPermissionsByOrganization(dbSession, organizationUuid, userId);
  89. dbClient.qProfileEditUsersDao().deleteByOrganizationAndUser(dbSession, organization, user);
  90. dbClient.userGroupDao().deleteByOrganizationAndUser(dbSession, organizationUuid, userId);
  91. dbClient.propertiesDao().deleteByOrganizationAndUser(dbSession, organizationUuid, userId);
  92. dbClient.propertiesDao().deleteByOrganizationAndMatchingLogin(dbSession, organizationUuid, user.getLogin(), singletonList(DEFAULT_ISSUE_ASSIGNEE));
  93. dbClient.organizationMemberDao().delete(dbSession, organizationUuid, userId);
  94. userIndexer.commitAndIndex(dbSession, user);
  95. }
  96. private void ensureLastAdminIsNotRemoved(DbSession dbSession, OrganizationDto organizationDto, UserDto user) {
  97. int remainingAdmins = dbClient.authorizationDao().countUsersWithGlobalPermissionExcludingUser(dbSession,
  98. organizationDto.getUuid(), ADMINISTER.getKey(), user.getId());
  99. checkRequest(remainingAdmins > 0, "The last administrator member cannot be removed");
  100. }
  101. }