]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4463 Renamed permission column and added DAO methods
authorJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Thu, 4 Jul 2013 15:47:28 +0000 (17:47 +0200)
committerJean-Baptiste Vilain <jean-baptiste.vilain@sonarsource.com>
Thu, 4 Jul 2013 15:47:28 +0000 (17:47 +0200)
20 files changed:
sonar-core/src/main/java/org/sonar/core/user/Permission.java
sonar-core/src/main/java/org/sonar/core/user/PermissionDao.java
sonar-core/src/main/java/org/sonar/core/user/PermissionTemplateMapper.java
sonar-core/src/main/resources/org/sonar/core/persistence/schema-h2.ddl
sonar-core/src/main/resources/org/sonar/core/user/PermissionTemplateMapper.xml
sonar-core/src/test/java/org/sonar/core/user/PermissionDaoTest.java
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addGroupPermissionToTemplate-result.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addNullGroupPermissionToTemplate-result.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/addUserPermissionToTemplate-result.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/deletePermissionTemplate-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/deletePermissionTemplate.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate-result.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeGroupPermissionFromTemplate.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate-result.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeNullGroupPermissionFromTemplate.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate-result.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/removeUserPermissionFromTemplate.xml
sonar-core/src/test/resources/org/sonar/core/user/PermissionDaoTest/selectPermissionTemplate.xml
sonar-server/src/main/webapp/WEB-INF/db/migrate/416_create_permission_templates_users.rb
sonar-server/src/main/webapp/WEB-INF/db/migrate/417_create_permission_templates_groups.rb

index be5fb50a755f533a1cb50079d124b046e1aa6e27..98bb97723a8de01389d558e86d50bbecbde00946 100644 (file)
@@ -61,4 +61,7 @@ public class Permission {
     return allGlobal;
   }
 
+  public static boolean isValid(String permission) {
+    return allGlobal.containsKey(permission);
+  }
 }
index 2e70595ec14c306cf0aaf6eca421de62ef767a04..8e9bbf5d2ded2cef16a587c65f2aa246a87916ed 100644 (file)
@@ -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)
index cc0f392e5d22840f0765a2aeb80a72742cf2ecf4..f3f3f85f884fcc11432150091461040d252062b9 100644 (file)
@@ -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);
index 6bf00be3dbb5a90b16595cac50fa3841c4bfc4bc..958224d5b5662b108dbe6ad2a1296a36c24b75cf 100644 (file)
@@ -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
 );
index ee260ea609bf723eb818095fc79e76ccc168982b..e01b01f683c068177d0d847bb8da4432c8ced7b9 100644 (file)
@@ -8,8 +8,23 @@
     VALUES (#{name}, #{description}, #{createdAt}, #{updatedAt})
   </insert>
 
+  <delete id="delete" parameterType="Long">
+    DELETE FROM permission_templates
+    WHERE id = #{templateId}
+  </delete>
+
+  <delete id="deleteUsersPermissions" parameterType="Long">
+    DELETE FROM perm_templates_users
+    WHERE template_id = #{templateId}
+  </delete>
+
+  <delete id="deleteGroupsPermissions" parameterType="Long">
+    DELETE FROM perm_templates_groups
+    WHERE template_id = #{templateId}
+  </delete>
+
   <insert id="insertUserPermission" parameterType="PermissionTemplateUser">
-    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})
   </insert>
 
@@ -20,7 +35,7 @@
   </delete>
 
   <insert id="insertGroupPermission" parameterType="PermissionTemplateGroup">
-    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})
   </insert>
 
     </choose>
   </delete>
 
+  <select id="selectByName" parameterType="String" resultType="PermissionTemplate">
+    SELECT id, name, description, created_at AS createdAt, updated_at AS updatedAt
+    FROM permission_templates
+    WHERE name = #{templateName}
+  </select>
+
   <select id="selectAllPermissionTemplates" resultType="PermissionTemplate">
     SELECT id, name, description, created_at AS createdAt, updated_at AS updatedAt
     FROM permission_templates
@@ -50,7 +71,7 @@
             pt.created_at AS template_created_at,
             pt.updated_at AS template_updated_at,
             ptu.id AS permission_template_user_id,
