From 8b20d9b681313b7b6b9bf0071e9169995d434912 Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Thu, 11 May 2017 16:14:09 +0200 Subject: [PATCH] SONAR-9194 Add canAdmin flag to WS api/navigation/global --- .../java/org/sonar/server/ui/ws/GlobalAction.java | 10 +++++++++- .../org/sonar/server/ui/ws/global-example.json | 3 ++- .../org/sonar/server/ui/ws/GlobalActionTest.java | 12 ++++++++++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/GlobalAction.java b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/GlobalAction.java index a5a9bc5a71b..ac787e96528 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ui/ws/GlobalAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ui/ws/GlobalAction.java @@ -36,6 +36,7 @@ import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.organization.OrganizationFlags; import org.sonar.server.ui.PageRepository; import org.sonar.server.ui.VersionFormatter; +import org.sonar.server.user.UserSession; import static org.sonar.api.CoreProperties.RATING_GRID; import static org.sonar.core.config.WebConstants.SONARQUBE_DOT_COM_ENABLED; @@ -63,9 +64,10 @@ public class GlobalAction implements NavigationWsAction { private final DbClient dbClient; private final OrganizationFlags organizationFlags; private final DefaultOrganizationProvider defaultOrganizationProvider; + private final UserSession userSession; public GlobalAction(PageRepository pageRepository, Settings settings, ResourceTypes resourceTypes, Server server, - DbClient dbClient, OrganizationFlags organizationFlags, DefaultOrganizationProvider defaultOrganizationProvider) { + DbClient dbClient, OrganizationFlags organizationFlags, DefaultOrganizationProvider defaultOrganizationProvider, UserSession userSession) { this.pageRepository = pageRepository; this.settings = settings; this.resourceTypes = resourceTypes; @@ -73,6 +75,7 @@ public class GlobalAction implements NavigationWsAction { this.dbClient = dbClient; this.organizationFlags = organizationFlags; this.defaultOrganizationProvider = defaultOrganizationProvider; + this.userSession = userSession; } @Override @@ -88,6 +91,7 @@ public class GlobalAction implements NavigationWsAction { @Override public void handle(Request request, Response response) throws Exception { JsonWriter json = response.newJsonWriter().beginObject(); + writeActions(json); writePages(json); writeSettings(json); writeDeprecatedLogoProperties(json); @@ -98,6 +102,10 @@ public class GlobalAction implements NavigationWsAction { json.endObject().close(); } + private void writeActions(JsonWriter json) { + json.prop("canAdmin", userSession.isSystemAdministrator()); + } + private void writePages(JsonWriter json) { json.name("globalPages").beginArray(); for (org.sonar.api.web.page.Page page : pageRepository.getGlobalPages(false)) { diff --git a/server/sonar-server/src/main/resources/org/sonar/server/ui/ws/global-example.json b/server/sonar-server/src/main/resources/org/sonar/server/ui/ws/global-example.json index 6af08ab2f98..cddaab6ff31 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/ui/ws/global-example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/ui/ws/global-example.json @@ -26,5 +26,6 @@ "version": "6.2", "productionDatabase": true, "organizationsEnabled": false, - "defaultOrganization": "key_foo" + "defaultOrganization": "key_foo", + "canAdmin": false } diff --git a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java index 1a651fec00c..85e45dab932 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ui/ws/GlobalActionTest.java @@ -50,7 +50,7 @@ import static org.sonar.test.JsonAssert.assertJson; public class GlobalActionTest { @Rule - public UserSessionRule userSessionRule = UserSessionRule.standalone(); + public UserSessionRule userSession = UserSessionRule.standalone(); private Settings settings = new MapSettings(); @@ -162,6 +162,14 @@ public class GlobalActionTest { executeAndVerify("organization_support.json"); } + @Test + public void can_admin_on_global_level() { + init(); + userSession.logIn().setRoot(); + + assertJson(call()).isSimilarTo("{\"canAdmin\":true}"); + } + @Test public void test_example_response() throws Exception { init(createPages(), new ResourceTypeTree[] { @@ -205,7 +213,7 @@ public class GlobalActionTest { }}); pageRepository.start(); ws = new WsActionTester(new GlobalAction(pageRepository, settings, new ResourceTypes(resourceTypeTrees), server, - dbClient, organizationFlags, defaultOrganizationProvider)); + dbClient, organizationFlags, defaultOrganizationProvider, userSession)); } private void executeAndVerify(String json) { -- 2.39.5