From b7ea5c0ccdedbd70bf741f43091f6509a999061d Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Vilain Date: Mon, 22 Jul 2013 17:11:34 +0200 Subject: [PATCH] SONAR-4464 Fixed loaded template issue when default groups have been removed --- .../startup/RegisterPermissionTemplates.java | 14 ++++++++++++-- .../startup/RegisterPermissionTemplatesTest.java | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterPermissionTemplates.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterPermissionTemplates.java index 93b0b2d29b3..5e85f988f0d 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterPermissionTemplates.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterPermissionTemplates.java @@ -36,6 +36,7 @@ import org.sonar.server.platform.PersistentSettings; public class RegisterPermissionTemplates { public static final String DEFAULT_TEMPLATE_PROPERTY = "sonar.permission.template.default"; + public static final String DEFAULT_PROJECTS_TEMPLATE_PROPERTY = "sonar.permission.template.TRK.default"; private static final Logger LOG = LoggerFactory.getLogger(RegisterPermissionTemplates.class); @@ -56,13 +57,22 @@ public class RegisterPermissionTemplates { TimeProfiler profiler = new TimeProfiler(LOG).start("Register permission templates"); if(shouldRegister()) { - insertDefaultTemplate(PermissionTemplateDto.DEFAULT.getName()); + if(hasExistingPermissionsConfig()) { + String projectsPermissionsKey = settings.getString(DEFAULT_PROJECTS_TEMPLATE_PROPERTY); + setDefaultProperty(projectsPermissionsKey); + } else { + insertDefaultTemplate(PermissionTemplateDto.DEFAULT.getName()); + setDefaultProperty(PermissionTemplateDto.DEFAULT.getKee()); + } registerInitialization(); - setDefaultProperty(PermissionTemplateDto.DEFAULT.getKee()); } profiler.stop(); } + private boolean hasExistingPermissionsConfig() { + return settings.getString(DEFAULT_PROJECTS_TEMPLATE_PROPERTY) != null; + } + private boolean shouldRegister() { return loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.PERMISSION_TEMPLATE_TYPE, PermissionTemplateDto.DEFAULT.getKee()) == 0; } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/RegisterPermissionTemplatesTest.java b/sonar-server/src/test/java/org/sonar/server/startup/RegisterPermissionTemplatesTest.java index c192075eec6..f8c147b1544 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/RegisterPermissionTemplatesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/RegisterPermissionTemplatesTest.java @@ -90,6 +90,21 @@ public class RegisterPermissionTemplatesTest { verify(loadedTemplateDao, never()).insert(any(LoadedTemplateDto.class)); } + @Test + public void should_reference_TRK_template_as_default_when_present() throws Exception { + when(settings.getString(RegisterPermissionTemplates.DEFAULT_PROJECTS_TEMPLATE_PROPERTY)).thenReturn("my_projects_template"); + + LoadedTemplateDto expectedTemplate = new LoadedTemplateDto().setKey(PermissionTemplateDto.DEFAULT.getKee()) + .setType(LoadedTemplateDto.PERMISSION_TEMPLATE_TYPE); + + RegisterPermissionTemplates initializer = new RegisterPermissionTemplates(loadedTemplateDao, permissionDao, userDao, settings); + initializer.start(); + + verify(loadedTemplateDao).insert(argThat(Matches.template(expectedTemplate))); + verify(settings).saveProperty(RegisterPermissionTemplates.DEFAULT_TEMPLATE_PROPERTY, "my_projects_template"); + verifyZeroInteractions(permissionDao); + } + private static class Matches extends BaseMatcher { private final LoadedTemplateDto referenceTemplate; -- 2.39.5