From 52cdc38ac06e8c40858e26d97b53d02b8f2b5926 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Wed, 5 Sep 2012 16:31:46 +0200 Subject: [PATCH] SONAR-3773 When migrating to Sonar 3.2, the default roles on Views are not filled and remain empty --- .../startup/SetDefaultProjectPermissions.java | 37 ++++++++++++------- .../SetDefaultProjectPermissionsTest.java | 7 +++- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/startup/SetDefaultProjectPermissions.java b/sonar-server/src/main/java/org/sonar/server/startup/SetDefaultProjectPermissions.java index 9aa5702ea21..dc4d24d4b8f 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/SetDefaultProjectPermissions.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/SetDefaultProjectPermissions.java @@ -20,6 +20,7 @@ package org.sonar.server.startup; import com.google.common.collect.Maps; +import org.apache.commons.lang.StringUtils; import org.slf4j.LoggerFactory; import org.sonar.server.platform.PersistentSettings; @@ -39,22 +40,30 @@ public class SetDefaultProjectPermissions { } public void start() { - if (persistentSettings.getSettings().getKeysStartingWith("sonar.role.").isEmpty()) { - LoggerFactory.getLogger(SetDefaultProjectPermissions.class).info("Setting default project permissions"); - Map props = Maps.newHashMap(); - props.put("sonar.role.admin.TRK.defaultGroups", SONAR_ADMINISTRATORS); - props.put("sonar.role.user.TRK.defaultGroups", ANYONE_AND_USERS); - props.put("sonar.role.codeviewer.TRK.defaultGroups", ANYONE_AND_USERS); - - // Support old versions of Views plugin - props.put("sonar.role.admin.VW.defaultGroups", SONAR_ADMINISTRATORS); - props.put("sonar.role.user.VW.defaultGroups", ANYONE_AND_USERS); - props.put("sonar.role.codeviewer.VW.defaultGroups", ANYONE_AND_USERS); - props.put("sonar.role.admin.SVW.defaultGroups", SONAR_ADMINISTRATORS); - props.put("sonar.role.user.SVW.defaultGroups", ANYONE_AND_USERS); - props.put("sonar.role.codeviewer.SVW.defaultGroups", ANYONE_AND_USERS); + Map props = Maps.newHashMap(); + props.putAll(missingProperties("TRK")); + // Support old versions of Views plugin + props.putAll(missingProperties("VW")); + props.putAll(missingProperties("SVW")); + + if (!props.isEmpty()) { + LoggerFactory.getLogger(SetDefaultProjectPermissions.class).info("Set default project roles"); persistentSettings.saveProperties(props); } } + + private Map missingProperties(String qualifier) { + Map props = Maps.newHashMap(); + if (StringUtils.isBlank(persistentSettings.getSettings().getString("sonar.role.user." + qualifier + ".defaultGroups"))) { + completeDefaultRoles(qualifier, props); + } + return props; + } + + private void completeDefaultRoles(String qualifier, Map props) { + props.put("sonar.role.admin." + qualifier + ".defaultGroups", SONAR_ADMINISTRATORS); + props.put("sonar.role.user." + qualifier + ".defaultGroups", ANYONE_AND_USERS); + props.put("sonar.role.codeviewer." + qualifier + ".defaultGroups", ANYONE_AND_USERS); + } } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java b/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java index 3b2cba14604..3139bb6b192 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/SetDefaultProjectPermissionsTest.java @@ -46,7 +46,7 @@ public class SetDefaultProjectPermissionsTest { @Override public boolean matches(Object o) { Map map = (Map) o; - return map.size() == 9 && map.get("sonar.role.admin.TRK.defaultGroups").equals("sonar-administrators"); + return map.size()>1 && map.get("sonar.role.admin.TRK.defaultGroups").equals("sonar-administrators"); } })); } @@ -54,7 +54,10 @@ public class SetDefaultProjectPermissionsTest { @Test public void do_not_set_default_permissions_if_exist() { PersistentSettings persistentSettings = mock(PersistentSettings.class); - Settings settings = new Settings().setProperty("sonar.role.admin.TRK.defaultGroups", "custom-group"); + Settings settings = new Settings() + .setProperty("sonar.role.user.TRK.defaultGroups", "custom-group") + .setProperty("sonar.role.user.VW.defaultGroups", "custom-group") + .setProperty("sonar.role.user.SVW.defaultGroups", "custom-group"); when(persistentSettings.getSettings()).thenReturn(settings); new SetDefaultProjectPermissions(persistentSettings).start(); -- 2.39.5