From: Teryk Bellahsene Date: Thu, 12 Nov 2015 15:59:54 +0000 (+0100) Subject: SONAR-6947 api/permissions/remove_group_from_template use RemoveGroupFromTemplateWsRe... X-Git-Tag: 5.3-RC1~258 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a000d18795598ee8c666dc7b90aa57eb3904bdd1;p=sonarqube.git SONAR-6947 api/permissions/remove_group_from_template use RemoveGroupFromTemplateWsRequest --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/RemoveGroupFromTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/RemoveGroupFromTemplateAction.java index a79f6877298..2ea303c3ef6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/RemoveGroupFromTemplateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/RemoveGroupFromTemplateAction.java @@ -29,17 +29,23 @@ import org.sonar.db.permission.PermissionTemplateDto; import org.sonar.db.user.GroupDto; import org.sonar.server.permission.ws.PermissionDependenciesFinder; import org.sonar.server.permission.ws.PermissionsWsAction; -import org.sonar.server.usergroups.ws.WsGroupRef; -import org.sonar.server.permission.ws.WsTemplateRef; import org.sonar.server.user.UserSession; +import org.sonar.server.usergroups.ws.WsGroupRef; +import org.sonarqube.ws.client.permission.RemoveGroupFromTemplateWsRequest; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; +import static org.sonar.server.permission.ws.PermissionRequestValidator.validateProjectPermission; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createGroupIdParameter; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createGroupNameParameter; 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.PermissionRequestValidator.validateProjectPermission; +import static org.sonar.server.permission.ws.WsTemplateRef.newTemplateRef; +import static org.sonar.server.usergroups.ws.WsGroupRef.newWsGroupRef; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME; +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; public class RemoveGroupFromTemplateAction implements PermissionsWsAction { private final DbClient dbClient; @@ -70,17 +76,21 @@ public class RemoveGroupFromTemplateAction implements PermissionsWsAction { } @Override - public void handle(Request wsRequest, Response wsResponse) throws Exception { + public void handle(Request request, Response response) throws Exception { checkGlobalAdminUser(userSession); + doHandle(toRemoveGroupFromTemplateWsRequest(request)); + response.noContent(); + } - String permission = wsRequest.mandatoryParam(PARAM_PERMISSION); - WsGroupRef group = WsGroupRef.newWsGroupRefFromPermissionRequest(wsRequest); + private void doHandle(RemoveGroupFromTemplateWsRequest request) { + String permission = request.getPermission(); + Long groupIdInRequest = request.getGroupId() == null ? null : Long.valueOf(request.getGroupId()); + WsGroupRef group = newWsGroupRef(groupIdInRequest, request.getGroupName()); DbSession dbSession = dbClient.openSession(false); try { validateProjectPermission(permission); - - PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, WsTemplateRef.fromRequest(wsRequest)); + PermissionTemplateDto template = dependenciesFinder.getTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName())); GroupDto groupDto = dependenciesFinder.getGroup(dbSession, group); Long groupId = groupDto == null ? null : groupDto.getId(); @@ -88,7 +98,14 @@ public class RemoveGroupFromTemplateAction implements PermissionsWsAction { } finally { dbClient.closeSession(dbSession); } + } - wsResponse.noContent(); + private static RemoveGroupFromTemplateWsRequest toRemoveGroupFromTemplateWsRequest(Request request) { + return new RemoveGroupFromTemplateWsRequest() + .setPermission(request.mandatoryParam(PARAM_PERMISSION)) + .setGroupId(request.param(PARAM_GROUP_ID)) + .setGroupName(request.param(PARAM_GROUP_NAME)) + .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 5db97477bd1..f8844c287e0 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 @@ -123,6 +123,15 @@ public class PermissionsWsClient { .setParam(PARAM_PROJECT_KEY, request.getProjectKey())); } + public void removeGroupFromTemplate(RemoveGroupFromTemplateWsRequest request) { + wsClient.execute(newPostRequest(action("remove_group_from_template")) + .setParam(PARAM_PERMISSION, request.getPermission()) + .setParam(PARAM_GROUP_ID, request.getGroupId()) + .setParam(PARAM_GROUP_NAME, request.getGroupName()) + .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/RemoveGroupFromTemplateWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/RemoveGroupFromTemplateWsRequest.java new file mode 100644 index 00000000000..48b48e39398 --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/RemoveGroupFromTemplateWsRequest.java @@ -0,0 +1,83 @@ +/* + * 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 RemoveGroupFromTemplateWsRequest { + private String permission; + private String groupId; + private String groupName; + private String templateId; + private String templateName; + + public String getPermission() { + return permission; + } + + public RemoveGroupFromTemplateWsRequest setPermission(String permission) { + this.permission = requireNonNull(permission); + return this; + } + + @CheckForNull + public String getGroupId() { + return groupId; + } + + public RemoveGroupFromTemplateWsRequest setGroupId(@Nullable String groupId) { + this.groupId = groupId; + return this; + } + + @CheckForNull + public String getGroupName() { + return groupName; + } + + public RemoveGroupFromTemplateWsRequest setGroupName(@Nullable String groupName) { + this.groupName = groupName; + return this; + } + + @CheckForNull + public String getTemplateId() { + return templateId; + } + + public RemoveGroupFromTemplateWsRequest setTemplateId(@Nullable String templateId) { + this.templateId = templateId; + return this; + } + + @CheckForNull + public String getTemplateName() { + return templateName; + } + + public RemoveGroupFromTemplateWsRequest setTemplateName(@Nullable String templateName) { + this.templateName = templateName; + return this; + } +}