]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4464 Fixed loaded template issue when default groups have been removed
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Mon, 22 Jul 2013 15:11:34 +0000 (17:11 +0200)
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Mon, 22 Jul 2013 15:11:34 +0000 (17:11 +0200)
sonar-server/src/main/java/org/sonar/server/startup/RegisterPermissionTemplates.java
sonar-server/src/test/java/org/sonar/server/startup/RegisterPermissionTemplatesTest.java

index 93b0b2d29b3a7391a9cb0311b2e8161db649b51a..5e85f988f0d74f2b8a308fe17cb9911fc79499ad 100644 (file)
@@ -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;
   }
index c192075eec65f1bebe1929c35ae5e59bf77f3677..f8c147b1544c402e4ef40703ab95fae00ae077dd 100644 (file)
@@ -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<LoadedTemplateDto> {
 
     private final LoadedTemplateDto referenceTemplate;