]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8270 organization in WS api/permissions/add_project_creator_to_template
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 16 Oct 2016 21:03:59 +0000 (23:03 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sun, 16 Oct 2016 21:16:37 +0000 (23:16 +0200)
server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/AddProjectCreatorToTemplateAction.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/AddProjectCreatorToTemplateActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/AddProjectCreatorToTemplateWsRequest.java

index 51a9244ff9ef9338971b8791d3e07de192e2ea18..05e7de5c0adbded69cf2e55b39a03030aa1b5fe7 100644 (file)
@@ -34,10 +34,11 @@ import org.sonar.server.permission.ws.PermissionsWsAction;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.client.permission.AddProjectCreatorToTemplateWsRequest;
 
-import static org.sonar.server.permission.PermissionPrivilegeChecker.checkGlobalAdminUser;
+import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin;
 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.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
 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;
@@ -70,14 +71,16 @@ public class AddProjectCreatorToTemplateAction implements PermissionsWsAction {
 
   @Override
   public void handle(Request request, Response response) throws Exception {
-    checkGlobalAdminUser(userSession);
     doHandle(toWsRequest(request));
     response.noContent();
   }
 
   private void doHandle(AddProjectCreatorToTemplateWsRequest request) {
     try (DbSession dbSession = dbClient.openSession(false)) {
-      PermissionTemplateDto template = wsSupport.findTemplate(dbSession, WsTemplateRef.newTemplateRef(request.getTemplateId(), request.getTemplateName()));
+      PermissionTemplateDto template = wsSupport.findTemplate(dbSession, WsTemplateRef.newTemplateRef(
+        request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
+      checkProjectAdmin(userSession, template.getOrganizationUuid(), Optional.empty());
+
       Optional<PermissionTemplateCharacteristicDto> templatePermission = dbClient.permissionTemplateCharacteristicDao()
         .selectByPermissionAndTemplateId(dbSession, request.getPermission(), template.getId());
       if (templatePermission.isPresent()) {
@@ -111,6 +114,7 @@ public class AddProjectCreatorToTemplateAction implements PermissionsWsAction {
     AddProjectCreatorToTemplateWsRequest wsRequest = AddProjectCreatorToTemplateWsRequest.builder()
       .setPermission(request.mandatoryParam(PARAM_PERMISSION))
       .setTemplateId(request.param(PARAM_TEMPLATE_ID))
+      .setOrganization(request.param(PARAM_ORGANIZATION_KEY))
       .setTemplateName(request.param(PARAM_TEMPLATE_NAME))
       .build();
     validateProjectPermission(wsRequest.getPermission());
index fc1bdde293a6415e93d0a36922ccbe9ae1ff6bdf..fde918805377b3c6a645d958274086f82783760a 100644 (file)
@@ -24,18 +24,17 @@ import org.junit.Before;
 import org.junit.Test;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
-import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
 import org.sonar.db.permission.template.PermissionTemplateDto;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.permission.ws.BasePermissionWsTest;
 import org.sonar.server.ws.WsTester;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.when;
+import static org.sonar.core.permission.GlobalPermissions.QUALITY_GATE_ADMIN;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
@@ -53,13 +52,14 @@ public class AddProjectCreatorToTemplateActionTest extends BasePermissionWsTest<
 
   @Before
   public void setUp() {
-    userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
     template = insertTemplate();
     when(system.now()).thenReturn(2_000_000_000L);
   }
 
   @Test
   public void insert_row_when_no_template_permission() throws Exception {
+    loginAsAdminOnDefaultOrganization();
+
     newRequest()
       .setParam(PARAM_PERMISSION, UserRole.ADMIN)
       .setParam(PARAM_TEMPLATE_ID, template.getUuid())
@@ -70,6 +70,7 @@ public class AddProjectCreatorToTemplateActionTest extends BasePermissionWsTest<
 
   @Test
   public void update_row_when_existing_template_permission() throws Exception {
+    loginAsAdminOnDefaultOrganization();
     PermissionTemplateCharacteristicDto characteristic = db.getDbClient().permissionTemplateCharacteristicDao().insert(db.getSession(),
       new PermissionTemplateCharacteristicDto()
         .setTemplateId(template.getId())
@@ -93,6 +94,8 @@ public class AddProjectCreatorToTemplateActionTest extends BasePermissionWsTest<
 
   @Test
   public void fail_when_template_does_not_exist() throws Exception {
+    loginAsAdminOnDefaultOrganization();
+
     expectedException.expect(NotFoundException.class);
 
     newRequest()
@@ -103,29 +106,21 @@ public class AddProjectCreatorToTemplateActionTest extends BasePermissionWsTest<
 
   @Test
   public void fail_if_permission_is_not_a_project_permission() throws Exception {
+    loginAsAdminOnDefaultOrganization();
+
     expectedException.expect(IllegalArgumentException.class);
 
     newRequest()
-      .setParam(PARAM_PERMISSION, GlobalPermissions.QUALITY_GATE_ADMIN)
+      .setParam(PARAM_PERMISSION, QUALITY_GATE_ADMIN)
       .setParam(PARAM_TEMPLATE_ID, template.getUuid())
       .execute();
   }
 
   @Test
-  public void fail_if_not_authenticated() throws Exception {
-    expectedException.expect(UnauthorizedException.class);
-    userSession.anonymous();
-
-    newRequest()
-      .setParam(PARAM_PERMISSION, UserRole.ADMIN)
-      .setParam(PARAM_TEMPLATE_ID, template.getUuid())
-      .execute();
-  }
+  public void fail_if_not_admin_of_default_organization() throws Exception {
+    userSession.login().addOrganizationPermission(db.getDefaultOrganization().getUuid(), QUALITY_GATE_ADMIN);
 
-  @Test
-  public void fail_if_insufficient_privileges() throws Exception {
     expectedException.expect(ForbiddenException.class);
-    userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_GATE_ADMIN);
 
     newRequest()
       .setParam(PARAM_PERMISSION, UserRole.ADMIN)
index cbec95d29b0ae5ab03e5b9b13b022c9bcbaad735..cd8c2747e952b7bf3411b8f2df302891f0e30258 100644 (file)
@@ -26,11 +26,13 @@ import static java.util.Objects.requireNonNull;
 
 public class AddProjectCreatorToTemplateWsRequest {
   private final String templateId;
+  private final String organization;
   private final String templateName;
   private final String permission;
 
   public AddProjectCreatorToTemplateWsRequest(Builder builder) {
     this.templateId = builder.templateId;
+    this.organization = builder.organization;
     this.templateName = builder.templateName;
     this.permission = requireNonNull(builder.permission);
   }
@@ -40,6 +42,11 @@ public class AddProjectCreatorToTemplateWsRequest {
     return templateId;
   }
 
+  @CheckForNull
+  public String getOrganization() {
+    return organization;
+  }
+
   @CheckForNull
   public String getTemplateName() {
     return templateName;
@@ -55,6 +62,7 @@ public class AddProjectCreatorToTemplateWsRequest {
 
   public static class Builder {
     private String templateId;
+    private String organization;
     private String templateName;
     private String permission;
 
@@ -67,6 +75,11 @@ public class AddProjectCreatorToTemplateWsRequest {
       return this;
     }
 
+    public Builder setOrganization(String s) {
+      this.organization = s;
+      return this;
+    }
+
     public Builder setTemplateName(String templateName) {
       this.templateName = templateName;
       return this;