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.

AddUserAction.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.Optional;
  22. import org.sonar.api.config.Configuration;
  23. import org.sonar.api.server.ws.Request;
  24. import org.sonar.api.server.ws.Response;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.component.ComponentDto;
  29. import org.sonar.db.user.UserId;
  30. import org.sonar.server.permission.PermissionChange;
  31. import org.sonar.server.permission.PermissionService;
  32. import org.sonar.server.permission.PermissionUpdater;
  33. import org.sonar.server.permission.UserPermissionChange;
  34. import org.sonar.server.user.UserSession;
  35. import static java.util.Collections.singletonList;
  36. import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
  37. import static org.sonar.server.permission.ws.WsParameters.createProjectParameters;
  38. import static org.sonar.server.permission.ws.WsParameters.createUserLoginParameter;
  39. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
  40. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
  41. public class AddUserAction implements PermissionsWsAction {
  42. public static final String ACTION = "add_user";
  43. private final DbClient dbClient;
  44. private final UserSession userSession;
  45. private final PermissionUpdater permissionUpdater;
  46. private final PermissionWsSupport wsSupport;
  47. private final WsParameters wsParameters;
  48. private final PermissionService permissionService;
  49. private final Configuration configuration;
  50. public AddUserAction(DbClient dbClient, UserSession userSession, PermissionUpdater permissionUpdater, PermissionWsSupport wsSupport,
  51. WsParameters wsParameters, PermissionService permissionService, Configuration configuration) {
  52. this.dbClient = dbClient;
  53. this.userSession = userSession;
  54. this.permissionUpdater = permissionUpdater;
  55. this.wsSupport = wsSupport;
  56. this.wsParameters = wsParameters;
  57. this.permissionService = permissionService;
  58. this.configuration = configuration;
  59. }
  60. @Override
  61. public void define(WebService.NewController context) {
  62. WebService.NewAction action = context.createAction(ACTION)
  63. .setDescription("Add permission to a user.<br /> " +
  64. "This service defaults to global permissions, but can be limited to project permissions by providing project id or project key.<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. .setHandler(this);
  73. wsParameters.createPermissionParameter(action, "The permission you would like to grant to the user");
  74. createUserLoginParameter(action);
  75. createProjectParameters(action);
  76. }
  77. @Override
  78. public void handle(Request request, Response response) throws Exception {
  79. try (DbSession dbSession = dbClient.openSession(false)) {
  80. UserId user = wsSupport.findUser(dbSession, request.mandatoryParam(PARAM_USER_LOGIN));
  81. Optional<ComponentDto> project = wsSupport.findProject(dbSession, request);
  82. checkProjectAdmin(userSession, configuration, project.orElse(null));
  83. PermissionChange change = new UserPermissionChange(
  84. PermissionChange.Operation.ADD,
  85. request.mandatoryParam(PARAM_PERMISSION),
  86. project.orElse(null),
  87. user, permissionService);
  88. permissionUpdater.apply(dbSession, singletonList(change));
  89. }
  90. response.noContent();
  91. }
  92. }