diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-07-22 18:51:50 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-07-23 08:35:05 +0200 |
commit | bdd27251c8f15e7f59cd8701dc53efd57cb4f9f4 (patch) | |
tree | c248d7ebb85279472f4df121eb0833b10f55e3a2 /server | |
parent | e7e4d9f3fea348a80388f7cfd9fedfb48b9c799c (diff) | |
download | sonarqube-bdd27251c8f15e7f59cd8701dc53efd57cb4f9f4.tar.gz sonarqube-bdd27251c8f15e7f59cd8701dc53efd57cb4f9f4.zip |
SONAR-5477 Return global settings in /batch/global WS
Diffstat (limited to 'server')
6 files changed, 116 insertions, 19 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/batch/GlobalReferentialsAction.java b/server/sonar-server/src/main/java/org/sonar/server/batch/GlobalReferentialsAction.java index 1e8ad12599c..f733b8b9566 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/batch/GlobalReferentialsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/batch/GlobalReferentialsAction.java @@ -21,23 +21,30 @@ package org.sonar.server.batch; import org.apache.commons.io.IOUtils; +import org.sonar.api.config.Settings; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.RequestHandler; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.batch.protocol.input.GlobalReferentials; import org.sonar.core.measure.db.MetricDto; +import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; import org.sonar.server.db.DbClient; import org.sonar.server.plugins.MimeTypes; +import org.sonar.server.user.UserSession; + +import java.util.Map; public class GlobalReferentialsAction implements RequestHandler { private final DbClient dbClient; + private final Settings settings; - public GlobalReferentialsAction(DbClient dbClient) { + public GlobalReferentialsAction(DbClient dbClient, Settings settings) { this.dbClient = dbClient; + this.settings = settings; } void define(WebService.NewController controller) { @@ -50,25 +57,15 @@ public class GlobalReferentialsAction implements RequestHandler { @Override public void handle(Request request, Response response) throws Exception { - // TODO check user permission + UserSession userSession = UserSession.get(); + boolean hasScanPerm = userSession.hasGlobalPermission(GlobalPermissions.SCAN_EXECUTION); + boolean hasDryRunPerm = userSession.hasGlobalPermission(GlobalPermissions.DRY_RUN_EXECUTION); DbSession session = dbClient.openSession(false); try { GlobalReferentials ref = new GlobalReferentials(); - for (MetricDto metric : dbClient.metricDao().findEnabled(session)) { - Boolean optimizedBestValue = metric.isOptimizedBestValue(); - ref.metrics().add( - new org.sonar.batch.protocol.input.Metric(metric.getId(), metric.getKey(), - metric.getValueType(), - metric.getDescription(), - metric.getDirection(), - metric.getName(), - metric.isQualitative(), - metric.isUserManaged(), - metric.getWorstValue(), - metric.getBestValue(), - optimizedBestValue != null ? optimizedBestValue : false)); - } + addMetrics(ref, session); + addSettings(ref, hasScanPerm, hasDryRunPerm); response.stream().setMediaType(MimeTypes.JSON); IOUtils.write(ref.toJson(), response.stream().output()); @@ -77,4 +74,36 @@ public class GlobalReferentialsAction implements RequestHandler { } } + private void addMetrics(GlobalReferentials ref, DbSession session) { + for (MetricDto metric : dbClient.metricDao().findEnabled(session)) { + Boolean optimizedBestValue = metric.isOptimizedBestValue(); + ref.addMetric( + new org.sonar.batch.protocol.input.Metric(metric.getId(), metric.getKey(), + metric.getValueType(), + metric.getDescription(), + metric.getDirection(), + metric.getName(), + metric.isQualitative(), + metric.isUserManaged(), + metric.getWorstValue(), + metric.getBestValue(), + optimizedBestValue != null ? optimizedBestValue : false)); + } + } + + private void addSettings(GlobalReferentials ref, boolean hasScanPerm, boolean hasDryRunPerm) { + for (Map.Entry<String, String> entry : settings.getProperties().entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + + if (isPropertyAllowed(key, hasScanPerm, hasDryRunPerm)) { + ref.addGlobalSetting(key, value); + } + } + } + + private boolean isPropertyAllowed(String key, boolean hasScanPerm, boolean hasDryRunPerm){ + return !key.contains(".secured") || hasScanPerm || (key.contains(".license") && hasDryRunPerm); + } + } diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/BatchWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/BatchWsTest.java index 689736c314b..35a48d01451 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/batch/BatchWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/batch/BatchWsTest.java @@ -28,6 +28,7 @@ import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import org.sonar.api.config.Settings; import org.sonar.server.db.DbClient; import org.sonar.server.ws.WsTester; @@ -54,7 +55,7 @@ public class BatchWsTest { @Before public void before() throws IOException { - tester = new WsTester(new BatchWs(batchIndex, new GlobalReferentialsAction(mock(DbClient.class)))); + tester = new WsTester(new BatchWs(batchIndex, new GlobalReferentialsAction(mock(DbClient.class), mock(Settings.class)))); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/GlobalReferentialsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/GlobalReferentialsActionTest.java index 107a9f093ff..42b107eca07 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/batch/GlobalReferentialsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/batch/GlobalReferentialsActionTest.java @@ -25,10 +25,13 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; +import org.sonar.api.config.Settings; import org.sonar.core.measure.db.MetricDto; +import org.sonar.core.permission.GlobalPermissions; import org.sonar.core.persistence.DbSession; import org.sonar.server.db.DbClient; import org.sonar.server.measure.persistence.MetricDao; +import org.sonar.server.user.MockUserSession; import org.sonar.server.ws.WsTester; import static com.google.common.collect.Lists.newArrayList; @@ -44,6 +47,8 @@ public class GlobalReferentialsActionTest { @Mock MetricDao metricDao; + Settings settings; + WsTester tester; @Before @@ -52,11 +57,13 @@ public class GlobalReferentialsActionTest { when(dbClient.openSession(false)).thenReturn(session); when(dbClient.metricDao()).thenReturn(metricDao); - tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalReferentialsAction(dbClient))); + settings = new Settings(); + + tester = new WsTester(new BatchWs(mock(BatchIndex.class), new GlobalReferentialsAction(dbClient, settings))); } @Test - public void return_global_referentials() throws Exception { + public void return_metrics() throws Exception { when(metricDao.findEnabled(session)).thenReturn(newArrayList( MetricDto.createFor("coverage").setDescription("Coverage by unit tests").setValueType("PERCENT").setQualitative(true) .setWorstValue(0d).setBestValue(100d).setOptimizedBestValue(false).setDirection(1).setEnabled(true) @@ -65,4 +72,40 @@ public class GlobalReferentialsActionTest { WsTester.TestRequest request = tester.newGetRequest("batch", "global"); request.execute().assertJson(getClass(), "return_global_referentials.json"); } + + @Test + public void return_global_settings() throws Exception { + MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION, GlobalPermissions.DRY_RUN_EXECUTION); + + settings.setProperty("foo", "bar"); + settings.setProperty("foo.secured", "1234"); + settings.setProperty("foo.license.secured", "5678"); + + WsTester.TestRequest request = tester.newGetRequest("batch", "global"); + request.execute().assertJson(getClass(), "return_global_settings.json"); + } + + @Test + public void return_only_license_settings_without_scan_but_with_preview_permission() throws Exception { + MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.DRY_RUN_EXECUTION); + + settings.setProperty("foo", "bar"); + settings.setProperty("foo.secured", "1234"); + settings.setProperty("foo.license.secured", "5678"); + + WsTester.TestRequest request = tester.newGetRequest("batch", "global"); + request.execute().assertJson(getClass(), "return_only_license_settings_without_scan_but_with_preview_permission.json"); + } + + @Test + public void return_no_secured_settings_without_scan_and_preview_permission() throws Exception { + MockUserSession.set().setLogin("john").setGlobalPermissions(); + + settings.setProperty("foo", "bar"); + settings.setProperty("foo.secured", "1234"); + settings.setProperty("foo.license.secured", "5678"); + + WsTester.TestRequest request = tester.newGetRequest("batch", "global"); + request.execute().assertJson(getClass(), "return_no_secured_settings_without_scan_and_preview_permission.json"); + } } diff --git a/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_global_settings.json b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_global_settings.json new file mode 100644 index 00000000000..d6faa69622a --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_global_settings.json @@ -0,0 +1,9 @@ +{ + "timestamp": 0, + "metrics": [], + "globalSettings": { + "foo" : "bar", + "foo.secured" : "1234", + "foo.license.secured" : "5678" + } +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_no_secured_settings_without_scan_and_preview_permission.json b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_no_secured_settings_without_scan_and_preview_permission.json new file mode 100644 index 00000000000..6bc5b2da8aa --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_no_secured_settings_without_scan_and_preview_permission.json @@ -0,0 +1,7 @@ +{ + "timestamp": 0, + "metrics": [], + "globalSettings": { + "foo" : "bar" + } +} diff --git a/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_only_license_settings_without_scan_but_with_preview_permission.json b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_only_license_settings_without_scan_but_with_preview_permission.json new file mode 100644 index 00000000000..b0af2f8ca33 --- /dev/null +++ b/server/sonar-server/src/test/resources/org/sonar/server/batch/GlobalReferentialsActionTest/return_only_license_settings_without_scan_but_with_preview_permission.json @@ -0,0 +1,8 @@ +{ + "timestamp": 0, + "metrics": [], + "globalSettings": { + "foo" : "bar", + "foo.license.secured" : "5678" + } +} |