import java.util.List;
import java.util.function.Consumer;
+import java.util.function.Predicate;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.server.ui.PageRepository;
import org.sonar.server.user.UserSession;
+import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.server.ws.KeyExamples.KEY_ORG_EXAMPLE_001;
import static org.sonar.server.ws.WsUtils.checkFoundWithOptional;
.prop("canProvisionProjects", userSession.hasPermission(OrganizationPermission.PROVISION_PROJECTS, organization))
.prop("canDelete", organization.isGuarded() ? userSession.isSystemAdministrator() : userSession.hasPermission(OrganizationPermission.ADMINISTER, organization))
.prop("isDefault", organization.getKey().equals(defaultOrganizationProvider.get().getKey()));
- List<Page> pages = pageRepository.getOrganizationPages(false);
+ Predicate<Page> personalOrgForBilling = page -> organization.getUserId() == null || !page.getKey().startsWith("billing/");
+ List<Page> pages = pageRepository.getOrganizationPages(false).stream().filter(personalOrgForBilling).collect(toList());
json.name("pages");
writePages(json, pages);
if (userSession.hasPermission(OrganizationPermission.ADMINISTER, organization)) {
- List<Page> adminPages = pageRepository.getOrganizationPages(true);
+ List<Page> adminPages = pageRepository.getOrganizationPages(true).stream().filter(personalOrgForBilling).collect(toList());
json.name("adminPages");
writePages(json, adminPages);
}
.doesNotContain("my-plugin/org-admin-page");
}
+ @Test
+ public void filter_out_billing_pages_for_personal_organizations() {
+ initWithPages(
+ Page.builder("my-plugin/org-page").setName("Organization page").setScope(ORGANIZATION).build(),
+ Page.builder("my-plugin/org-admin-page").setName("Organization admin page").setScope(ORGANIZATION).setAdmin(true).build(),
+ Page.builder("billing/org-page").setName("Organization page").setScope(ORGANIZATION).build(),
+ Page.builder("billing/org-admin-page").setName("Organization admin page").setScope(ORGANIZATION).setAdmin(true).build()
+ );
+ OrganizationDto organization = dbTester.organizations().insert(dto -> dto.setUserId(101));
+ userSession.logIn().addPermission(ADMINISTER, organization);
+
+ TestResponse response = executeRequest(organization);
+
+ assertThat(response.getInput())
+ .contains("my-plugin/org-page", "my-plugin/org-admin-page")
+ .doesNotContain("billing/org-page")
+ .doesNotContain("billing/org-admin-page");
+ }
+
+ @Test
+ public void include_billing_pages_for_non_personal_organizations() {
+ initWithPages(
+ Page.builder("billing/org-page").setName("Organization page").setScope(ORGANIZATION).build(),
+ Page.builder("billing/org-admin-page").setName("Organization admin page").setScope(ORGANIZATION).setAdmin(true).build()
+ );
+ OrganizationDto organization = dbTester.organizations().insert(dto -> dto.setUserId(null));
+ userSession.logIn().addPermission(ADMINISTER, organization);
+
+ TestResponse response = executeRequest(organization);
+
+ assertThat(response.getInput())
+ .contains("billing/org-page", "billing/org-admin-page");
+ }
+
@Test
public void returns_non_admin_and_canDelete_false_when_user_not_logged_in_and_key_is_the_default_organization() {
TestResponse response = executeRequest(dbTester.getDefaultOrganization());