From 84c173736bd43b7b7aa546e6b84d2d52089c68c1 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Vilain Date: Fri, 5 Jul 2013 11:22:15 +0200 Subject: [PATCH] SONAR-4463 Added binding in internal rb class for new permission template operations --- .../org/sonar/core/user/PermissionDao.java | 1 + .../InternalPermissionTemplateService.java | 31 +++++++++++++------ .../org/sonar/server/platform/Platform.java | 2 ++ .../webapp/WEB-INF/app/models/internal.rb | 4 +++ ...InternalPermissionTemplateServiceTest.java | 16 ++++++++++ 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/sonar-core/src/main/java/org/sonar/core/user/PermissionDao.java b/sonar-core/src/main/java/org/sonar/core/user/PermissionDao.java index ed8967b2959..9f99630b65f 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/PermissionDao.java +++ b/sonar-core/src/main/java/org/sonar/core/user/PermissionDao.java @@ -65,6 +65,7 @@ public class PermissionDao implements TaskExtension, ServerExtension { return permissionTemplate; } + @CheckForNull public List selectAllPermissionTemplates() { SqlSession session = myBatis.openSession(); try { diff --git a/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java b/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java index e996b13403c..7be63ada0cf 100644 --- a/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java +++ b/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java @@ -20,6 +20,7 @@ package org.sonar.server.permission; +import com.google.common.collect.Lists; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.ServerComponent; @@ -45,11 +46,30 @@ public class InternalPermissionTemplateService implements ServerComponent { this.userDao = userDao; } + @CheckForNull + public PermissionTemplate selectPermissionTemplate(String templateName) { + PermissionTemplateUpdater.checkUserCredentials(); + PermissionTemplateDto permissionTemplateDto = permissionDao.selectPermissionTemplate(templateName); + return PermissionTemplate.create(permissionTemplateDto); + } + + public List selectAllPermissionTemplates() { + PermissionTemplateUpdater.checkUserCredentials(); + List permissionTemplates = Lists.newArrayList(); + List permissionTemplateDtos = permissionDao.selectAllPermissionTemplates(); + if(permissionTemplateDtos != null) { + for (PermissionTemplateDto permissionTemplateDto : permissionTemplateDtos) { + permissionTemplates.add(PermissionTemplate.create(permissionTemplateDto)); + } + } + return permissionTemplates; + } + public PermissionTemplate createPermissionTemplate(String name, String description) { PermissionTemplateUpdater.checkUserCredentials(); - PermissionTemplateDto permissionTemplateDto = permissionDao.createPermissionTemplate(name, description); checkThatTemplateNameIsUnique(name); - if(permissionTemplateDto == null) { + PermissionTemplateDto permissionTemplateDto = permissionDao.createPermissionTemplate(name, description); + if(permissionTemplateDto.getId() == null) { String errorMsg = "Template creation failed"; LOG.error(errorMsg); throw new ServerErrorException(errorMsg); @@ -57,13 +77,6 @@ public class InternalPermissionTemplateService implements ServerComponent { return PermissionTemplate.create(permissionTemplateDto); } - @CheckForNull - public PermissionTemplate selectPermissionTemplate(String templateName) { - PermissionTemplateUpdater.checkUserCredentials(); - PermissionTemplateDto permissionTemplateDto = permissionDao.selectPermissionTemplate(templateName); - return PermissionTemplate.create(permissionTemplateDto); - } - public void deletePermissionTemplate(String templateName) { PermissionTemplateUpdater.checkUserCredentials(); PermissionTemplateDto permissionTemplateDto = permissionDao.selectTemplateByName(templateName); diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 2d7014b3728..be04a975fb3 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -78,6 +78,7 @@ import org.sonar.server.issue.*; import org.sonar.server.notifications.NotificationCenter; import org.sonar.server.notifications.NotificationService; import org.sonar.server.permission.InternalPermissionService; +import org.sonar.server.permission.InternalPermissionTemplateService; import org.sonar.server.plugins.*; import org.sonar.server.qualitymodel.DefaultModelManager; import org.sonar.server.rule.RubyRuleService; @@ -257,6 +258,7 @@ public final class Platform { servicesContainer.addSingleton(DefaultUserFinder.class); servicesContainer.addSingleton(DefaultUserService.class); servicesContainer.addSingleton(InternalPermissionService.class); + servicesContainer.addSingleton(InternalPermissionTemplateService.class); // components servicesContainer.addSingleton(DefaultRubyComponentService.class); diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb index 912991a77fc..ded7ff05eca 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/internal.rb @@ -50,6 +50,10 @@ class Internal component(Java::OrgSonarServerPermission::InternalPermissionService.java_class) end + def self.permission_templates + component(Java::OrgSonarServierPermission::InternalPermissionTemplateService.java_class) + end + private def self.component(component_java_class) Java::OrgSonarServerPlatform::Platform.component(component_java_class) diff --git a/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionTemplateServiceTest.java b/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionTemplateServiceTest.java index 7abe57e7496..d7e9bf89028 100644 --- a/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionTemplateServiceTest.java +++ b/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionTemplateServiceTest.java @@ -132,6 +132,22 @@ public class InternalPermissionTemplateServiceTest { assertThat(permissionTemplate.getGroupsForPermission(Permission.SYSTEM_ADMIN.key())).containsOnly("admin_group"); } + @Test + public void should_retrieve_all_permission_templates() throws Exception { + PermissionTemplateDto template1 = + new PermissionTemplateDto().setId(1L).setName("template1").setDescription("template1"); + PermissionTemplateDto template2 = + new PermissionTemplateDto().setId(2L).setName("template2").setDescription("template2"); + when(permissionDao.selectAllPermissionTemplates()).thenReturn(Lists.newArrayList(template1, template2)); + + List templates = permissionTemplateService.selectAllPermissionTemplates(); + + assertThat(templates).hasSize(2); + assertThat(templates).onProperty("id").containsOnly(1L, 2L); + assertThat(templates).onProperty("name").containsOnly("template1", "template2"); + assertThat(templates).onProperty("description").containsOnly("template1", "template2"); + } + @Test public void should_add_user_permission() throws Exception { UserDto userDto = new UserDto().setId(1L).setLogin("user").setName("user"); -- 2.39.5