From: Teryk Bellahsene Date: Tue, 5 Dec 2017 18:06:41 +0000 (+0100) Subject: SONAR-10123 UserSession#keepkeepAuthorizedComponents handles branches properly X-Git-Tag: 6.7.1~17 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ca42beccae145e371dd465cf5db2e34f1469b46c;p=sonarqube.git SONAR-10123 UserSession#keepkeepAuthorizedComponents handles branches properly --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ServerUserSession.java b/server/sonar-server/src/main/java/org/sonar/server/user/ServerUserSession.java index 28ec0313cd2..d8722146207 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/ServerUserSession.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ServerUserSession.java @@ -188,12 +188,12 @@ public class ServerUserSession extends AbstractUserSession { protected List doKeepAuthorizedComponents(String permission, Collection components) { try (DbSession dbSession = dbClient.openSession(false)) { Set projectUuids = components.stream() - .map(ComponentDto::projectUuid) + .map(c -> defaultIfEmpty(c.getMainBranchProjectUuid(), c.projectUuid())) .collect(MoreCollectors.toSet(components.size())); Set 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())); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ServerUserSessionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ServerUserSessionTest.java index d19500ea2e4..038cb3badd0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ServerUserSessionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ServerUserSessionTest.java @@ -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);