diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-11-12 17:19:38 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-11-17 13:41:02 +0100 |
commit | 3682b11b6704b11688bdf435159c7f8eb651e03c (patch) | |
tree | 1dbb341bca58e60ad7f87ee5f49d59a537bc3550 | |
parent | f0f35839eba2256b1166b21f683c1cba67eef6be (diff) | |
download | sonarqube-3682b11b6704b11688bdf435159c7f8eb651e03c.tar.gz sonarqube-3682b11b6704b11688bdf435159c7f8eb651e03c.zip |
SONAR-6947 api/permissions/remove_user_from_template use RemoveUserFromTemplateWsRequest
4 files changed, 103 insertions, 11 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/RemoveUserFromTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/RemoveUserFromTemplateAction.java index 3a1254cd483..18e125af5e6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/RemoveUserFromTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/RemoveUserFromTemplateAction.java @@ -31,14 +31,17 @@ import org.sonar.server.permission.ws.PermissionDependenciesFinder; import org.sonar.server.permission.ws.PermissionsWsAction; import org.sonar.server.permission.ws.WsTemplateRef; import org.sonar.server.user.UserSession; +import org.sonarqube.ws.client.permission.RemoveUserFromTemplateWsRequest; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN; +import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectPermissionParameter; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createUserLoginParameter; -import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN; public class RemoveUserFromTemplateAction implements PermissionsWsAction { private final DbClient dbClient; @@ -67,16 +70,21 @@ public class RemoveUserFromTemplateAction implements PermissionsWsAction { } @Override - public void handle(Request wsRequest, Response wsResponse) throws Exception { + public void handle(Request request, Response response) throws Exception { + doHandle(toRemoveUserFromTemplateWsRequest(request)); + response.noContent(); + } + + private void doHandle(RemoveUserFromTemplateWsRequest request) { checkGlobalAdminUser(userSession); - String permission = wsRequest.mandatoryParam(PARAM_PERMISSION); - String userLogin = wsRequest.mandatoryParam(PARAM_USER_LOGIN); + String permission = request.getPermission(); + String userLogin = request.getLogin(); DbSession dbSession = dbClient.openSession(false); try { validateProjectPermission(permission); - PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, WsTemplateRef.fromRequest(wsRequest)); + PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, WsTemplateRef.newTemplateRef(request.getTemplateId(), request.getTemplateName())); UserDto user = dependenciesFinder.getUser(dbSession, userLogin); dbClient.permissionTemplateDao().deleteUserPermission(dbSession, template.getId(), user.getId(), permission); @@ -84,7 +92,13 @@ public class RemoveUserFromTemplateAction implements PermissionsWsAction { } finally { dbClient.closeSession(dbSession); } + } - wsResponse.noContent(); + private static RemoveUserFromTemplateWsRequest toRemoveUserFromTemplateWsRequest(Request request) { + return new RemoveUserFromTemplateWsRequest() + .setPermission(request.mandatoryParam(PARAM_PERMISSION)) + .setLogin(request.mandatoryParam(PARAM_USER_LOGIN)) + .setTemplateId(request.param(PARAM_TEMPLATE_ID)) + .setTemplateName(request.param(PARAM_TEMPLATE_NAME)); } } 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 cd0b590fecb..7988dce7689 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 @@ -140,6 +140,14 @@ public class PermissionsWsClient { .setParam(PARAM_PROJECT_KEY, request.getProjectKey())); } + public void removeUserFromTemplate(RemoveUserFromTemplateWsRequest request) { + wsClient.execute(newPostRequest(action("remove_user_from_template")) + .setParam(PARAM_PERMISSION, request.getPermission()) + .setParam(PARAM_USER_LOGIN, request.getLogin()) + .setParam(PARAM_TEMPLATE_ID, request.getTemplateId()) + .setParam(PARAM_TEMPLATE_NAME, request.getTemplateName())); + } + private static String action(String action) { return PermissionsWsParameters.ENDPOINT + "/" + action; } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/RemoveUserFromTemplateWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/RemoveUserFromTemplateWsRequest.java new file mode 100644 index 00000000000..a1829e9adb1 --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/RemoveUserFromTemplateWsRequest.java @@ -0,0 +1,71 @@ +/* + * 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 RemoveUserFromTemplateWsRequest { + private String login; + private String permission; + private String templateId; + private String templateName; + + public String getLogin() { + return login; + } + + public RemoveUserFromTemplateWsRequest setLogin(String login) { + this.login = requireNonNull(login); + return this; + } + + public String getPermission() { + return permission; + } + + public RemoveUserFromTemplateWsRequest setPermission(String permission) { + this.permission = requireNonNull(permission); + return this; + } + + @CheckForNull + public String getTemplateId() { + return templateId; + } + + public RemoveUserFromTemplateWsRequest setTemplateId(@Nullable String templateId) { + this.templateId = templateId; + return this; + } + + @CheckForNull + public String getTemplateName() { + return templateName; + } + + public RemoveUserFromTemplateWsRequest setTemplateName(@Nullable String templateName) { + this.templateName = templateName; + return this; + } +} 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 index 342021b413b..7766cf5233d 100644 --- 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 @@ -40,13 +40,12 @@ public class RemoveUserWsRequest { return this; } - @CheckForNull public String getLogin() { return login; } - public RemoveUserWsRequest setLogin(@Nullable String login) { - this.login = login; + public RemoveUserWsRequest setLogin(String login) { + this.login = requireNonNull(login); return this; } |