From e10c0b208b1274f2ce58a39e76a4142c69997754 Mon Sep 17 00:00:00 2001 From: Jacek Date: Mon, 29 Nov 2021 11:09:49 +0100 Subject: [PATCH] SONAR-15702 Add 'canBrowseAllChildProjects' to ComponentAction --- .../sonar/server/ui/ws/ComponentAction.java | 3 ++ .../server/ui/ws/ComponentActionTest.java | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java index 521d9aa52a4..abbcc844f42 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ui/ws/ComponentAction.java @@ -213,6 +213,9 @@ public class ComponentAction implements NavigationWsAction { if (branch != null) { json.prop("branch", branch); } + if (Qualifiers.APP.equals(component.qualifier())) { + json.prop("canBrowseAllChildProjects", userSession.hasChildProjectsPermission(USER, component)); + } if (QUALIFIERS_WITH_VISIBILITY.contains(component.qualifier())) { json.prop("visibility", Visibility.getLabel(component.isPrivate())); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java index a12f8aa65e4..149999a499d 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java @@ -81,6 +81,7 @@ import static org.mockito.Mockito.when; import static org.sonar.api.measures.CoreMetrics.QUALITY_PROFILES_KEY; import static org.sonar.api.utils.DateUtils.parseDateTime; import static org.sonar.api.web.page.Page.Scope.COMPONENT; +import static org.sonar.db.component.ComponentDbTester.toProjectDto; import static org.sonar.db.component.ComponentTesting.newDirectory; import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.ComponentTesting.newModuleDto; @@ -244,6 +245,42 @@ public class ComponentActionTest { "}"); } + @Test + public void return_canBrowseAllChildProjects_when_component_is_an_application() { + db.qualityGates().createDefaultQualityGate(); + ComponentDto application1 = db.components().insertPrivateApplication(); + ComponentDto project11 = db.components().insertPrivateProject(); + ComponentDto project12 = db.components().insertPrivateProject(); + userSession.registerApplication( + toProjectDto(application1, 1L), + toProjectDto(project11, 1L), + toProjectDto(project12, 1L)); + userSession.addProjectPermission(UserRole.USER, application1, project11, project12); + + ComponentDto application2 = db.components().insertPrivateApplication(); + ComponentDto project21 = db.components().insertPrivateProject(); + ComponentDto project22 = db.components().insertPrivateProject(); + userSession.registerApplication( + toProjectDto(application2, 1L), + toProjectDto(project21, 1L), + toProjectDto(project22, 1L)); + userSession.addProjectPermission(UserRole.USER, application2, project21); + + init(); + + // access to all projects (project11, project12) + String json = execute(application1.getDbKey()); + assertJson(json).isSimilarTo("{" + + "\"canBrowseAllChildProjects\":true" + + "}"); + + // access to some projects (project11) + json = execute(application2.getDbKey()); + assertJson(json).isSimilarTo("{" + + "\"canBrowseAllChildProjects\":false" + + "}"); + } + @Test public void return_component_info_when_snapshot() { ComponentDto project = insertProject(); -- 2.39.5