]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8268 organization in WS api/permissions/delete_template
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 16 Oct 2016 21:06:54 +0000 (23:06 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 16 Oct 2016 21:16:39 +0000 (23:16 +0200)
server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/DeleteTemplateAction.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/DeleteTemplateActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/DeleteTemplateWsRequest.java

index e8cd35ae02c8a4e73e6e36556ba84f512c25ae90..144109f4dfe7933e5f05a2a45f55e7142453835b 100644 (file)
@@ -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));
   }
 
index 99a6d7054b49a8f20197a8985d354b699c7da58c..c2bbaeed651ae969764867fdae788a73197b9f7b 100644 (file)
@@ -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 {
index 31b56b3f8b92379616de1677a5207b3f293bc2dc..f553a1719042dcd225e70e96d10057652cad87f2 100644 (file)
@@ -24,6 +24,7 @@ import javax.annotation.Nullable;
 
 public class DeleteTemplateWsRequest {
   private String templateId;
+  private String organization;
   private String templateName;
 
   @CheckForNull
@@ -36,6 +37,16 @@ public class DeleteTemplateWsRequest {
     return this;
   }
 
+  @CheckForNull
+  public String getOrganization() {
+    return organization;
+  }
+
+  public DeleteTemplateWsRequest setOrganization(@Nullable String s) {
+    this.organization = s;
+    return this;
+  }
+
   @CheckForNull
   public String getTemplateName() {
     return templateName;