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);
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;
}
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;