-            ptu.permission AS user_permission,
+            ptu.permission_reference AS user_permission,
             u.name AS user_name
     FROM permission_templates AS pt
     INNER JOIN perm_templates_users AS ptu ON ptu.template_id = pt.id
@@ -65,7 +86,7 @@
             pt.created_at AS template_created_at,
             pt.updated_at AS template_updated_at,
             ptg.id AS permission_template_group_id,
-            ptg.permission AS group_permission,
+            ptg.permission_reference AS group_permission,
             g.name AS group_name
     FROM permission_templates AS pt
     INNER JOIN perm_templates_groups AS ptg ON ptg.template_id = pt.id
@@ -74,7 +95,7 @@
   </select>
 
   <resultMap id="fullPermissionsTemplateResult" type="PermissionTemplate">
-    <id property="id" column="template_id" />
+    <id property="id" column="template_id"/>
     <result property="name" column="template_name"/>
     <result property="description" column="template_description"/>
     <result property="createdAt" column="template_created_at"/>
index 205c9365cfb6207586dd5b8ff09e7090ea81f321..01e0c6e4d4c394066eb9e8ca295cea17a6d1dbf2 100644 (file)
@@ -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");
index b7b0225d34589552d00a50b53057e55cd3a60a59..ae1db0b574e4cde28b3995b19af9be3d347c8d47 100644 (file)
@@ -1,5 +1,5 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
   <perm_templates_users/>
-  <perm_templates_groups id="1" template_id="1" group_id="1" permission="new_permission"/>
+  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="new_permission"/>
 </dataset>
\ No newline at end of file
index fda5cefa69b3f74e6035af1a7b7a52d1c48297aa..4fe3b439832c114d5a84c5acbefc8a7b64dfef64 100644 (file)
@@ -1,5 +1,5 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
   <perm_templates_users/>
-  <perm_templates_groups id="1" template_id="1" group_id="[null]" permission="new_permission"/>
+  <perm_templates_groups id="1" template_id="1" group_id="[null]" permission_reference="new_permission"/>
 </dataset>
\ No newline at end of file
index 3f2435361132c1ce9d8be593a2a8b69d3e488dc8..8676d568d5ef798acbdbbaec8de8c9acf3a39fb4 100644 (file)
@@ -1,5 +1,5 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
-  <perm_templates_users id="1" template_id="1" user_id="1" permission="new_permission"/>
+  <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="new_permission"/>
   <perm_templates_groups/>
 </dataset>
\ 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 (file)
index 0000000..df9b962
--- /dev/null
@@ -0,0 +1,7 @@
+<dataset>
+  <permission_templates id="2" name="other template" description="other description" created_at="[null]" updated_at="[null]"/>
+
+  <perm_templates_users/>
+
+  <perm_templates_groups/>
+</dataset>
\ 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 (file)
index 0000000..61c4043
--- /dev/null
@@ -0,0 +1,12 @@
+<dataset>
+  <permission_templates id="1" name="my template" description="my description" created_at="[null]" updated_at="[null]"/>
+  <permission_templates id="2" name="other template" description="other description" created_at="[null]" updated_at="[null]"/>
+
+  <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="user_permission1"/>
+  <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="user_permission1"/>
+  <perm_templates_users id="3" template_id="1" user_id="2" permission_reference="user_permission2"/>
+
+  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="group_permission1"/>
+  <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="group_permission1"/>
+  <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="group_permission2"/>
+</dataset>
\ No newline at end of file
index 1d43ca22feab2a8a600c0e431a7bad09009c1ec1..d2b04dcce89bf32458a7d5a1e3b8b3dfe6283ca4 100644 (file)
@@ -1,5 +1,5 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
   <perm_templates_users/>
-  <perm_templates_groups id="1" template_id="1" group_id="1" permission="remaining_permission"/>
+  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
 </dataset>
\ No newline at end of file
index 06e5322a5d3cffe26769474d85300396a2ac7040..2cb0ab460fdf07197dd1accfb99b9b625f4a0223 100644 (file)
@@ -1,6 +1,6 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
   <perm_templates_users/>
