From a60604f5d250121fd594bb3d331a0f85fe889797 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Vilain Date: Thu, 4 Jul 2013 17:47:28 +0200 Subject: [PATCH] SONAR-4463 Renamed permission column and added DAO methods --- .../java/org/sonar/core/user/Permission.java | 3 ++ .../org/sonar/core/user/PermissionDao.java | 23 ++++++++++++++ .../core/user/PermissionTemplateMapper.java | 8 +++++ .../org/sonar/core/persistence/schema-h2.ddl | 4 +-- .../core/user/PermissionTemplateMapper.xml | 31 ++++++++++++++++--- .../sonar/core/user/PermissionDaoTest.java | 23 ++++++++++++++ .../addGroupPermissionToTemplate-result.xml | 2 +- ...ddNullGroupPermissionToTemplate-result.xml | 2 +- .../addUserPermissionToTemplate-result.xml | 2 +- .../deletePermissionTemplate-result.xml | 7 +++++ .../deletePermissionTemplate.xml | 12 +++++++ ...moveGroupPermissionFromTemplate-result.xml | 2 +- .../removeGroupPermissionFromTemplate.xml | 4 +-- ...NullGroupPermissionFromTemplate-result.xml | 2 +- .../removeNullGroupPermissionFromTemplate.xml | 4 +-- ...emoveUserPermissionFromTemplate-result.xml | 2 +- .../removeUserPermissionFromTemplate.xml | 4 +-- .../selectPermissionTemplate.xml | 12 +++---- .../416_create_permission_templates_users.rb | 2 +- .../417_create_permission_templates_groups.rb | 2 +- 20 files changed, 124 insertions(+), 27 deletions(-) create mode 100644 sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/deletePermissionTemplate-result.xml create mode 100644 sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/deletePermissionTemplate.xml diff --git a/sonar-core/src/main/java/org/sonar/core/user/Permission.java b/sonar-core/src/main/java/org/sonar/core/user/Permission.java index be5fb50a755..98bb97723a8 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/Permission.java +++ b/sonar-core/src/main/java/org/sonar/core/user/Permission.java @@ -61,4 +61,7 @@ public class Permission { return allGlobal; } + public static boolean isValid(String permission) { + return allGlobal.containsKey(permission); + } } 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 2e70595ec14..8e9bbf5d2de 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 @@ -37,6 +37,16 @@ public class PermissionDao implements TaskExtension, ServerExtension { this.myBatis = myBatis; } + public PermissionTemplateDto selectTemplateByName(String templateName) { + SqlSession session = myBatis.openSession(); + try { + PermissionTemplateMapper mapper = session.getMapper(PermissionTemplateMapper.class); + return mapper.selectByName(templateName); + } finally { + MyBatis.closeQuietly(session); + } + } + public PermissionTemplateDto selectPermissionTemplate(String templateName) { PermissionTemplateDto permissionTemplate = null; SqlSession session = myBatis.openSession(); @@ -78,6 +88,19 @@ public class PermissionDao implements TaskExtension, ServerExtension { return permissionTemplate; } + public void deletePermissionTemplate(Long templateId) { + SqlSession session = myBatis.openSession(); + try { + PermissionTemplateMapper mapper = session.getMapper(PermissionTemplateMapper.class); + mapper.deleteUsersPermissions(templateId); + mapper.deleteGroupsPermissions(templateId); + mapper.delete(templateId); + session.commit(); + } finally { + MyBatis.closeQuietly(session); + } + } + public void addUserPermission(Long templateId, Long userId, String permission) { PermissionTemplateUserDto permissionTemplateUser = new PermissionTemplateUserDto() .setTemplateId(templateId) diff --git a/sonar-core/src/main/java/org/sonar/core/user/PermissionTemplateMapper.java b/sonar-core/src/main/java/org/sonar/core/user/PermissionTemplateMapper.java index cc0f392e5d2..f3f3f85f884 100644 --- a/sonar-core/src/main/java/org/sonar/core/user/PermissionTemplateMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/user/PermissionTemplateMapper.java @@ -27,6 +27,14 @@ public interface PermissionTemplateMapper { void insert(PermissionTemplateDto permissionTemplate); + void delete(Long templateId); + + void deleteUsersPermissions(Long templateId); + + void deleteGroupsPermissions(Long templateId); + + PermissionTemplateDto selectByName(String templateName); + PermissionTemplateDto selectTemplateUsersPermissions(String templateName); PermissionTemplateDto selectTemplateGroupsPermissions(String templateName); diff --git a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl index 6bf00be3dbb..958224d5b56 100644 --- a/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl +++ b/sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl @@ -544,7 +544,7 @@ CREATE TABLE "PERM_TEMPLATES_USERS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "USER_ID" INTEGER NOT NULL, "TEMPLATE_ID" INTEGER NOT NULL, - "PERMISSION" VARCHAR(64) NOT NULL, + "PERMISSION_REFERENCE" VARCHAR(64) NOT NULL, "CREATED_AT" TIMESTAMP, "UPDATED_AT" TIMESTAMP ); @@ -553,7 +553,7 @@ CREATE TABLE "PERM_TEMPLATES_GROUPS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "GROUP_ID" INTEGER, "TEMPLATE_ID" INTEGER NOT NULL, - "PERMISSION" VARCHAR(64) NOT NULL, + "PERMISSION_REFERENCE" VARCHAR(64) NOT NULL, "CREATED_AT" TIMESTAMP, "UPDATED_AT" TIMESTAMP ); diff --git a/sonar-core/src/main/resources/org/sonar/core/user/PermissionTemplateMapper.xml b/sonar-core/src/main/resources/org/sonar/core/user/PermissionTemplateMapper.xml index ee260ea609b..e01b01f683c 100644 --- a/sonar-core/src/main/resources/org/sonar/core/user/PermissionTemplateMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/user/PermissionTemplateMapper.xml @@ -8,8 +8,23 @@ VALUES (#{name}, #{description}, #{createdAt}, #{updatedAt}) + + DELETE FROM permission_templates + WHERE id = #{templateId} + + + + DELETE FROM perm_templates_users + WHERE template_id = #{templateId} + + + + DELETE FROM perm_templates_groups + WHERE template_id = #{templateId} + + - INSERT INTO perm_templates_users (template_id, user_id, permission, created_at, updated_at) + INSERT INTO perm_templates_users (template_id, user_id, permission_reference, created_at, updated_at) VALUES (#{templateId}, #{userId}, #{permission}, #{createdAt}, #{updatedAt}) @@ -20,7 +35,7 @@ - INSERT INTO perm_templates_groups (template_id, group_id, permission, created_at, updated_at) + INSERT INTO perm_templates_groups (template_id, group_id, permission_reference, created_at, updated_at) VALUES (#{templateId}, #{groupId}, #{permission}, #{createdAt}, #{updatedAt}) @@ -38,6 +53,12 @@ + + - + diff --git a/sonar-core/src/test/java/org/sonar/core/user/PermissionDaoTest.java b/sonar-core/src/test/java/org/sonar/core/user/PermissionDaoTest.java index 205c9365cfb..01e0c6e4d4c 100644 --- a/sonar-core/src/test/java/org/sonar/core/user/PermissionDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/user/PermissionDaoTest.java @@ -62,6 +62,18 @@ public class PermissionDaoTest extends AbstractDaoTestCase { assertThat(permissionTemplate.getGroupsPermissions()).onProperty("permission").containsOnly("group_permission1", "group_permission1", "group_permission2"); } + @Test + public void should_permission_template_by_name() throws Exception { + setupData("selectPermissionTemplate"); + + PermissionTemplateDto permissionTemplate = permissionDao.selectTemplateByName("my template"); + + assertThat(permissionTemplate).isNotNull(); + assertThat(permissionTemplate.getId()).isEqualTo(1L); + assertThat(permissionTemplate.getName()).isEqualTo("my template"); + assertThat(permissionTemplate.getDescription()).isEqualTo("my description"); + } + @Test public void should_select_all_permission_templates() throws Exception { setupData("selectAllPermissionTemplates"); @@ -74,6 +86,17 @@ public class PermissionDaoTest extends AbstractDaoTestCase { assertThat(permissionTemplates).onProperty("description").containsOnly("description1", "description2", "description3"); } + @Test + public void should_delete_permission_template() throws Exception { + setupData("deletePermissionTemplate"); + + permissionDao.deletePermissionTemplate(1L); + + checkTable("deletePermissionTemplate", "permission_templates", "id", "name", "description"); + checkTable("deletePermissionTemplate", "perm_templates_users", "id", "template_id", "user_id", "permission"); + checkTable("deletePermissionTemplate", "perm_templates_groups", "id", "template_id", "group_id", "permission"); + } + @Test public void should_add_user_permission_to_template() throws Exception { setupData("addUserPermissionToTemplate"); diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addGroupPermissionToTemplate-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addGroupPermissionToTemplate-result.xml index b7b0225d345..ae1db0b574e 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addGroupPermissionToTemplate-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addGroupPermissionToTemplate-result.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addNullGroupPermissionToTemplate-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addNullGroupPermissionToTemplate-result.xml index fda5cefa69b..4fe3b439832 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addNullGroupPermissionToTemplate-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addNullGroupPermissionToTemplate-result.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addUserPermissionToTemplate-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addUserPermissionToTemplate-result.xml index 3f243536113..8676d568d5e 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addUserPermissionToTemplate-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addUserPermissionToTemplate-result.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/deletePermissionTemplate-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/deletePermissionTemplate-result.xml new file mode 100644 index 00000000000..df9b962986d --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/deletePermissionTemplate-result.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/deletePermissionTemplate.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/deletePermissionTemplate.xml new file mode 100644 index 00000000000..61c40436844 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/deletePermissionTemplate.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate-result.xml index 1d43ca22fea..d2b04dcce89 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate-result.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate.xml index 06e5322a5d3..2cb0ab460fd 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate.xml @@ -1,6 +1,6 @@ - - + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate-result.xml index 1d43ca22fea..d2b04dcce89 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate-result.xml @@ -1,5 +1,5 @@ - + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate.xml index 095449a77b6..23bd158f3a1 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate.xml @@ -1,6 +1,6 @@ - - + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate-result.xml index 68bf04077f4..f4c500ba1ff 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate-result.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate-result.xml @@ -1,7 +1,7 @@ - + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate.xml index 69427d2d3bf..219c1f07286 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate.xml @@ -1,8 +1,8 @@ - - + + \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/selectPermissionTemplate.xml b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/selectPermissionTemplate.xml index 0de01c53486..91b6e9744eb 100644 --- a/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/selectPermissionTemplate.xml +++ b/sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/selectPermissionTemplate.xml @@ -1,16 +1,16 @@ - - - + + + - - - + + + diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/416_create_permission_templates_users.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/416_create_permission_templates_users.rb index 13581fad766..10a70d79299 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/416_create_permission_templates_users.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/416_create_permission_templates_users.rb @@ -27,7 +27,7 @@ class CreatePermissionTemplatesUsers < ActiveRecord::Migration create_table :perm_templates_users do |t| t.column :user_id, :integer, :null => false t.column :template_id, :integer, :null => false - t.column :permission, :string, :null => false, :limit => 64 + t.column :permission_reference, :string, :null => false, :limit => 64 t.column :created_at, :datetime, :null => true t.column :updated_at, :datetime, :null => true end diff --git a/sonar-server/src/main/webapp/WEB-INF/db/migrate/417_create_permission_templates_groups.rb b/sonar-server/src/main/webapp/WEB-INF/db/migrate/417_create_permission_templates_groups.rb index b88ceabfc62..b9ccf96f711 100644 --- a/sonar-server/src/main/webapp/WEB-INF/db/migrate/417_create_permission_templates_groups.rb +++ b/sonar-server/src/main/webapp/WEB-INF/db/migrate/417_create_permission_templates_groups.rb @@ -27,7 +27,7 @@ class CreatePermissionTemplatesGroups < ActiveRecord::Migration create_table :perm_templates_groups do |t| t.column :group_id, :integer, :null => true t.column :template_id, :integer, :null => false - t.column :permission, :string, :null => false, :limit => 64 + t.column :permission_reference, :string, :null => false, :limit => 64 t.column :created_at, :datetime, :null => true t.column :updated_at, :datetime, :null => true end -- 2.39.5