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.

AddGroupAction.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 java.util.List;
  22. import java.util.Optional;
  23. import org.sonar.api.server.ws.Change;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.api.server.ws.Response;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonar.server.permission.GroupPermissionChange;
  31. import org.sonar.server.permission.GroupUuidOrAnyone;
  32. import org.sonar.server.permission.PermissionChange;
  33. import org.sonar.server.permission.PermissionService;
  34. import org.sonar.server.permission.PermissionUpdater;
  35. import org.sonar.server.user.UserSession;
  36. import static org.sonar.server.permission.ws.WsParameters.createGroupIdParameter;
  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 AddGroupAction implements PermissionsWsAction {
  41. public static final String ACTION = "add_group";
  42. private final DbClient dbClient;
  43. private final UserSession userSession;
  44. private final PermissionUpdater permissionUpdater;
  45. private final PermissionWsSupport wsSupport;
  46. private final WsParameters wsParameters;
  47. private final PermissionService permissionService;
  48. public AddGroupAction(DbClient dbClient, UserSession userSession, PermissionUpdater permissionUpdater, PermissionWsSupport wsSupport,
  49. WsParameters wsParameters, PermissionService permissionService) {
  50. this.dbClient = dbClient;
  51. this.userSession = userSession;
  52. this.permissionUpdater = permissionUpdater;
  53. this.wsSupport = wsSupport;
  54. this.wsParameters = wsParameters;
  55. this.permissionService = permissionService;
  56. }
  57. @Override
  58. public void define(WebService.NewController context) {
  59. WebService.NewAction action = context.createAction(ACTION)
  60. .setDescription("Add a permission to a group.<br /> " +
  61. "This service defaults to global permissions, but can be limited to project permissions by providing project id or project key.<br /> " +
  62. "The group name or group id must be provided. <br />" +
  63. "Requires one of the following permissions:" +
  64. "<ul>" +
  65. "<li>'Administer System'</li>" +
  66. "<li>'Administer' rights on the specified project</li>" +
  67. "</ul>")
  68. .setSince("5.2")
  69. .setChangelog(
  70. new Change("8.4", "Parameter 'groupId' is deprecated. Format changes from integer to string. Use 'name' instead."))
  71. .setPost(true)
  72. .setHandler(this);
  73. wsParameters.createPermissionParameter(action, "The permission you would like to grant to the group.");
  74. createGroupNameParameter(action);
  75. createGroupIdParameter(action);
  76. createProjectParameters(action);
  77. }
  78. @Override
  79. public void handle(Request request, Response response) throws Exception {
  80. try (DbSession dbSession = dbClient.openSession(false)) {
  81. GroupUuidOrAnyone group = wsSupport.findGroup(dbSession, request);
  82. Optional<ComponentDto> project = wsSupport.findProject(dbSession, request);
  83. wsSupport.checkPermissionManagementAccess(userSession, project.orElse(null));
  84. PermissionChange change = new GroupPermissionChange(
  85. PermissionChange.Operation.ADD,
  86. request.mandatoryParam(PARAM_PERMISSION),
  87. project.orElse(null),
  88. group, permissionService);
  89. permissionUpdater.apply(dbSession, List.of(change));
  90. }
  91. response.noContent();
  92. }
  93. }