]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6851 apply default permissions to new provisioned projects
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 28 Sep 2015 08:03:51 +0000 (10:03 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Mon, 28 Sep 2015 09:10:17 +0000 (11:10 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/ReportSubmitter.java
server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java
server/sonar-server/src/test/java/org/sonar/server/computation/ReportSubmitterTest.java

index bae1073871c053c71b1a297536f32076b1889587..0a564abb0cd73c7e0c802535becb5b3d076e8c99 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.db.ce.CeTaskTypes;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.component.ComponentService;
 import org.sonar.server.component.NewComponent;
+import org.sonar.server.permission.PermissionService;
 import org.sonar.server.user.UserSession;
 
 @ServerSide
@@ -39,13 +40,15 @@ public class ReportSubmitter {
   private final UserSession userSession;
   private final ReportFiles reportFiles;
   private final ComponentService componentService;
+  private final PermissionService permissionService;
 
   public ReportSubmitter(CeQueue queue, UserSession userSession, ReportFiles reportFiles,
-    ComponentService componentService) {
+    ComponentService componentService, PermissionService permissionService) {
     this.queue = queue;
     this.userSession = userSession;
     this.reportFiles = reportFiles;
     this.componentService = componentService;
+    this.permissionService = permissionService;
   }
 
   public CeTask submit(String projectKey, @Nullable String projectBranch, @Nullable String projectName, InputStream reportInput) {
@@ -60,6 +63,7 @@ public class ReportSubmitter {
       newProject.setQualifier(Qualifiers.PROJECT);
       // no need to verify the permission "provisioning" as it's already handled by componentService
       project = componentService.create(newProject);
+      permissionService.applyDefaultPermissionTemplate(project.getKey());
     }
 
     // the report file must be saved before submitting the task
index 4849b402f0190a62e6d5ca77af807f7b32f2c99f..ba3fca56e4e638dd14418067a6a18faa40479691 100644 (file)
@@ -143,9 +143,10 @@ public class PermissionService {
     }
   }
 
+  /**
+   * Important - this method checks caller permissions
+   */
   public void applyDefaultPermissionTemplate(final String componentKey) {
-    userSession.checkLoggedIn();
-
     DbSession session = dbClient.openSession(false);
     try {
       ComponentDto component = componentFinder.getByKey(session, componentKey);
@@ -172,6 +173,9 @@ public class PermissionService {
     applyPermissionTemplate(query);
   }
 
+  /**
+   * Important - this method checks caller permissions
+   */
   public void applyPermissionTemplate(ApplyPermissionTemplateQuery query) {
     DbSession dbSession = dbClient.openSession(false);
     try {
index 4151a532fb148d344baede34311cd95900d6fa42..6abbf2a15ae127ae80694a2eddf3b3ad72d5eb9e 100644 (file)
@@ -29,12 +29,14 @@ import org.sonar.db.ce.CeTaskTypes;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.component.ComponentService;
 import org.sonar.server.component.NewComponent;
+import org.sonar.server.permission.PermissionService;
 import org.sonar.server.tester.UserSessionRule;
 
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.argThat;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.mockito.Mockito.when;
 
 public class ReportSubmitterTest {
@@ -45,7 +47,8 @@ public class ReportSubmitterTest {
   CeQueue queue = mock(CeQueueImpl.class);
   ReportFiles reportFiles = mock(ReportFiles.class);
   ComponentService componentService = mock(ComponentService.class);
-  ReportSubmitter underTest = new ReportSubmitter(queue, userSession, reportFiles, componentService);
+  PermissionService permissionService = mock(PermissionService.class);
+  ReportSubmitter underTest = new ReportSubmitter(queue, userSession, reportFiles, componentService, permissionService);
 
   @Test
   public void submit_a_report_on_existing_project() {
@@ -55,6 +58,7 @@ public class ReportSubmitterTest {
 
     underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
 
+    verifyZeroInteractions(permissionService);
     verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
       @Override
       protected boolean matchesSafely(CeTaskSubmit submit) {
@@ -74,10 +78,11 @@ public class ReportSubmitterTest {
     when(queue.prepareSubmit()).thenReturn(new CeTaskSubmit.Builder("TASK_1"));
     userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.PROVISIONING);
     when(componentService.getNullableByKey("MY_PROJECT")).thenReturn(null);
-    when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid("P1"));
+    when(componentService.create(any(NewComponent.class))).thenReturn(new ComponentDto().setUuid("P1").setKey("MY_PROJECT"));
 
     underTest.submit("MY_PROJECT", null, "My Project", IOUtils.toInputStream("{binary}"));
 
+    verify(permissionService).applyDefaultPermissionTemplate("MY_PROJECT");
     verify(queue).submit(argThat(new TypeSafeMatcher<CeTaskSubmit>() {
       @Override
       protected boolean matchesSafely(CeTaskSubmit submit) {