diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-08-28 15:43:20 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2015-08-28 17:30:34 +0200 |
commit | ffc4e81629792f92b532e661b7a5d2a05550fd3a (patch) | |
tree | 74094bbe16ea21d20334a643af8e20dfe7d85c5d /sonar-db | |
parent | 332c20dbcc77765ac095634817b5d60c9bbe1dcf (diff) | |
download | sonarqube-ffc4e81629792f92b532e661b7a5d2a05550fd3a.tar.gz sonarqube-ffc4e81629792f92b532e661b7a5d2a05550fd3a.zip |
SONAR-6498 WS permissions/delete_template impossible to delete a default template
Diffstat (limited to 'sonar-db')
5 files changed, 45 insertions, 65 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/RowNotFoundException.java b/sonar-db/src/main/java/org/sonar/db/RowNotFoundException.java index 669b43f9be1..a2f576c40b1 100644 --- a/sonar-db/src/main/java/org/sonar/db/RowNotFoundException.java +++ b/sonar-db/src/main/java/org/sonar/db/RowNotFoundException.java @@ -25,7 +25,7 @@ package org.sonar.db; * When selecting by id or key, the methods respect one of the following pattern: * <ul> * <li>selectOrFailByKey return the element or throws a RowNotFoundException</li> - * <li>selectByKey return an Optional (now) or a nullable element (legacy)</li> + * <li>selectByUuid return an Optional (now) or a nullable element (legacy)</li> * </ul> */ public class RowNotFoundException extends RuntimeException { diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java index 3ea563637d9..3700d39a1ce 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateDao.java @@ -126,25 +126,25 @@ public class PermissionTemplateDao implements Dao { } @CheckForNull - public PermissionTemplateDto selectByKey(DbSession session, String templateKey) { - return mapper(session).selectByKey(templateKey); + public PermissionTemplateDto selectByUuid(DbSession session, String templateKey) { + return mapper(session).selectByUuid(templateKey); } @CheckForNull - public PermissionTemplateDto selectByKey(String templateKey) { + public PermissionTemplateDto selectByUuid(String templateKey) { DbSession session = myBatis.openSession(false); try { - return selectByKey(session, templateKey); + return selectByUuid(session, templateKey); } finally { MyBatis.closeQuietly(session); } } @CheckForNull - public PermissionTemplateDto selectByKeyWithUserAndGroupPermissions(DbSession session, String templateKey) { + public PermissionTemplateDto selectByUuidWithUserAndGroupPermissions(DbSession session, String templateKey) { PermissionTemplateDto permissionTemplate = null; PermissionTemplateMapper mapper = mapper(session); - permissionTemplate = mapper.selectByKey(templateKey); + permissionTemplate = mapper.selectByUuid(templateKey); PermissionTemplateDto templateUsersPermissions = mapper.selectTemplateUsersPermissions(templateKey); if (templateUsersPermissions != null) { permissionTemplate.setUsersPermissions(templateUsersPermissions.getUsersPermissions()); @@ -157,10 +157,10 @@ public class PermissionTemplateDao implements Dao { } @CheckForNull - public PermissionTemplateDto selectByKeyWithUserAndGroupPermissions(String templateKey) { + public PermissionTemplateDto selectByUuidWithUserAndGroupPermissions(String templateKey) { DbSession session = myBatis.openSession(false); try { - return selectByKeyWithUserAndGroupPermissions(session, templateKey); + return selectByUuidWithUserAndGroupPermissions(session, templateKey); } finally { MyBatis.closeQuietly(session); } @@ -186,21 +186,11 @@ public class PermissionTemplateDao implements Dao { return permissionTemplate; } - public void deleteByKey(DbSession dbSession, String key) { - mapper(dbSession).deleteByKey(key); - } - - public void deleteById(Long templateId) { - SqlSession session = myBatis.openSession(false); - try { - PermissionTemplateMapper mapper = mapper(session); - mapper.deleteUsersPermissions(templateId); - mapper.deleteGroupsPermissions(templateId); - mapper.delete(templateId); - session.commit(); - } finally { - MyBatis.closeQuietly(session); - } + public void deleteById(DbSession session, long templateId) { + PermissionTemplateMapper mapper = mapper(session); + mapper.deleteUserPermissions(templateId); + mapper.deleteGroupPermissions(templateId); + mapper.delete(templateId); } /** @@ -319,25 +309,32 @@ public class PermissionTemplateDao implements Dao { session.commit(); } + public void deleteGroupPermissions(DbSession session, long templateId) { + mapper(session).deleteGroupPermissions(templateId); + } + + public void deleteUserPermissions(DbSession session, long templateId) { + mapper(session).deleteUserPermissions(templateId); + } + /** * Load permission template and load associated collections of users and groups permissions */ @VisibleForTesting PermissionTemplateDto selectPermissionTemplateWithPermissions(DbSession session, String templateKey) { - PermissionTemplateDto permissionTemplateDto = selectByKey(session, templateKey); + PermissionTemplateDto permissionTemplateDto = selectByUuid(session, templateKey); if (permissionTemplateDto == null) { throw new IllegalArgumentException("Could not retrieve permission template with key " + templateKey); } - PermissionTemplateDto templateWithPermissions = selectByKeyWithUserAndGroupPermissions(session, permissionTemplateDto.getKee()); + PermissionTemplateDto templateWithPermissions = selectByUuidWithUserAndGroupPermissions(session, permissionTemplateDto.getKee()); if (templateWithPermissions == null) { throw new IllegalArgumentException("Could not retrieve permissions for template with key " + templateKey); } return templateWithPermissions; } - public PermissionTemplateDto selectByName(DbSession dbSession, String templateName) { - char wildcard = '%'; - return mapper(dbSession).selectByName(wildcard + templateName.toUpperCase() + wildcard); + public PermissionTemplateDto selectByName(DbSession dbSession, String name) { + return mapper(dbSession).selectByName(name.toUpperCase()); } /** diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateMapper.java index 536a79afbb5..d4006be65e9 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionTemplateMapper.java @@ -35,11 +35,15 @@ public interface PermissionTemplateMapper { void delete(long templateId); - void deleteUsersPermissions(long templateId); + void deleteUserPermissions(long templateId); - void deleteGroupsPermissions(long templateId); + void deleteUserPermission(PermissionTemplateUserDto permissionTemplateUser); + + void deleteGroupPermissions(long templateId); - PermissionTemplateDto selectByKey(String templateKey); + void deleteGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup); + + PermissionTemplateDto selectByUuid(String templateUuid); PermissionTemplateDto selectTemplateUsersPermissions(String templateKey); @@ -47,12 +51,8 @@ public interface PermissionTemplateMapper { void insertUserPermission(PermissionTemplateUserDto permissionTemplateUser); - void deleteUserPermission(PermissionTemplateUserDto permissionTemplateUser); - void insertGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup); - void deleteGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup); - void deleteByGroupId(long groupId); List<GroupWithPermissionDto> selectGroups(Map<String, Object> params); @@ -64,6 +64,4 @@ public interface PermissionTemplateMapper { int countUsers(Map<String, Object> params); int countGroups(Map<String, Object> parameters); - - void deleteByKey(String key); } diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/PermissionTemplateMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/PermissionTemplateMapper.xml index 9eb1a4850c6..7381e9e7616 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/PermissionTemplateMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/PermissionTemplateMapper.xml @@ -19,17 +19,12 @@ WHERE id = #{templateId} </delete> - <delete id="deleteByKey" parameterType="string"> - DELETE FROM permission_templates - WHERE kee = #{key} - </delete> - - <delete id="deleteUsersPermissions" parameterType="long"> + <delete id="deleteUserPermissions" parameterType="long"> DELETE FROM perm_templates_users WHERE template_id = #{templateId} </delete> - <delete id="deleteGroupsPermissions" parameterType="long"> + <delete id="deleteGroupPermissions" parameterType="long"> DELETE FROM perm_templates_groups WHERE template_id = #{templateId} </delete> @@ -175,11 +170,11 @@ id, name, kee, description, key_pattern AS keyPattern, created_at AS createdAt, updated_at AS updatedAt </sql> - <select id="selectByKey" parameterType="String" resultType="PermissionTemplate"> + <select id="selectByUuid" parameterType="String" resultType="PermissionTemplate"> SELECT <include refid="templateColumns"/> FROM permission_templates - WHERE kee = #{kee} + WHERE kee=#{uuid} </select> <select id="selectAllPermissionTemplates" resultType="PermissionTemplate"> @@ -192,7 +187,7 @@ SELECT <include refid="templateColumns"/> FROM permission_templates - WHERE UPPER(name) LIKE #{templateName} ESCAPE '/' + WHERE UPPER(name)=#{templateName} </select> <select id="selectTemplateUsersPermissions" parameterType="String" resultMap="fullPermissionsTemplateResult"> diff --git a/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java index 526892b5c8b..b75c082da79 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/PermissionTemplateDaoTest.java @@ -96,7 +96,7 @@ public class PermissionTemplateDaoTest { public void should_select_permission_template() { db.prepareDbUnit(getClass(), "selectPermissionTemplate.xml"); - PermissionTemplateDto permissionTemplate = underTest.selectByKeyWithUserAndGroupPermissions("my_template_20130102_030405"); + PermissionTemplateDto permissionTemplate = underTest.selectByUuidWithUserAndGroupPermissions("my_template_20130102_030405"); assertThat(permissionTemplate).isNotNull(); assertThat(permissionTemplate.getName()).isEqualTo("my template"); @@ -117,7 +117,7 @@ public class PermissionTemplateDaoTest { public void should_select_empty_permission_template() { db.prepareDbUnit(getClass(), "selectEmptyPermissionTemplate.xml"); - PermissionTemplateDto permissionTemplate = underTest.selectByKeyWithUserAndGroupPermissions("my_template_20130102_030405"); + PermissionTemplateDto permissionTemplate = underTest.selectByUuidWithUserAndGroupPermissions("my_template_20130102_030405"); assertThat(permissionTemplate).isNotNull(); assertThat(permissionTemplate.getName()).isEqualTo("my template"); @@ -130,7 +130,7 @@ public class PermissionTemplateDaoTest { public void should_select_permission_template_by_key() { db.prepareDbUnit(getClass(), "selectPermissionTemplate.xml"); - PermissionTemplateDto permissionTemplate = underTest.selectByKey("my_template_20130102_030405"); + PermissionTemplateDto permissionTemplate = underTest.selectByUuid("my_template_20130102_030405"); assertThat(permissionTemplate).isNotNull(); assertThat(permissionTemplate.getId()).isEqualTo(1L); @@ -165,23 +165,13 @@ public class PermissionTemplateDaoTest { public void should_delete_permission_template() { db.prepareDbUnit(getClass(), "deletePermissionTemplate.xml"); - underTest.deleteById(1L); + underTest.deleteById(session, 1L); + session.commit(); checkTemplateTables("deletePermissionTemplate-result.xml"); } @Test - public void delete_by_key() { - String templateKey = "permission-template-key"; - underTest.insert(session, newPermissionTemplateDto().setKee(templateKey)); - assertThat(underTest.selectByKey(session, templateKey)).isNotNull(); - - underTest.deleteByKey(session, templateKey); - - assertThat(underTest.selectByKey(session, templateKey)).isNull(); - } - - @Test public void should_add_user_permission_to_template() { db.prepareDbUnit(getClass(), "addUserPermissionToTemplate.xml"); @@ -252,8 +242,8 @@ public class PermissionTemplateDaoTest { PermissionTemplateDto permissionTemplateDto = new PermissionTemplateDto().setName("Test template").setKee("test_template"); PermissionTemplateDto templateWithPermissions = new PermissionTemplateDto().setKee("test_template"); underTest = mock(PermissionTemplateDao.class); - when(underTest.selectByKey(db.getSession(), "test_template")).thenReturn(permissionTemplateDto); - when(underTest.selectByKeyWithUserAndGroupPermissions(db.getSession(), "test_template")).thenReturn(templateWithPermissions); + when(underTest.selectByUuid(db.getSession(), "test_template")).thenReturn(permissionTemplateDto); + when(underTest.selectByUuidWithUserAndGroupPermissions(db.getSession(), "test_template")).thenReturn(templateWithPermissions); when(underTest.selectPermissionTemplateWithPermissions(db.getSession(), "test_template")).thenCallRealMethod(); PermissionTemplateDto permissionTemplate = underTest.selectPermissionTemplateWithPermissions(db.getSession(), "test_template"); |