diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-08-11 11:50:20 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-08-11 11:50:28 +0200 |
commit | 0e52cad9c679bda4ba5bd2a1487d441a732d2d7b (patch) | |
tree | 9fd9a9c04efadd8e684cd25a3f1818f554fad174 /sonar-core | |
parent | f4f68fce0f7c2fbe9cc8ff08555327f2618e483a (diff) | |
download | sonarqube-0e52cad9c679bda4ba5bd2a1487d441a732d2d7b.tar.gz sonarqube-0e52cad9c679bda4ba5bd2a1487d441a732d2d7b.zip |
SONAR-5175 When deleting a group, rows related to this group in perm_templates_groups table should be deleted
Diffstat (limited to 'sonar-core')
6 files changed, 45 insertions, 3 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/permission/PermissionTemplateDao.java b/sonar-core/src/main/java/org/sonar/core/permission/PermissionTemplateDao.java index 9ec979bc283..c28804d6d9b 100644 --- a/sonar-core/src/main/java/org/sonar/core/permission/PermissionTemplateDao.java +++ b/sonar-core/src/main/java/org/sonar/core/permission/PermissionTemplateDao.java @@ -32,6 +32,7 @@ import org.sonar.core.persistence.MyBatis; import javax.annotation.CheckForNull; import javax.annotation.Nullable; + import java.text.Normalizer; import java.util.Date; import java.util.List; @@ -244,6 +245,13 @@ public class PermissionTemplateDao implements TaskComponent, ServerComponent { } } + /** + * Remove a group from all templates (used when removing a group) + */ + public void removeByGroup(Long groupId, SqlSession session) { + session.getMapper(PermissionTemplateMapper.class).deleteByGroupId(groupId); + } + private String generateTemplateKee(String name, Date timeStamp) { if (PermissionTemplateDto.DEFAULT.getName().equals(name)) { return PermissionTemplateDto.DEFAULT.getKee(); diff --git a/sonar-core/src/main/java/org/sonar/core/permission/PermissionTemplateMapper.java b/sonar-core/src/main/java/org/sonar/core/permission/PermissionTemplateMapper.java index 63c2b7b9ff5..79e50415ffd 100644 --- a/sonar-core/src/main/java/org/sonar/core/permission/PermissionTemplateMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/permission/PermissionTemplateMapper.java @@ -48,4 +48,6 @@ public interface PermissionTemplateMapper { void insertGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup); void deleteGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup); + + void deleteByGroupId(long groupId); } diff --git a/sonar-core/src/main/resources/org/sonar/core/permission/PermissionTemplateMapper.xml b/sonar-core/src/main/resources/org/sonar/core/permission/PermissionTemplateMapper.xml index 7791fb0aa89..2ec027daa89 100644 --- a/sonar-core/src/main/resources/org/sonar/core/permission/PermissionTemplateMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/permission/PermissionTemplateMapper.xml @@ -61,6 +61,11 @@ </choose> </delete> + <delete id="deleteByGroupId" parameterType="long"> + DELETE FROM perm_templates_groups + WHERE group_id = #{groupId} + </delete> + <select id="selectUsers" parameterType="map" resultType="UserWithPermission"> SELECT u.login as login, u.name as name, ptu.permission_reference as permission FROM users u diff --git a/sonar-core/src/test/java/org/sonar/core/permission/PermissionTemplateDaoTest.java b/sonar-core/src/test/java/org/sonar/core/permission/PermissionTemplateDaoTest.java index 5a71651c35e..0bb6f825edd 100644 --- a/sonar-core/src/test/java/org/sonar/core/permission/PermissionTemplateDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/permission/PermissionTemplateDaoTest.java @@ -20,6 +20,7 @@ package org.sonar.core.permission; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.sonar.api.utils.System2; @@ -33,23 +34,28 @@ import java.util.Date; import java.util.List; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; public class PermissionTemplateDaoTest extends AbstractDaoTestCase { Date now; PermissionTemplateDao permissionTemplateDao; + DbSession session; System2 system = mock(System2.class); @Before public void setUpDao() throws ParseException { + session = getMyBatis().openSession(false); now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2013-01-02 01:04:05"); when(system.now()).thenReturn(now.getTime()); permissionTemplateDao = new PermissionTemplateDao(getMyBatis(), system); } + @After + public void after() { + this.session.close(); + } + @Test public void should_create_permission_template() throws Exception { setupData("createPermissionTemplate"); @@ -207,6 +213,15 @@ public class PermissionTemplateDaoTest extends AbstractDaoTestCase { } @Test + public void remove_by_group() throws Exception { + setupData("remove_by_group"); + permissionTemplateDao.removeByGroup(2L, session); + session.commit(); + + checkTable("remove_by_group", "perm_templates_groups", "id", "template_id", "group_id", "permission_reference"); + } + + @Test public void should_add_group_permission_with_null_name() throws Exception { setupData("addNullGroupPermissionToTemplate"); permissionTemplateDao.addGroupPermission(1L, null, "new_permission"); diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/PermissionTemplateDaoTest/remove_by_group-result.xml b/sonar-core/src/test/resources/org/sonar/core/permission/PermissionTemplateDaoTest/remove_by_group-result.xml new file mode 100644 index 00000000000..94ab2fde7ed --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/PermissionTemplateDaoTest/remove_by_group-result.xml @@ -0,0 +1,5 @@ +<dataset> + <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/> + <perm_templates_users/> + <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission_other_group"/> +</dataset> diff --git a/sonar-core/src/test/resources/org/sonar/core/permission/PermissionTemplateDaoTest/remove_by_group.xml b/sonar-core/src/test/resources/org/sonar/core/permission/PermissionTemplateDaoTest/remove_by_group.xml new file mode 100644 index 00000000000..2ff5815d855 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/permission/PermissionTemplateDaoTest/remove_by_group.xml @@ -0,0 +1,7 @@ +<dataset> + <permission_templates id="1" name="my template" kee="my_template_20130102_030405" description="my description"/> + <perm_templates_users/> + <perm_templates_groups id="1" template_id="1" group_id="1" permission_reference="remaining_permission_other_group"/> + <perm_templates_groups id="2" template_id="1" group_id="2" permission_reference="permission_to_remove"/> + <perm_templates_groups id="3" template_id="1" group_id="2" permission_reference="permission_to_remove2"/> +</dataset> |