Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RemoveUserFromTemplateAction.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.permission.ws.template;
  21. import javax.annotation.CheckForNull;
  22. import javax.annotation.Nullable;
  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.permission.template.PermissionTemplateDto;
  29. import org.sonar.server.permission.RequestValidator;
  30. import org.sonar.server.permission.UserId;
  31. import org.sonar.server.permission.ws.PermissionWsSupport;
  32. import org.sonar.server.permission.ws.PermissionsWsAction;
  33. import org.sonar.server.permission.ws.WsParameters;
  34. import org.sonar.server.user.UserSession;
  35. import static java.util.Objects.requireNonNull;
  36. import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdmin;
  37. import static org.sonar.server.permission.ws.WsParameters.createTemplateParameters;
  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_TEMPLATE_ID;
  41. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
  42. import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
  43. public class RemoveUserFromTemplateAction implements PermissionsWsAction {
  44. private final DbClient dbClient;
  45. private final PermissionWsSupport wsSupport;
  46. private final UserSession userSession;
  47. private final WsParameters wsParameters;
  48. private final RequestValidator requestValidator;
  49. public RemoveUserFromTemplateAction(DbClient dbClient, PermissionWsSupport wsSupport, UserSession userSession, WsParameters wsParameters, RequestValidator requestValidator) {
  50. this.dbClient = dbClient;
  51. this.wsSupport = wsSupport;
  52. this.userSession = userSession;
  53. this.wsParameters = wsParameters;
  54. this.requestValidator = requestValidator;
  55. }
  56. private static RemoveUserFromTemplateRequest toRemoveUserFromTemplateWsRequest(Request request) {
  57. return new RemoveUserFromTemplateRequest()
  58. .setPermission(request.mandatoryParam(PARAM_PERMISSION))
  59. .setLogin(request.mandatoryParam(PARAM_USER_LOGIN))
  60. .setTemplateId(request.param(PARAM_TEMPLATE_ID))
  61. .setTemplateName(request.param(PARAM_TEMPLATE_NAME));
  62. }
  63. @Override
  64. public void define(WebService.NewController context) {
  65. WebService.NewAction action = context
  66. .createAction("remove_user_from_template")
  67. .setPost(true)
  68. .setSince("5.2")
  69. .setDescription("Remove a user from a permission template.<br /> " +
  70. "Requires the following permission: 'Administer System'.")
  71. .setHandler(this);
  72. createTemplateParameters(action);
  73. wsParameters.createProjectPermissionParameter(action);
  74. createUserLoginParameter(action);
  75. }
  76. @Override
  77. public void handle(Request request, Response response) throws Exception {
  78. doHandle(toRemoveUserFromTemplateWsRequest(request));
  79. response.noContent();
  80. }
  81. private void doHandle(RemoveUserFromTemplateRequest request) {
  82. String permission = request.getPermission();
  83. String userLogin = request.getLogin();
  84. try (DbSession dbSession = dbClient.openSession(false)) {
  85. requestValidator.validateProjectPermission(permission);
  86. PermissionTemplateDto template = wsSupport.findTemplate(dbSession, WsTemplateRef.newTemplateRef(request.getTemplateId(), request.getTemplateName()));
  87. checkGlobalAdmin(userSession);
  88. UserId user = wsSupport.findUser(dbSession, userLogin);
  89. dbClient.permissionTemplateDao().deleteUserPermission(dbSession, template.getUuid(), user.getUuid(), permission);
  90. dbSession.commit();
  91. }
  92. }
  93. private static class RemoveUserFromTemplateRequest {
  94. private String login;
  95. private String permission;
  96. private String templateId;
  97. private String organization;
  98. private String templateName;
  99. public String getLogin() {
  100. return login;
  101. }
  102. public RemoveUserFromTemplateRequest setLogin(String login) {
  103. this.login = requireNonNull(login);
  104. return this;
  105. }
  106. public String getPermission() {
  107. return permission;
  108. }
  109. public RemoveUserFromTemplateRequest setPermission(String permission) {
  110. this.permission = requireNonNull(permission);
  111. return this;
  112. }
  113. @CheckForNull
  114. public String getTemplateId() {
  115. return templateId;
  116. }
  117. public RemoveUserFromTemplateRequest setTemplateId(@Nullable String templateId) {
  118. this.templateId = templateId;
  119. return this;
  120. }
  121. @CheckForNull
  122. public String getOrganization() {
  123. return organization;
  124. }
  125. public RemoveUserFromTemplateRequest setOrganization(@Nullable String s) {
  126. this.organization = s;
  127. return this;
  128. }
  129. @CheckForNull
  130. public String getTemplateName() {
  131. return templateName;
  132. }
  133. public RemoveUserFromTemplateRequest setTemplateName(@Nullable String templateName) {
  134. this.templateName = templateName;
  135. return this;
  136. }
  137. }
  138. }