-  <perm_templates_groups id="1" template_id="1" group_id="1" permission="remaining_permission"/>
-  <perm_templates_groups id="2" template_id="1" group_id="2" permission="existing_permission"/>
+  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
+  <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="existing_permission"/>
 </dataset>
\ No newline at end of file
index 1d43ca22feab2a8a600c0e431a7bad09009c1ec1..d2b04dcce89bf32458a7d5a1e3b8b3dfe6283ca4 100644 (file)
@@ -1,5 +1,5 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
   <perm_templates_users/>
-  <perm_templates_groups id="1" template_id="1" group_id="1" permission="remaining_permission"/>
+  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
 </dataset>
\ No newline at end of file
index 095449a77b627576b5e5c2d277d29a7b97bbed29..23bd158f3a1539020880a4c386c178d802b1c825 100644 (file)
@@ -1,6 +1,6 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
   <perm_templates_users/>
-  <perm_templates_groups id="1" template_id="1" group_id="1" permission="remaining_permission"/>
-  <perm_templates_groups id="2" template_id="1" group_id="[null]" permission="existing_permission"/>
+  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission"/>
+  <perm_templates_groups id="2" template_id="1" group_id="[null]" permission_reference="existing_permission"/>
 </dataset>
\ No newline at end of file
index 68bf04077f4926eb883c4d8ae7114773a7c0bfb7..f4c500ba1ffbe137fa4171bd7c1f5e8170f3422f 100644 (file)
@@ -1,7 +1,7 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
 
-  <perm_templates_users id="1" template_id="1" user_id="1" permission="remaining_permission"/>
+  <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="remaining_permission"/>
 
   <perm_templates_groups/>
 </dataset>
\ No newline at end of file
index 69427d2d3bf96e7e34c500c1809098b5e14e90f3..219c1f07286be20d006228f4549271ff8d879700 100644 (file)
@@ -1,8 +1,8 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description"/>
 
-  <perm_templates_users id="1" template_id="1" user_id="1" permission="remaining_permission"/>
-  <perm_templates_users id="2" template_id="1" user_id="2" permission="existing_permission"/>
+  <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="remaining_permission"/>
+  <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="existing_permission"/>
 
   <perm_templates_groups/>
 </dataset>
\ No newline at end of file
index 0de01c534864f066e81d3c183bc0f99842bcdce7..91b6e9744ebf1e5f8ec011200b020f25aeb5149a 100644 (file)
@@ -1,16 +1,16 @@
 <dataset>
   <permission_templates id="1" name="my template" description="my description" created_at="[null]" updated_at="[null]"/>
 
-  <perm_templates_users id="1" template_id="1" user_id="1" permission="user_permission1"/>
-  <perm_templates_users id="2" template_id="1" user_id="2" permission="user_permission1"/>
-  <perm_templates_users id="3" template_id="1" user_id="2" permission="user_permission2"/>
+  <perm_templates_users id="1" template_id="1" user_id="1" permission_reference="user_permission1"/>
+  <perm_templates_users id="2" template_id="1" user_id="2" permission_reference="user_permission1"/>
+  <perm_templates_users id="3" template_id="1" user_id="2" permission_reference="user_permission2"/>
 
   <users id="1" login="user1" name="user1" />
   <users id="2" login="user2" name="user2" />
 
-  <perm_templates_groups id="1" template_id="1" group_id="1" permission="group_permission1"/>
-  <perm_templates_groups id="2" template_id="1" group_id="2" permission="group_permission1"/>
-  <perm_templates_groups id="3" template_id="1" group_id="[null]" permission="group_permission2"/>
+  <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="group_permission1"/>
+  <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="group_permission1"/>
+  <perm_templates_groups id="3" template_id="1" group_id="[null]" permission_reference="group_permission2"/>
 
   <groups id="1" name="group1"/>
   <groups id="2" name="group2"/>
index 13581fad766a7cdc53dcef3f517471f312caddbb..10a70d79299db52e72d4ac4f109f1f3b75dd4960 100644 (file)
@@ -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
index b88ceabfc620cd4c603cad06a076aefaa5c3bb2a..b9ccf96f7111c9626d180fdfbb6c291ff66bc4e4 100644 (file)
@@ -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