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.

RemoveUserAction.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.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.component.ComponentDto;
  28. import org.sonar.db.user.UserId;
  29. import org.sonar.server.exceptions.BadRequestException;
  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.db.permission.GlobalPermission.ADMINISTER;
  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 RemoveUserAction implements PermissionsWsAction {
  42. public static final String ACTION = "remove_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. public RemoveUserAction(DbClient dbClient, UserSession userSession, PermissionUpdater permissionUpdater, PermissionWsSupport wsSupport,
  50. WsParameters wsParameters, PermissionService permissionService) {
  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. }
  58. @Override
  59. public void define(WebService.NewController context) {
  60. WebService.NewAction action = context.createAction(ACTION)
  61. .setDescription("Remove permission from a user.<br /> " +
  62. "This service defaults to global permissions, but can be limited to project permissions by providing project id or project key.<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. .setPost(true)
  70. .setHandler(this);
  71. wsParameters.createPermissionParameter(action, "The permission you would like to revoke from the user.");
  72. createUserLoginParameter(action);
  73. createProjectParameters(action);
  74. }
  75. @Override
  76. public void handle(Request request, Response response) throws Exception {
  77. try (DbSession dbSession = dbClient.openSession(false)) {
  78. UserId user = wsSupport.findUser(dbSession, request.mandatoryParam(PARAM_USER_LOGIN));
  79. String permission = request.mandatoryParam(PARAM_PERMISSION);
  80. if (ADMINISTER.getKey().equals(permission) && user.getLogin().equals(userSession.getLogin())) {
  81. throw BadRequestException.create("As an admin, you can't remove your own admin right");
  82. }
  83. Optional<ComponentDto> project = wsSupport.findProject(dbSession, request);
  84. wsSupport.checkPermissionManagementAccess(userSession, project.orElse(null));
  85. PermissionChange change = new UserPermissionChange(
  86. PermissionChange.Operation.REMOVE,
  87. permission,
  88. project.orElse(null),
  89. user, permissionService);
  90. permissionUpdater.apply(dbSession, singletonList(change));
  91. response.noContent();
  92. }
  93. }
  94. }