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.

RemoveGroupAction.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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.permission.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.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.entity.EntityDto;
  28. import org.sonar.db.user.GroupDto;
  29. import org.sonar.server.common.management.ManagedInstanceChecker;
  30. import org.sonar.server.common.permission.GroupPermissionChange;
  31. import org.sonar.server.permission.GroupUuidOrAnyone;
  32. import org.sonar.server.common.permission.Operation;
  33. import org.sonar.server.permission.PermissionService;
  34. import org.sonar.server.common.permission.PermissionUpdater;
  35. import org.sonar.server.user.UserSession;
  36. import static java.util.Collections.singletonList;
  37. import static org.sonar.server.permission.ws.WsParameters.createGroupNameParameter;
  38. import static org.sonar.server.permission.ws.WsParameters.createProjectParameters;
  39. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
  40. public class RemoveGroupAction implements PermissionsWsAction {
  41. public static final String ACTION = "remove_group";
  42. private final DbClient dbClient;
  43. private final UserSession userSession;
  44. private final PermissionUpdater<GroupPermissionChange> permissionUpdater;
  45. private final PermissionWsSupport wsSupport;
  46. private final WsParameters wsParameters;
  47. private final PermissionService permissionService;
  48. private final ManagedInstanceChecker managedInstanceChecker;
  49. public RemoveGroupAction(DbClient dbClient, UserSession userSession, PermissionUpdater<GroupPermissionChange> permissionUpdater, PermissionWsSupport wsSupport,
  50. WsParameters wsParameters, PermissionService permissionService, ManagedInstanceChecker managedInstanceChecker) {
  51. this.dbClient = dbClient;
  52. this.userSession = userSession;
  53. this.permissionUpdater = permissionUpdater;
  54. this.wsSupport = wsSupport;
  55. this.wsParameters = wsParameters;
  56. this.permissionService = permissionService;
  57. this.managedInstanceChecker = managedInstanceChecker;
  58. }
  59. @Override
  60. public void define(WebService.NewController context) {
  61. WebService.NewAction action = context.createAction(ACTION)
  62. .setDescription("Remove a permission from a group.<br /> " +
  63. "This service defaults to global permissions, but can be limited to project permissions by providing project id or project key.<br /> " +
  64. "The group name must be provided.<br />" +
  65. "Requires one of the following permissions:" +
  66. "<ul>" +
  67. "<li>'Administer System'</li>" +
  68. "<li>'Administer' rights on the specified project</li>" +
  69. "</ul>")
  70. .setSince("5.2")
  71. .setPost(true)
  72. .setChangelog(
  73. new Change("10.0", "Parameter 'groupId' is removed. Use 'groupName' instead."),
  74. new Change("8.4", "Parameter 'groupId' is deprecated. Format changes from integer to string. Use 'groupName' instead."))
  75. .setHandler(this);
  76. wsParameters.createPermissionParameter(action, "The permission you would like to revoke from the group.");
  77. createGroupNameParameter(action);
  78. createProjectParameters(action);
  79. }
  80. @Override
  81. public void handle(Request request, Response response) throws Exception {
  82. try (DbSession dbSession = dbClient.openSession(false)) {
  83. EntityDto entityDto = wsSupport.findEntity(dbSession, request);
  84. GroupDto groupDto = wsSupport.findGroupDtoOrNullIfAnyone(dbSession, request);
  85. if (entityDto != null && entityDto.isProject() && groupDto != null) {
  86. managedInstanceChecker.throwIfGroupAndProjectAreManaged(dbSession, groupDto.getUuid(), entityDto.getUuid());
  87. }
  88. wsSupport.checkPermissionManagementAccess(userSession, entityDto);
  89. String permission = request.mandatoryParam(PARAM_PERMISSION);
  90. wsSupport.checkRemovingOwnBrowsePermissionOnPrivateProject(dbSession, userSession, entityDto, permission, GroupUuidOrAnyone.from(groupDto));
  91. GroupPermissionChange change = new GroupPermissionChange(
  92. Operation.REMOVE,
  93. permission,
  94. entityDto,
  95. groupDto,
  96. permissionService);
  97. permissionUpdater.apply(dbSession, singletonList(change));
  98. }
  99. response.noContent();
  100. }
  101. }