]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10123 UserSession#keepkeepAuthorizedComponents handles branches properly
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 5 Dec 2017 18:06:41 +0000 (19:06 +0100)
committerTeryk Bellahsene <teryk@users.noreply.github.com>
Thu, 7 Dec 2017 13:39:16 +0000 (14:39 +0100)
server/sonar-server/src/main/java/org/sonar/server/user/ServerUserSession.java
server/sonar-server/src/test/java/org/sonar/server/user/ServerUserSessionTest.java

index 28ec0313cd27e7daadc33f5fa3c9b819a291c395..d87221462070a2ed5886fbd25f8bf45ec39620ea 100644 (file)
@@ -188,12 +188,12 @@ public class ServerUserSession extends AbstractUserSession {
   protected List<ComponentDto> doKeepAuthorizedComponents(String permission, Collection<ComponentDto> components) {
     try (DbSession dbSession = dbClient.openSession(false)) {
       Set<String> projectUuids = components.stream()
-        .map(ComponentDto::projectUuid)
+        .map(c -> defaultIfEmpty(c.getMainBranchProjectUuid(), c.projectUuid()))
         .collect(MoreCollectors.toSet(components.size()));
       Set<String> authorizedProjectUuids = dbClient.authorizationDao().keepAuthorizedProjectUuids(dbSession, projectUuids, getUserId(), permission);
 
       return components.stream()
-        .filter(c -> authorizedProjectUuids.contains(c.projectUuid()))
+        .filter(c -> authorizedProjectUuids.contains(c.projectUuid()) || authorizedProjectUuids.contains(c.getMainBranchProjectUuid()))
         .collect(MoreCollectors.toList(components.size()));
     }
   }
index d19500ea2e4ff536523ba6eb79a5c12c7b707f98..038cb3badd0f402183114c8e62fcf8a6c94835b7 100644 (file)
@@ -40,6 +40,7 @@ import org.sonar.server.organization.TestDefaultOrganizationProvider;
 import org.sonar.server.organization.TestOrganizationFlags;
 
 import static com.google.common.base.Preconditions.checkState;
+import static java.util.Arrays.asList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
@@ -447,6 +448,17 @@ public class ServerUserSessionTest {
       .containsExactly(privateProject, publicProject);
   }
 
+  @Test
+  public void keepAuthorizedComponents_on_branches() {
+    user = db.users().insertUser();
+    db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, privateProject);
+    ComponentDto privateBranchProject = db.components().insertProjectBranch(privateProject);
+    UserSession underTest = newUserSession(user);
+
+    assertThat(underTest.keepAuthorizedComponents(UserRole.ADMIN, asList(privateProject, privateBranchProject)))
+      .containsExactlyInAnyOrder(privateProject, privateBranchProject);
+  }
+
   @Test
   public void isSystemAdministrator_returns_true_if_org_feature_is_enabled_and_user_is_root() {
     organizationFlags.setEnabled(true);