From f0f35839eba2256b1166b21f683c1cba67eef6be Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Thu, 12 Nov 2015 17:08:52 +0100 Subject: [PATCH] SONAR-6947 api/permissions/remove_user use RemoveUserWsRequest --- .../permission/ws/RemoveUserAction.java | 32 +++++++-- .../permission/PermissionsWsClient.java | 8 +++ .../permission/RemoveUserWsRequest.java | 72 +++++++++++++++++++ 3 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/permission/RemoveUserWsRequest.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserAction.java index 14b71ec299a..136ce829b24 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/RemoveUserAction.java @@ -20,6 +20,7 @@ package org.sonar.server.permission.ws; +import com.google.common.base.Optional; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -27,11 +28,17 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.server.permission.PermissionChange; import org.sonar.server.permission.PermissionUpdater; -import org.sonar.server.permission.ws.PermissionRequest.Builder; +import org.sonarqube.ws.client.permission.RemoveUserWsRequest; +import static org.sonar.server.permission.ws.PermissionRequestValidator.validatePermission; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameter; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createUserLoginParameter; +import static org.sonar.server.permission.ws.WsProjectRef.newOptionalWsProjectRef; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN; public class RemoveUserAction implements PermissionsWsAction { @@ -64,19 +71,32 @@ public class RemoveUserAction implements PermissionsWsAction { @Override public void handle(Request request, Response response) throws Exception { + doHandle(toRemoveUserWsRequest(request)); + response.noContent(); + } + + private void doHandle(RemoveUserWsRequest request) { DbSession dbSession = dbClient.openSession(false); try { - PermissionRequest permissionRequest = new Builder(request).withUser().build(); + Optional projectRef = newOptionalWsProjectRef(request.getProjectId(), request.getProjectKey()); + validatePermission(request.getPermission(), projectRef); PermissionChange permissionChange = permissionChangeBuilder.buildUserPermissionChange( dbSession, - permissionRequest.permission(), - permissionRequest.project(), - permissionRequest.userLogin()); + request.getPermission(), + projectRef, + request.getLogin()); permissionUpdater.removePermission(permissionChange); - response.noContent(); } finally { dbClient.closeSession(dbSession); } } + + private static RemoveUserWsRequest toRemoveUserWsRequest(Request request) { + return new RemoveUserWsRequest() + .setPermission(request.mandatoryParam(PARAM_PERMISSION)) + .setLogin(request.mandatoryParam(PARAM_USER_LOGIN)) + .setProjectId(request.param(PARAM_PROJECT_ID)) + .setProjectKey(request.param(PARAM_PROJECT_KEY)); + } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java index f8844c287e0..cd0b590fecb 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java @@ -132,6 +132,14 @@ public class PermissionsWsClient { .setParam(PARAM_TEMPLATE_NAME, request.getTemplateName())); } + public void removeUser(RemoveUserWsRequest request) { + wsClient.execute(newPostRequest(action("remove_user")) + .setParam(PARAM_PERMISSION, request.getPermission()) + .setParam(PARAM_USER_LOGIN, request.getLogin()) + .setParam(PARAM_PROJECT_ID, request.getProjectId()) + .setParam(PARAM_PROJECT_KEY, request.getProjectKey())); + } + private static String action(String action) { return PermissionsWsParameters.ENDPOINT + "/" + action; } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/RemoveUserWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/RemoveUserWsRequest.java new file mode 100644 index 00000000000..342021b413b --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/RemoveUserWsRequest.java @@ -0,0 +1,72 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonarqube.ws.client.permission; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +import static java.util.Objects.requireNonNull; + +public class RemoveUserWsRequest { + private String permission; + private String login; + private String projectId; + private String projectKey; + + public String getPermission() { + return permission; + } + + public RemoveUserWsRequest setPermission(String permission) { + this.permission = requireNonNull(permission); + return this; + } + + @CheckForNull + public String getLogin() { + return login; + } + + public RemoveUserWsRequest setLogin(@Nullable String login) { + this.login = login; + return this; + } + + @CheckForNull + public String getProjectId() { + return projectId; + } + + public RemoveUserWsRequest setProjectId(@Nullable String projectId) { + this.projectId = projectId; + return this; + } + + @CheckForNull + public String getProjectKey() { + return projectKey; + } + + public RemoveUserWsRequest setProjectKey(@Nullable String projectKey) { + this.projectKey = projectKey; + return this; + } +} -- 2.39.5