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()));
}
}
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;
.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);