aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@sonarsource.com>2016-10-16 23:06:54 +0200
committerSimon Brandhof <simon.brandhof@sonarsource.com>2016-10-16 23:16:39 +0200
commitfcdbb222fe151a8b01fbe87402a3c3c5cb4a48b0 (patch)
tree33c5bb34b374ebe37060f4856cec5b6cce5add03
parent95829331ba25f1b4bd619b12814723dbe24d1ac1 (diff)
downloadsonarqube-fcdbb222fe151a8b01fbe87402a3c3c5cb4a48b0.tar.gz
sonarqube-fcdbb222fe151a8b01fbe87402a3c3c5cb4a48b0.zip
SONAR-8268 organization in WS api/permissions/delete_template
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/DeleteTemplateAction.java12
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java42
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/permission/DeleteTemplateWsRequest.java11
3 files changed, 39 insertions, 26 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/DeleteTemplateAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/DeleteTemplateAction.java
index e8cd35ae02c..144109f4dfe 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/DeleteTemplateAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/DeleteTemplateAction.java
@@ -19,6 +19,7 @@
*/
package org.sonar.server.permission.ws.template;
+import java.util.Optional;
import java.util.Set;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
@@ -31,14 +32,14 @@ import org.sonar.server.permission.ws.PermissionsWsAction;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.client.permission.DeleteTemplateWsRequest;
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createTemplateParameters;
import static org.sonar.server.permission.ws.template.WsTemplateRef.newTemplateRef;
import static org.sonar.server.ws.WsUtils.checkRequest;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
-// TODO move to package "template"
public class DeleteTemplateAction implements PermissionsWsAction {
private final DbClient dbClient;
private final UserSession userSession;
@@ -66,14 +67,16 @@ public class DeleteTemplateAction implements PermissionsWsAction {
@Override
public void handle(Request request, Response response) throws Exception {
- checkGlobalAdminUser(userSession);
doHandle(toDeleteTemplateWsRequest(request));
response.noContent();
}
private void doHandle(DeleteTemplateWsRequest request) {
try (DbSession dbSession = dbClient.openSession(false)) {
- PermissionTemplateDto template = finder.findTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
+ PermissionTemplateDto template = finder.findTemplate(dbSession, newTemplateRef(
+ request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
+ checkProjectAdmin(userSession, template.getOrganizationUuid(), Optional.empty());
+
checkTemplateUuidIsNotDefault(template.getUuid());
dbClient.permissionTemplateDao().deleteById(dbSession, template.getId());
dbSession.commit();
@@ -83,6 +86,7 @@ public class DeleteTemplateAction implements PermissionsWsAction {
private static DeleteTemplateWsRequest toDeleteTemplateWsRequest(Request request) {
return new DeleteTemplateWsRequest()
.setTemplateId(request.param(PARAM_TEMPLATE_ID))
+ .setOrganization(request.param(PARAM_ORGANIZATION_KEY))
.setTemplateName(request.param(PARAM_TEMPLATE_NAME));
}
diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java
index 99a6d7054b4..c2bbaeed651 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java
@@ -44,18 +44,16 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.internal.util.collections.Sets.newSet;
-import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
public class DeleteTemplateActionTest extends BasePermissionWsTest<DeleteTemplateAction> {
- private static final String TEMPLATE_UUID = "permission-template-uuid";
private static final String ACTION = "delete_template";
private DefaultPermissionTemplateFinder defaultTemplatePermissionFinder = mock(DefaultPermissionTemplateFinder.class);
- private PermissionTemplateDto permissionTemplate;
+ private PermissionTemplateDto template;
@Override
protected DeleteTemplateAction buildWsAction() {
@@ -64,26 +62,26 @@ public class DeleteTemplateActionTest extends BasePermissionWsTest<DeleteTemplat
@Before
public void setUp() {
- userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
+ loginAsAdminOnDefaultOrganization();
when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(Collections.emptySet());
- permissionTemplate = insertTemplateAndAssociatedPermissions(newPermissionTemplateDto().setUuid(TEMPLATE_UUID));
+ template = insertTemplateAndAssociatedPermissions();
}
@Test
public void delete_template_in_db() throws Exception {
- WsTester.Result result = newRequest(TEMPLATE_UUID);
+ WsTester.Result result = newRequest(template.getUuid());
assertThat(result.outputAsString()).isEmpty();
- assertThat(db.getDbClient().permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(db.getSession(), TEMPLATE_UUID)).isNull();
+ assertThat(db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid())).isNull();
}
@Test
public void delete_template_by_name_case_insensitive() throws Exception {
wsTester.newPostRequest(CONTROLLER, ACTION)
- .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
+ .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
.execute();
- assertThat(db.getDbClient().permissionTemplateDao().selectByUuidWithUserAndGroupPermissions(db.getSession(), TEMPLATE_UUID)).isNull();
+ assertThat(db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid())).isNull();
}
@Test
@@ -95,11 +93,12 @@ public class DeleteTemplateActionTest extends BasePermissionWsTest<DeleteTemplat
@Test
public void fail_if_template_is_default() throws Exception {
+ when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(newSet(template.getUuid()));
+
expectedException.expect(BadRequestException.class);
expectedException.expectMessage("It is not possible to delete a default template");
- when(defaultTemplatePermissionFinder.getDefaultTemplateUuids()).thenReturn(newSet(TEMPLATE_UUID));
- newRequest(TEMPLATE_UUID);
+ newRequest(template.getUuid());
}
@Test
@@ -107,7 +106,7 @@ public class DeleteTemplateActionTest extends BasePermissionWsTest<DeleteTemplat
expectedException.expect(UnauthorizedException.class);
userSession.anonymous();
- newRequest(TEMPLATE_UUID);
+ newRequest(template.getUuid());
}
@Test
@@ -115,7 +114,7 @@ public class DeleteTemplateActionTest extends BasePermissionWsTest<DeleteTemplat
expectedException.expect(ForbiddenException.class);
userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
- newRequest(TEMPLATE_UUID);
+ newRequest(template.getUuid());
}
@Test
@@ -129,26 +128,25 @@ public class DeleteTemplateActionTest extends BasePermissionWsTest<DeleteTemplat
public void delete_perm_tpl_characteristic_when_delete_template() throws Exception {
db.getDbClient().permissionTemplateCharacteristicDao().insert(db.getSession(), new PermissionTemplateCharacteristicDto()
.setPermission(UserRole.USER)
- .setTemplateId(permissionTemplate.getId())
+ .setTemplateId(template.getId())
.setWithProjectCreator(true)
.setCreatedAt(new Date().getTime())
.setUpdatedAt(new Date().getTime()));
db.commit();
- newRequest(TEMPLATE_UUID);
+ newRequest(template.getUuid());
- assertThat(db.getDbClient().permissionTemplateCharacteristicDao().selectByTemplateIds(db.getSession(), asList(permissionTemplate.getId()))).isEmpty();
+ assertThat(db.getDbClient().permissionTemplateCharacteristicDao().selectByTemplateIds(db.getSession(), asList(template.getId()))).isEmpty();
}
- private PermissionTemplateDto insertTemplateAndAssociatedPermissions(PermissionTemplateDto template) {
- db.getDbClient().permissionTemplateDao().insert(db.getSession(), template);
+ private PermissionTemplateDto insertTemplateAndAssociatedPermissions() {
+ PermissionTemplateDto dto = addTemplateToDefaultOrganization();
UserDto user = db.getDbClient().userDao().insert(db.getSession(), UserTesting.newUserDto().setActive(true));
GroupDto group = db.getDbClient().groupDao().insert(db.getSession(), GroupTesting.newGroupDto());
- db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), template.getId(), user.getId(), UserRole.ADMIN);
- db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), template.getId(), group.getId(), UserRole.CODEVIEWER);
+ db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), dto.getId(), user.getId(), UserRole.ADMIN);
+ db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), dto.getId(), group.getId(), UserRole.CODEVIEWER);
db.commit();
-
- return template;
+ return dto;
}
private WsTester.Result newRequest(@Nullable String id) throws Exception {
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/DeleteTemplateWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/DeleteTemplateWsRequest.java
index 31b56b3f8b9..f553a171904 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/DeleteTemplateWsRequest.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/DeleteTemplateWsRequest.java
@@ -24,6 +24,7 @@ import javax.annotation.Nullable;
public class DeleteTemplateWsRequest {
private String templateId;
+ private String organization;
private String templateName;
@CheckForNull
@@ -37,6 +38,16 @@ public class DeleteTemplateWsRequest {
}
@CheckForNull
+ public String getOrganization() {
+ return organization;
+ }
+
+ public DeleteTemplateWsRequest setOrganization(@Nullable String s) {
+ this.organization = s;
+ return this;
+ }
+
+ @CheckForNull
public String getTemplateName() {
return templateName;
}