aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-01-23 09:46:54 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2017-01-23 14:16:01 +0100
commit0584dc8d5d5d82a2035763c34afba0e12cf6e8d0 (patch)
tree957b7f5e0be0f02b500b96c911a37b3494147d8e /sonar-db
parent8063f2effb292a6c5883bf58b6db867b56a749f0 (diff)
downloadsonarqube-0584dc8d5d5d82a2035763c34afba0e12cf6e8d0.tar.gz
sonarqube-0584dc8d5d5d82a2035763c34afba0e12cf6e8d0.zip
SONAR-8682 fix error when filtering on name
and a given group name exists in multiple organizations
Diffstat (limited to 'sonar-db')
-rw-r--r--sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java12
-rw-r--r--sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java7
-rw-r--r--sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml22
-rw-r--r--sonar-db/src/test/java/org/sonar/db/permission/template/GroupWithPermissionTemplateDaoTest.java128
4 files changed, 107 insertions, 62 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java
index ac6bb49fefd..d9784e57c34 100644
--- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java
+++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java
@@ -69,16 +69,12 @@ public class PermissionTemplateDao implements Dao {
return mapper(dbSession).selectUserPermissionsByTemplateIdAndUserLogins(templateId, Collections.emptyList());
}
- /**
- * @deprecated does not support organizations. Should return group ids.
- */
- @Deprecated
- public List<String> selectGroupNamesByQueryAndTemplate(DbSession session, PermissionQuery query, long templateId) {
- return mapper(session).selectGroupNamesByQueryAndTemplate(query, templateId, new RowBounds(query.getPageOffset(), query.getPageSize()));
+ public List<String> selectGroupNamesByQueryAndTemplate(DbSession session, PermissionQuery query, String organizationUuid, long templateId) {
+ return mapper(session).selectGroupNamesByQueryAndTemplate(organizationUuid, templateId, query, new RowBounds(query.getPageOffset(), query.getPageSize()));
}
- public int countGroupNamesByQueryAndTemplate(DbSession session, PermissionQuery query, long templateId) {
- return mapper(session).countGroupNamesByQueryAndTemplate(query, templateId);
+ public int countGroupNamesByQueryAndTemplate(DbSession session, PermissionQuery query, String organizationUuid, long templateId) {
+ return mapper(session).countGroupNamesByQueryAndTemplate(organizationUuid, query, templateId);
}
public List<PermissionTemplateGroupDto> selectGroupPermissionsByTemplateIdAndGroupNames(DbSession dbSession, long templateId, List<String> groups) {
diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java
index 55b3d2569ec..2f58d738e66 100644
--- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java
+++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java
@@ -47,7 +47,7 @@ public interface PermissionTemplateMapper {
void deleteUserPermission(PermissionTemplateUserDto permissionTemplateUser);
void deleteGroupPermissionsByTemplateId(long templateId);
-
+
void deleteGroupPermissionsByTemplateIds(@Param("templateIds") List<Long> templateIds);
void deleteGroupPermission(PermissionTemplateGroupDto permissionTemplateGroup);
@@ -70,9 +70,10 @@ public interface PermissionTemplateMapper {
int countUserLoginsByQueryAndTemplate(@Param("query") PermissionQuery query, @Param("templateId") long templateId);
- List<String> selectGroupNamesByQueryAndTemplate(@Param("query") PermissionQuery query, @Param("templateId") long templateId, RowBounds rowBounds);
+ List<String> selectGroupNamesByQueryAndTemplate(@Param("organizationUuid") String organizationUuid,
+ @Param("templateId") long templateId, @Param("query") PermissionQuery query, RowBounds rowBounds);
- int countGroupNamesByQueryAndTemplate(@Param("query") PermissionQuery query, @Param("templateId") long templateId);
+ int countGroupNamesByQueryAndTemplate(@Param("organizationUuid") String organizationUuid, @Param("query") PermissionQuery query, @Param("templateId") long templateId);
List<PermissionTemplateDto> selectAll(@Param("organizationUuid") String organizationUuid, @Nullable @Param("upperCaseNameLikeSql") String upperCaseNameLikeSql);
diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml
index 0001f3e18f4..fa79c9a5add 100644
--- a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml
+++ b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml
@@ -157,11 +157,22 @@
<sql id="groupNamesByQueryAndTemplate">
FROM
- (SELECT g.id AS group_id, g.name AS name, ptg.permission_reference AS permission, ptg.template_id AS templateId
+ (SELECT
+ g.id AS group_id,
+ g.name AS name,
+ ptg.permission_reference AS permission,
+ ptg.template_id AS templateId
FROM groups g
- LEFT JOIN perm_templates_groups ptg ON ptg.group_id=g.id
+ LEFT JOIN perm_templates_groups ptg ON
+ ptg.group_id=g.id
+ where
+ g.organization_uuid=#{organizationUuid,jdbcType=VARCHAR}
UNION ALL
- SELECT 0 AS group_id, 'Anyone' AS name, ptg.permission_reference AS permission, ptg.template_id AS templateId
+ SELECT
+ 0 AS group_id,
+ 'Anyone' AS name,
+ ptg.permission_reference AS permission,
+ ptg.template_id AS templateId
FROM perm_templates_groups ptg
<where>
<if test="query.withAtLeastOnePermission()">
@@ -259,8 +270,9 @@
g.name AS groupName,
ptg.created_at as createdAt,
ptg.updated_at as updatedAt
- FROM perm_templates_groups ptg
- INNER JOIN groups g ON g.id=ptg.group_id
+ FROM perm_templates_groups ptg
+ INNER JOIN groups g ON
+ g.id=ptg.group_id
UNION ALL
SELECT
ptg.id,
diff --git a/sonar-db/src/test/java/org/sonar/db/permission/template/GroupWithPermissionTemplateDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/template/GroupWithPermissionTemplateDaoTest.java
index 6cf5abc3adc..1c197714719 100644
--- a/sonar-db/src/test/java/org/sonar/db/permission/template/GroupWithPermissionTemplateDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/permission/template/GroupWithPermissionTemplateDaoTest.java
@@ -27,6 +27,7 @@ import org.junit.Test;
import org.sonar.api.utils.System2;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.permission.PermissionQuery;
import org.sonar.db.user.GroupDto;
@@ -43,95 +44,122 @@ public class GroupWithPermissionTemplateDaoTest {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
+
private DbSession session = db.getSession();
private PermissionTemplateDbTester permissionTemplateDbTester = db.permissionTemplates();
private PermissionTemplateDao underTest = db.getDbClient().permissionTemplateDao();
@Test
public void select_group_names_by_query_and_template() {
- GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1"));
- GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2"));
- GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3"));
+ OrganizationDto organization = db.organizations().insert();
+ GroupDto group1 = db.users().insertGroup(organization, "Group-1");
+ GroupDto group2 = db.users().insertGroup(organization, "Group-2");
+ GroupDto group3 = db.users().insertGroup(organization, "Group-3");
- PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
+ PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), USER);
permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), ADMIN);
permissionTemplateDbTester.addGroupToTemplate(template.getId(), group2.getId(), PROVISIONING);
- PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate();
+ PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate(organization);
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), null, USER);
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), group1.getId(), PROVISIONING);
- assertThat(selectGroupNamesByQueryAndTemplate(builder().build(), template.getId())).containsOnly("Group-1", "Group-2", "Group-3", "Anyone");
- assertThat(selectGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission().build(), template.getId())).containsOnly("Group-1", "Group-2");
- assertThat(selectGroupNamesByQueryAndTemplate(builder().setPermission(USER).build(), template.getId())).containsOnly("Group-1");
- assertThat(selectGroupNamesByQueryAndTemplate(builder().setPermission(USER).build(), anotherTemplate.getId())).containsOnly("Anyone");
- assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("groU").build(), template.getId())).containsOnly("Group-1", "Group-2", "Group-3");
- assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("nYo").build(), template.getId())).containsOnly("Anyone");
- assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("p-2").build(), template.getId())).containsOnly("Group-2");
-
- assertThat(selectGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission().build(), 123L)).isEmpty();
- assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("unknown").build(), template.getId())).isEmpty();
+ assertThat(selectGroupNamesByQueryAndTemplate(builder(), organization, template))
+ .containsOnly("Group-1", "Group-2", "Group-3", "Anyone");
+ assertThat(selectGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission(), organization, template))
+ .containsOnly("Group-1", "Group-2");
+ assertThat(selectGroupNamesByQueryAndTemplate(builder().setPermission(USER), organization, template))
+ .containsOnly("Group-1");
+ assertThat(selectGroupNamesByQueryAndTemplate(builder().setPermission(USER), organization, anotherTemplate))
+ .containsOnly("Anyone");
+ assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("groU"), organization, template))
+ .containsOnly("Group-1", "Group-2", "Group-3");
+ assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("nYo"), organization, template))
+ .containsOnly("Anyone");
+ assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("p-2"), organization, template))
+ .containsOnly("Group-2");
+
+ assertThat(selectGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission().build(), organization, 123L))
+ .isEmpty();
+ assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("unknown"), organization, template))
+ .isEmpty();
}
@Test
public void select_group_names_by_query_and_template_is_ordered_by_group_names() {
- GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2"));
- db.users().insertGroup(newGroupDto().setName("Group-3"));
- db.users().insertGroup(newGroupDto().setName("Group-1"));
+ OrganizationDto organization = db.organizations().insert();
+ GroupDto group2 = db.users().insertGroup(organization, "Group-2");
+ db.users().insertGroup(organization, "Group-3");
+ db.users().insertGroup(organization, "Group-1");
- PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
+ PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
permissionTemplateDbTester.addGroupToTemplate(template.getId(), group2.getId(), USER);
- assertThat(selectGroupNamesByQueryAndTemplate(builder().build(), template.getId())).containsExactly("Anyone", "Group-1", "Group-2", "Group-3");
+ assertThat(selectGroupNamesByQueryAndTemplate(builder(), organization, template))
+ .containsExactly("Anyone", "Group-1", "Group-2", "Group-3");
}
@Test
public void select_group_names_by_query_and_template_is_paginated() {
- IntStream.rangeClosed(0, 9).forEach(i -> db.users().insertGroup(newGroupDto().setName(i + "-name")));
+ OrganizationDto organization = db.organizations().insert();
+ IntStream.rangeClosed(0, 9).forEach(i -> db.users().insertGroup(organization, i + "-name"));
- PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
+ PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
- assertThat(selectGroupNamesByQueryAndTemplate(builder().setPageIndex(1).setPageSize(1).build(), template.getId())).containsExactly("0-name");
- assertThat(selectGroupNamesByQueryAndTemplate(builder().setPageIndex(2).setPageSize(3).build(), template.getId())).containsExactly("3-name", "4-name", "5-name");
+ assertThat(selectGroupNamesByQueryAndTemplate(builder().setPageIndex(1).setPageSize(1), organization, template))
+ .containsExactly("0-name");
+ assertThat(selectGroupNamesByQueryAndTemplate(builder().setPageIndex(2).setPageSize(3), organization, template))
+ .containsExactly("3-name", "4-name", "5-name");
}
@Test
public void select_group_names_by_query_and_template_returns_anyone() {
- PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
+ OrganizationDto organization = db.organizations().insert();
+ PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
GroupDto group = db.users().insertGroup(newGroupDto().setName("Group"));
- PermissionTemplateDto otherTemplate = permissionTemplateDbTester.insertTemplate();
+ PermissionTemplateDto otherTemplate = permissionTemplateDbTester.insertTemplate(organization);
permissionTemplateDbTester.addGroupToTemplate(otherTemplate.getId(), group.getId(), USER);
- assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("nyo").build(), template.getId())).containsExactly("Anyone");
+ assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("nyo"), organization, template))
+ .containsExactly("Anyone");
}
@Test
public void count_group_names_by_query_and_template() {
- GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1"));
- GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2"));
- GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3"));
+ OrganizationDto organization = db.organizations().insert();
+ GroupDto group1 = db.users().insertGroup(organization, "Group-1");
+ GroupDto group2 = db.users().insertGroup(organization, "Group-2");
+ GroupDto group3 = db.users().insertGroup(organization, "Group-3");
- PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
+ PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), USER);
permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), ADMIN);
permissionTemplateDbTester.addGroupToTemplate(template.getId(), group2.getId(), PROVISIONING);
- PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate();
+ PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate(organization);
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), null, USER);
permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), group1.getId(), PROVISIONING);
- assertThat(countGroupNamesByQueryAndTemplate(builder().build(), template.getId())).isEqualTo(4);
- assertThat(countGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission().build(), template.getId())).isEqualTo(2);
- assertThat(countGroupNamesByQueryAndTemplate(builder().setPermission(USER).build(), template.getId())).isEqualTo(1);
- assertThat(countGroupNamesByQueryAndTemplate(builder().setPermission(USER).build(), anotherTemplate.getId())).isEqualTo(1);
- assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("groU").build(), template.getId())).isEqualTo(3);
- assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("nYo").build(), template.getId())).isEqualTo(1);
- assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("p-2").build(), template.getId())).isEqualTo(1);
-
- assertThat(countGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission().build(), 123L)).isZero();
- assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("unknown").build(), template.getId())).isZero();
+ assertThat(countGroupNamesByQueryAndTemplate(builder(), organization, template))
+ .isEqualTo(4);
+ assertThat(countGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission(), organization, template))
+ .isEqualTo(2);
+ assertThat(countGroupNamesByQueryAndTemplate(builder().setPermission(USER), organization, template)).isEqualTo(1);
+ assertThat(countGroupNamesByQueryAndTemplate(builder().setPermission(USER), organization, anotherTemplate))
+ .isEqualTo(1);
+ assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("groU"), organization, template))
+ .isEqualTo(3);
+ assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("nYo"), organization, template))
+ .isEqualTo(1);
+ assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("p-2"), organization, template))
+ .isEqualTo(1);
+
+ assertThat(countGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission().build(), organization, 123L))
+ .isZero();
+ assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("unknown"), organization, template))
+ .isZero();
}
@Test
@@ -200,12 +228,20 @@ public class GroupWithPermissionTemplateDaoTest {
assertThat(underTest.selectGroupPermissionsByTemplateId(session, 321L)).isEmpty();
}
- private List<String> selectGroupNamesByQueryAndTemplate(PermissionQuery query, long templateId) {
- return underTest.selectGroupNamesByQueryAndTemplate(session, query, templateId);
+ private List<String> selectGroupNamesByQueryAndTemplate(PermissionQuery.Builder queryBuilder, OrganizationDto organization, PermissionTemplateDto permissionTemplateDto) {
+ return selectGroupNamesByQueryAndTemplate(queryBuilder.build(), organization, permissionTemplateDto.getId());
+ }
+
+ private List<String> selectGroupNamesByQueryAndTemplate(PermissionQuery query, OrganizationDto organization, long templateId) {
+ return underTest.selectGroupNamesByQueryAndTemplate(session, query, organization.getUuid(), templateId);
+ }
+
+ private int countGroupNamesByQueryAndTemplate(PermissionQuery.Builder queryBuilder, OrganizationDto organization, PermissionTemplateDto permissionTemplateDto) {
+ return countGroupNamesByQueryAndTemplate(queryBuilder.build(), organization, permissionTemplateDto.getId());
}
- private int countGroupNamesByQueryAndTemplate(PermissionQuery query, long templateId) {
- return underTest.countGroupNamesByQueryAndTemplate(session, query, templateId);
+ private int countGroupNamesByQueryAndTemplate(PermissionQuery query, OrganizationDto organization, long templateId) {
+ return underTest.countGroupNamesByQueryAndTemplate(session, query, organization.getUuid(), templateId);
}
}