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.

DeleteAction.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.usergroups.ws;
  21. import org.sonar.api.server.ws.Change;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.api.server.ws.WebService.NewController;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.permission.OrganizationPermission;
  29. import org.sonar.db.user.GroupDto;
  30. import org.sonar.server.user.UserSession;
  31. import static com.google.common.base.Preconditions.checkArgument;
  32. import static java.lang.String.format;
  33. import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_ID;
  34. import static org.sonar.server.usergroups.ws.GroupWsSupport.PARAM_GROUP_NAME;
  35. import static org.sonar.server.usergroups.ws.GroupWsSupport.defineGroupWsParameters;
  36. public class DeleteAction implements UserGroupsWsAction {
  37. private final DbClient dbClient;
  38. private final UserSession userSession;
  39. private final GroupWsSupport support;
  40. public DeleteAction(DbClient dbClient, UserSession userSession, GroupWsSupport support) {
  41. this.dbClient = dbClient;
  42. this.userSession = userSession;
  43. this.support = support;
  44. }
  45. @Override
  46. public void define(NewController context) {
  47. WebService.NewAction action = context.createAction("delete")
  48. .setDescription(format("Delete a group. The default groups cannot be deleted.<br/>" +
  49. "'%s' or '%s' must be provided.<br />" +
  50. "Requires the following permission: 'Administer System'.",
  51. PARAM_GROUP_ID, PARAM_GROUP_NAME))
  52. .setHandler(this)
  53. .setSince("5.2")
  54. .setPost(true)
  55. .setChangelog(
  56. new Change("8.4", "Parameter 'id' is deprecated. Format changes from integer to string. Use 'name' instead."));
  57. defineGroupWsParameters(action);
  58. }
  59. @Override
  60. public void handle(Request request, Response response) throws Exception {
  61. try (DbSession dbSession = dbClient.openSession(false)) {
  62. GroupDto group = support.findGroupDto(dbSession, request);
  63. userSession.checkPermission(OrganizationPermission.ADMINISTER);
  64. support.checkGroupIsNotDefault(dbSession, group);
  65. checkNotTryingToDeleteLastAdminGroup(dbSession, group);
  66. removeGroupPermissions(dbSession, group);
  67. removeFromPermissionTemplates(dbSession, group);
  68. removeGroupMembers(dbSession, group);
  69. dbClient.qProfileEditGroupsDao().deleteByGroup(dbSession, group);
  70. dbClient.groupDao().deleteByUuid(dbSession, group.getUuid());
  71. dbSession.commit();
  72. response.noContent();
  73. }
  74. }
  75. private void checkNotTryingToDeleteLastAdminGroup(DbSession dbSession, GroupDto group) {
  76. int remaining = dbClient.authorizationDao().countUsersWithGlobalPermissionExcludingGroup(dbSession,
  77. OrganizationPermission.ADMINISTER.getKey(), group.getUuid());
  78. checkArgument(remaining > 0, "The last system admin group cannot be deleted");
  79. }
  80. private void removeGroupPermissions(DbSession dbSession, GroupDto group) {
  81. dbClient.roleDao().deleteGroupRolesByGroupUuid(dbSession, group.getUuid());
  82. }
  83. private void removeFromPermissionTemplates(DbSession dbSession, GroupDto group) {
  84. dbClient.permissionTemplateDao().deleteByGroup(dbSession, group.getUuid());
  85. }
  86. private void removeGroupMembers(DbSession dbSession, GroupDto group) {
  87. dbClient.userGroupDao().deleteByGroupUuid(dbSession, group.getUuid());
  88. }
  89. }