From f6f2bbf78e59251b2bccb4dd757bb203c746e518 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 20 Feb 2017 11:49:48 +0100 Subject: [PATCH] SONAR-8658 add field configuration.canApplyPermissionTemplate to response of api/navigation/component --- .../sonar/server/ui/ws/ComponentAction.java | 29 ++++++++++--------- .../server/ui/ws/ComponentActionTest.java | 18 ++++++++++++ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java index fa72601e834..dd8839b82d5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/ComponentAction.java @@ -61,6 +61,7 @@ import static org.sonar.api.web.UserRole.ADMIN; import static org.sonar.api.web.UserRole.USER; import static org.sonar.core.permission.GlobalPermissions.QUALITY_GATE_ADMIN; import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN; +import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN; import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; @@ -211,12 +212,12 @@ public class ComponentAction implements NavigationWsAction { } private void writeConfiguration(JsonWriter json, ComponentDto component) { - boolean isAdmin = userSession.hasComponentPermission(ADMIN, component); + boolean isProjectAdmin = userSession.hasComponentPermission(ADMIN, component); json.name("configuration").beginObject(); - writeConfigPageAccess(json, isAdmin, component); + writeConfigPageAccess(json, isProjectAdmin, component); - if (isAdmin) { + if (isProjectAdmin) { json.name("extensions").beginArray(); List configPages = pageRepository.getComponentPages(true, component.qualifier()); configPages.forEach(page -> writePage(json, page)); @@ -225,21 +226,23 @@ public class ComponentAction implements NavigationWsAction { json.endObject(); } - private void writeConfigPageAccess(JsonWriter json, boolean isAdmin, ComponentDto component) { + private void writeConfigPageAccess(JsonWriter json, boolean isProjectAdmin, ComponentDto component) { boolean isProject = Qualifiers.PROJECT.equals(component.qualifier()); - boolean showManualMeasures = isAdmin && !Qualifiers.DIRECTORY.equals(component.qualifier()); + boolean showManualMeasures = isProjectAdmin && !Qualifiers.DIRECTORY.equals(component.qualifier()); boolean isQualityProfileAdmin = userSession.hasOrganizationPermission(component.getOrganizationUuid(), QUALITY_PROFILE_ADMIN); boolean isQualityGateAdmin = userSession.hasOrganizationPermission(component.getOrganizationUuid(), QUALITY_GATE_ADMIN); + boolean isOrganizationAdmin = userSession.hasOrganizationPermission(component.getOrganizationUuid(), SYSTEM_ADMIN); - json.prop("showSettings", isAdmin && componentTypeHasProperty(component, PROPERTY_CONFIGURABLE)); - json.prop("showQualityProfiles", isProject && (isAdmin || isQualityProfileAdmin)); - json.prop("showQualityGates", isProject && (isAdmin || isQualityGateAdmin)); + json.prop("showSettings", isProjectAdmin && componentTypeHasProperty(component, PROPERTY_CONFIGURABLE)); + json.prop("showQualityProfiles", isProject && (isProjectAdmin || isQualityProfileAdmin)); + json.prop("showQualityGates", isProject && (isProjectAdmin || isQualityGateAdmin)); json.prop("showManualMeasures", showManualMeasures); - json.prop("showLinks", isAdmin && isProject); - json.prop("showPermissions", isAdmin && componentTypeHasProperty(component, PROPERTY_HAS_ROLE_POLICY)); - json.prop("showHistory", isAdmin && componentTypeHasProperty(component, PROPERTY_MODIFIABLE_HISTORY)); - json.prop("showUpdateKey", isAdmin && componentTypeHasProperty(component, PROPERTY_UPDATABLE_KEY)); - json.prop("showBackgroundTasks", isAdmin); + json.prop("showLinks", isProjectAdmin && isProject); + json.prop("showPermissions", isProjectAdmin && componentTypeHasProperty(component, PROPERTY_HAS_ROLE_POLICY)); + json.prop("showHistory", isProjectAdmin && componentTypeHasProperty(component, PROPERTY_MODIFIABLE_HISTORY)); + json.prop("showUpdateKey", isProjectAdmin && componentTypeHasProperty(component, PROPERTY_UPDATABLE_KEY)); + json.prop("showBackgroundTasks", isProjectAdmin); + json.prop("canApplyPermissionTemplate", isOrganizationAdmin); } private boolean componentTypeHasProperty(ComponentDto component, String resourceTypeProperty) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java index 38baa6485f7..0a293bc8974 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java @@ -35,6 +35,7 @@ import org.sonar.api.web.page.Page; import org.sonar.api.web.page.Page.Qualifier; import org.sonar.api.web.page.PageDefinition; import org.sonar.core.component.DefaultResourceTypes; +import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.platform.PluginRepository; import org.sonar.db.DbClient; import org.sonar.db.DbTester; @@ -380,6 +381,23 @@ public class ComponentActionTest { assertJson(result).ignoreFields("snapshotDate", "key", "qualityGate.key").isSimilarTo(ws.getDef().responseExampleAsString()); } + @Test + public void canApplyPermissionTemplate_is_true_if_logged_in_as_organization_administrator() { + init(createPages()); + OrganizationDto org = dbTester.organizations().insert(); + ComponentDto project = dbTester.components().insertProject(org); + + userSessionRule.logIn() + .addProjectUuidPermissions(UserRole.ADMIN, project.uuid()) + .addOrganizationPermission(org.getUuid(), GlobalPermissions.SYSTEM_ADMIN); + assertJson(execute(project.key())).isSimilarTo("{\"configuration\": {\"canApplyPermissionTemplate\": true}}"); + + userSessionRule.logIn() + .addProjectUuidPermissions(UserRole.ADMIN, project.uuid()); + + assertJson(execute(project.key())).isSimilarTo("{\"configuration\": {\"canApplyPermissionTemplate\": false}}"); + } + private void init(Page... pages) { PluginRepository pluginRepository = mock(PluginRepository.class); when(pluginRepository.hasPlugin(anyString())).thenReturn(true); -- 2.39.5