diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2016-07-12 09:03:14 +0200 |
---|---|---|
committer | Stas Vilchik <vilchiks@gmail.com> | 2016-07-12 10:17:57 +0200 |
commit | 0fbbe800ee3ae1f68df6e5d4c868a2910b981a55 (patch) | |
tree | 17af61e39e89c1118c434b36b35462c885ea02ae /sonar-db/src | |
parent | 4517fd6b3732457a17baed69319538691d6a2baf (diff) | |
download | sonarqube-0fbbe800ee3ae1f68df6e5d4c868a2910b981a55.tar.gz sonarqube-0fbbe800ee3ae1f68df6e5d4c868a2910b981a55.zip |
SONAR-7838 SONAR-7835 Return all permissions of matching users
When searching for permissions users that match a given permission, instead of only returnin this permission we now return all user permissions
Diffstat (limited to 'sonar-db/src')
6 files changed, 220 insertions, 145 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionDao.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionDao.java index a1d4b753793..b0c421962f6 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionDao.java @@ -51,7 +51,7 @@ public class PermissionDao implements Dao { /** * @return a paginated list of users. - * @deprecated + * @deprecated use {@link #selectLoginsByPermissionQuery(DbSession, PermissionQuery)} or {@link #selectUserPermissionsByLoginsAnProject(DbSession, List, Long)} instead */ @Deprecated public List<UserWithPermissionDto> selectUsers(DbSession session, OldPermissionQuery query, @Nullable Long componentId, int offset, int limit) { @@ -71,10 +71,14 @@ public class PermissionDao implements Dao { return mapper(dbSession).countUsersByPermissionQuery(query); } - public List<UserPermissionDto> selectUserPermissionsByLogins(DbSession dbSession, List<String> logins) { - return executeLargeInputs(logins, mapper(dbSession)::selectUserPermissionsByLogins); + public List<UserPermissionDto> selectUserPermissionsByLoginsAnProject(DbSession dbSession, List<String> logins, @Nullable Long projectId) { + return executeLargeInputs(logins, l -> mapper(dbSession).selectUserPermissionsByLogins(l, projectId)); } + /** + * @deprecated use {@link #countUsersByQuery(DbSession, PermissionQuery)} instead + */ + @Deprecated public int countUsers(DbSession session, OldPermissionQuery query, @Nullable Long componentId) { Map<String, Object> params = usersParameters(query, componentId); @@ -93,7 +97,9 @@ public class PermissionDao implements Dao { * Membership parameter from query is not taking into account in order to deal more easily with the 'Anyone' group * * @return a non paginated list of groups. + * @deprecated use {@link #selectGroupNamesByPermissionQuery(DbSession, PermissionQuery)} or {@link #selectGroupPermissionsByGroupNamesAndProject(DbSession, List, Long)} instead */ + @Deprecated public List<GroupWithPermissionDto> selectGroups(DbSession session, OldPermissionQuery query, @Nullable Long componentId) { Map<String, Object> params = groupsParameters(query, componentId); return mapper(session).selectGroups(params); @@ -128,8 +134,8 @@ public class PermissionDao implements Dao { return mapper(dbSession).countGroupsByPermissionQuery(query); } - public List<GroupRoleDto> selectGroupPermissionsByQuery(DbSession dbSession, PermissionQuery query) { - return mapper(dbSession).selectGroupPermissionByQuery(query); + public List<GroupRoleDto> selectGroupPermissionsByGroupNamesAndProject(DbSession dbSession, List<String> groupNames, @Nullable Long projectId) { + return executeLargeInputs(groupNames, groups -> mapper(dbSession).selectGroupPermissionByGroupNames(groups, projectId)); } /** diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionMapper.java index 2a1fc5cc11a..81f98091b6c 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionMapper.java @@ -21,6 +21,7 @@ package org.sonar.db.permission; import java.util.List; import java.util.Map; +import javax.annotation.Nullable; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; @@ -37,7 +38,7 @@ public interface PermissionMapper { int countUsersByPermissionQuery(@Param("query") PermissionQuery query); - List<UserPermissionDto> selectUserPermissionsByLogins(@Param("logins") List<String> logins); + List<UserPermissionDto> selectUserPermissionsByLogins(@Param("logins") List<String> logins, @Nullable @Param("projectId") Long projectId); List<GroupWithPermissionDto> selectGroups(Map<String, Object> parameters); @@ -47,7 +48,7 @@ public interface PermissionMapper { int countGroupsByPermissionQuery(@Param("query") PermissionQuery query); - List<GroupRoleDto> selectGroupPermissionByQuery(@Param("query") PermissionQuery query); + List<GroupRoleDto> selectGroupPermissionByGroupNames(@Param("groupNames") List<String> groupNames, @Nullable @Param("projectId") Long projectId); void usersCountByProjectIdAndPermission(Map<String, Object> parameters, ResultHandler resultHandler); diff --git a/sonar-db/src/main/java/org/sonar/db/permission/PermissionQuery.java b/sonar-db/src/main/java/org/sonar/db/permission/PermissionQuery.java index c47fff65b9c..3addc4babe0 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/PermissionQuery.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/PermissionQuery.java @@ -19,9 +19,6 @@ */ package org.sonar.db.permission; -import com.google.common.collect.ImmutableList; -import java.util.Collections; -import java.util.List; import java.util.Locale; import javax.annotation.CheckForNull; import javax.annotation.Nullable; @@ -50,8 +47,6 @@ public class PermissionQuery { private final String searchQuery; private final String searchQueryToSql; private final boolean withPermissionOnly; - private final List<String> logins; - private final List<String> groupNames; private final int pageSize; private final int pageOffset; @@ -65,8 +60,6 @@ public class PermissionQuery { this.searchQueryToSql = builder.searchQuery == null ? null : buildLikeValue(builder.searchQuery, WildcardPosition.BEFORE_AND_AFTER).toLowerCase(Locale.ENGLISH); this.pageSize = builder.pageSize; this.pageOffset = offset(builder.pageIndex, builder.pageSize); - this.logins = builder.logins == null ? Collections.emptyList() : ImmutableList.copyOf(builder.logins); - this.groupNames = builder.groupNames == null ? Collections.emptyList() : ImmutableList.copyOf(builder.groupNames); } @CheckForNull @@ -105,14 +98,6 @@ public class PermissionQuery { return pageOffset; } - public List<String> getLogins() { - return logins; - } - - public List<String> getGroupNames() { - return groupNames; - } - public static Builder builder() { return new Builder(); } @@ -123,8 +108,6 @@ public class PermissionQuery { private String template; private String searchQuery; private boolean withPermissionOnly; - private List<String> logins; - private List<String> groupNames; private Integer pageIndex = DEFAULT_PAGE_INDEX; private Integer pageSize = DEFAULT_PAGE_SIZE; @@ -169,22 +152,10 @@ public class PermissionQuery { return this; } - public Builder setLogins(@Nullable List<String> logins) { - this.logins = logins; - return this; - } - - public Builder setGroupNames(@Nullable List<String> groupNames) { - this.groupNames = groupNames; - return this; - } - public PermissionQuery build() { this.pageIndex = firstNonNull(pageIndex, DEFAULT_PAGE_INDEX); this.pageSize = firstNonNull(pageSize, DEFAULT_PAGE_SIZE); checkArgument(searchQuery == null || searchQuery.length() >= SEARCH_QUERY_MIN_LENGTH); - checkArgument(logins == null || !logins.isEmpty()); - checkArgument(groupNames == null || !groupNames.isEmpty()); return new PermissionQuery(this); } } diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/PermissionMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/PermissionMapper.xml index ffe5974e41a..4140eb59667 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/PermissionMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/PermissionMapper.xml @@ -95,6 +95,12 @@ <foreach collection="logins" open="(" close=")" item="login" separator=","> #{login} </foreach> + <if test="projectId!=null"> + AND ur.resource_id=#{projectId} + </if> + <if test="projectId==null"> + AND ur.resource_id IS NULL + </if> </where> </select> @@ -215,11 +221,6 @@ <include refid="groupsByQuery" />) g </select> - <select id="selectGroupPermissionByQuery" parameterType="map" resultType="GroupRole"> - select distinct gr.id as id, gr.group_id as groupId, gr.resource_id as resourceId, gr.permission as role - <include refid="groupsByQuery" /> - </select> - <sql id="groupsByQuery"> from ( select g.id as group_id, g.name as name, gr.role as permission, gr.resource_id as resource_id, gr.id as id @@ -236,12 +237,6 @@ ) gr left join projects p on gr.resource_id = p.id <where> - <if test="!query.groupNames.isEmpty()"> - and gr.name in - <foreach collection="query.groupNames" open="(" close=")" item="name" separator=","> - #{name} - </foreach> - </if> <if test="query.searchQueryToSql != null"> and lower(gr.name) like #{query.searchQueryToSql} ESCAPE '/' </if> @@ -260,4 +255,31 @@ </if> </where> </sql> + + <select id="selectGroupPermissionByGroupNames" parameterType="map" resultType="GroupRole"> + SELECT sub.id, sub.groupId, sub.resourceId, sub.role + FROM + ( + SELECT gr.id AS id, gr.group_id AS groupId, gr.resource_id AS resourceId, gr.role AS role, g.name AS name + FROM group_roles gr + INNER JOIN groups g ON g.id=gr.group_id + UNION ALL + SELECT gr.id as id, 0 AS group_id, gr.resource_id AS resource_id, gr.role AS role, 'Anyone' AS name + FROM group_roles gr + WHERE gr.group_id is null + ) sub + <where> + sub.name IN + <foreach collection="groupNames" open="(" close=")" item="name" separator=","> + #{name} + </foreach> + <if test="projectId!=null"> + AND sub.resourceId=#{projectId} + </if> + <if test="projectId==null"> + AND sub.resourceId IS NULL + </if> + </where> + </select> + </mapper> diff --git a/sonar-db/src/test/java/org/sonar/db/permission/GroupWithPermissionDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/GroupWithPermissionDaoTest.java index 9e0b104b7e7..0a3b0b1ebee 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/GroupWithPermissionDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/GroupWithPermissionDaoTest.java @@ -20,15 +20,13 @@ package org.sonar.db.permission; import java.util.ArrayList; -import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.stream.IntStream; import org.junit.Rule; import org.junit.Test; -import org.sonar.api.security.DefaultGroups; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; -import org.sonar.core.permission.GlobalPermissions; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDbTester; @@ -37,9 +35,10 @@ import org.sonar.db.user.GroupDbTester; import org.sonar.db.user.GroupDto; import org.sonar.db.user.GroupRoleDto; -import static com.google.common.collect.Lists.newArrayList; +import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; +import static org.sonar.api.security.DefaultGroups.ANYONE; import static org.sonar.api.web.UserRole.ADMIN; import static org.sonar.api.web.UserRole.ISSUE_ADMIN; import static org.sonar.api.web.UserRole.USER; @@ -177,7 +176,7 @@ public class GroupWithPermissionDaoTest { permissionDb.addProjectPermissionToGroup(USER, group1.getId(), 456L); final List<CountByProjectAndPermissionDto> result = new ArrayList<>(); - underTest.groupsCountByComponentIdAndPermission(dbSession, Arrays.asList(123L, 456L, 789L), context -> result.add((CountByProjectAndPermissionDto) context.getResultObject())); + underTest.groupsCountByComponentIdAndPermission(dbSession, asList(123L, 456L, 789L), context -> result.add((CountByProjectAndPermissionDto) context.getResultObject())); assertThat(result).hasSize(3); assertThat(result).extracting("permission").containsOnly(ADMIN, USER); @@ -186,23 +185,45 @@ public class GroupWithPermissionDaoTest { } @Test - public void select_groups_by_query_ordered_by_group_names() { - GroupDto group2 = groupDb.insertGroup(newGroupDto().setName("Group-2")); - GroupDto group3 = groupDb.insertGroup(newGroupDto().setName("Group-3")); - GroupDto group1 = groupDb.insertGroup(newGroupDto().setName("Group-1")); + public void select_groups_by_query() { + GroupDto group1 = groupDb.insertGroup(newGroupDto()); + GroupDto group2 = groupDb.insertGroup(newGroupDto()); + GroupDto group3 = groupDb.insertGroup(newGroupDto()); + permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, null); + List<String> groupNames = underTest.selectGroupNamesByPermissionQuery(dbSession, PermissionQuery.builder().build()); + assertThat(groupNames).containsOnly("Anyone", group1.getName(), group2.getName(), group3.getName()); + } + + @Test + public void select_groups_by_query_is_ordered_by_group_names() { + groupDb.insertGroup(newGroupDto().setName("Group-2")); + groupDb.insertGroup(newGroupDto().setName("Group-3")); + groupDb.insertGroup(newGroupDto().setName("Group-1")); permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, null); - PermissionQuery.Builder dbQuery = PermissionQuery.builder(); - List<String> groupNames = selectGroupNames(dbQuery); - int countNames = countGroupNames(dbQuery); - List<GroupRoleDto> permissions = selectGroupPermissions(dbQuery); + assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, + PermissionQuery.builder().build())).containsExactly("Anyone", "Group-1", "Group-2", "Group-3"); + } - assertThat(groupNames).containsExactly("Anyone", "Group-1", "Group-2", "Group-3"); - assertThat(countNames).isEqualTo(4); - assertThat(permissions).hasSize(4) - .extracting(GroupRoleDto::getGroupId) - .containsOnlyOnce(0L, group1.getId(), group2.getId(), group3.getId()); + @Test + public void count_groups_by_query() { + GroupDto group1 = groupDb.insertGroup(newGroupDto().setName("Group-1")); + GroupDto group2 = groupDb.insertGroup(newGroupDto().setName("Group-2")); + GroupDto group3 = groupDb.insertGroup(newGroupDto().setName("Group-3")); + permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, null); + permissionDb.addGlobalPermissionToGroup(PROVISIONING, group1.getId()); + + assertThat(underTest.countGroupsByPermissionQuery(dbSession, + PermissionQuery.builder().build())).isEqualTo(4); + assertThat(underTest.countGroupsByPermissionQuery(dbSession, + PermissionQuery.builder().setPermission(PROVISIONING).build())).isEqualTo(1); + assertThat(underTest.countGroupsByPermissionQuery(dbSession, + PermissionQuery.builder().withPermissionOnly().build())).isEqualTo(2); + assertThat(underTest.countGroupsByPermissionQuery(dbSession, + PermissionQuery.builder().setSearchQuery("Group-").build())).isEqualTo(3); + assertThat(underTest.countGroupsByPermissionQuery(dbSession, + PermissionQuery.builder().setSearchQuery("Any").build())).isEqualTo(1); } @Test @@ -213,26 +234,20 @@ public class GroupWithPermissionDaoTest { ComponentDto project = componentDb.insertComponent(newProjectDto()); - permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, group1.getId()); permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, null); permissionDb.addGlobalPermissionToGroup(PROVISIONING, null); + permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, group1.getId()); + permissionDb.addGlobalPermissionToGroup(SYSTEM_ADMIN, group3.getId()); permissionDb.addProjectPermissionToGroup(UserRole.ADMIN, group2.getId(), project.getId()); - permissionDb.addGlobalPermissionToGroup(GlobalPermissions.SYSTEM_ADMIN, group3.getId()); - - PermissionQuery.Builder dbQuery = PermissionQuery.builder() - .withPermissionOnly() - .setPermission(SCAN_EXECUTION); - List<String> groupNames = selectGroupNames(dbQuery); - int countNames = countGroupNames(dbQuery); - List<GroupRoleDto> permissions = selectGroupPermissions(dbQuery); - - assertThat(groupNames).containsExactly(DefaultGroups.ANYONE, "Group-1"); - assertThat(countNames).isEqualTo(2); - assertThat(permissions) - .extracting(GroupRoleDto::getRole, GroupRoleDto::getGroupId, GroupRoleDto::getResourceId) - .containsOnlyOnce( - tuple(SCAN_EXECUTION, 0L, null), - tuple(SCAN_EXECUTION, group1.getId(), null)); + + assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, + PermissionQuery.builder().setPermission(SCAN_EXECUTION).build())).containsExactly(ANYONE, "Group-1"); + + assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, + PermissionQuery.builder().setPermission(SYSTEM_ADMIN).build())).containsExactly("Group-3"); + + assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, + PermissionQuery.builder().setPermission(PROVISIONING).build())).containsExactly(ANYONE); } @Test @@ -246,93 +261,122 @@ public class GroupWithPermissionDaoTest { permissionDb.addProjectPermissionToGroup(SCAN_EXECUTION, group1.getId(), project.getId()); permissionDb.addProjectPermissionToGroup(PROVISIONING, group1.getId(), project.getId()); + permissionDb.addProjectPermissionToGroup(USER, null, project.getId()); + permissionDb.addProjectPermissionToGroup(SYSTEM_ADMIN, group1.getId(), anotherProject.getId()); permissionDb.addProjectPermissionToGroup(SYSTEM_ADMIN, null, anotherProject.getId()); - permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, group2.getId()); permissionDb.addProjectPermissionToGroup(SCAN_EXECUTION, group3.getId(), anotherProject.getId()); - PermissionQuery.Builder dbQuery = PermissionQuery.builder() - .setComponentUuid(project.uuid()) - .withPermissionOnly(); - List<String> groupNames = selectGroupNames(dbQuery); - int countNames = countGroupNames(dbQuery); - List<GroupRoleDto> permissions = selectGroupPermissions(dbQuery); - - assertThat(groupNames).containsOnlyOnce(group1.getName()); - assertThat(countNames).isEqualTo(1); - assertThat(permissions).hasSize(2) - .extracting(GroupRoleDto::getRole, GroupRoleDto::getGroupId, GroupRoleDto::getResourceId) - .containsOnlyOnce( - tuple(SCAN_EXECUTION, group1.getId(), project.getId()), - tuple(PROVISIONING, group1.getId(), project.getId())); - } + permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, group2.getId()); - @Test - public void select_groups_by_query_in_group_names() { - groupDb.insertGroup(newGroupDto().setName("group-name-1")); - groupDb.insertGroup(newGroupDto().setName("group-name-2")); - groupDb.insertGroup(newGroupDto().setName("another-group-name")); - - PermissionQuery.Builder dbQuery = PermissionQuery.builder().setGroupNames(newArrayList("group-name-1", "group-name-2")); - List<String> groupNames = selectGroupNames(dbQuery); - int countNames = countGroupNames(dbQuery); - List<GroupRoleDto> permissions = selectGroupPermissions(dbQuery); - - assertThat(groupNames).containsOnlyOnce("group-name-1", "group-name-2"); - assertThat(countNames).isEqualTo(2); - assertThat(permissions).hasSize(2); + PermissionQuery.Builder builderOnComponent = PermissionQuery.builder().setComponentUuid(project.uuid()); + assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, + builderOnComponent.withPermissionOnly().build())).containsOnlyOnce(group1.getName()); + assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, + builderOnComponent.setPermission(SCAN_EXECUTION).build())).containsOnlyOnce(group1.getName()); + assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, + builderOnComponent.setPermission(USER).build())).containsOnlyOnce(ANYONE); } @Test public void select_groups_by_query_paginated() { IntStream.rangeClosed(0, 9).forEach(i -> groupDb.insertGroup(newGroupDto().setName(i + "-name"))); - PermissionQuery.Builder dbQuery = PermissionQuery.builder().setPageIndex(2).setPageSize(3); - List<String> groupNames = selectGroupNames(dbQuery); - int countNames = countGroupNames(dbQuery); - - assertThat(groupNames).containsExactly("3-name", "4-name", "5-name"); - assertThat(countNames).isEqualTo(10); + assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, + PermissionQuery.builder().setPageIndex(2).setPageSize(3).build())).containsExactly("3-name", "4-name", "5-name"); } @Test public void select_groups_by_query_with_search_query() { GroupDto group = groupDb.insertGroup(newGroupDto().setName("group-anyone")); groupDb.insertGroup(newGroupDto().setName("unknown")); - permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, group.getId()); - PermissionQuery.Builder dbQuery = PermissionQuery.builder().setSearchQuery("any"); - List<String> groupNames = selectGroupNames(dbQuery); - int countNames = countGroupNames(dbQuery); - - assertThat(groupNames).containsOnlyOnce(DefaultGroups.ANYONE, "group-anyone"); - assertThat(countNames).isEqualTo(2); + assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, + PermissionQuery.builder().setSearchQuery("any").build())).containsOnlyOnce(ANYONE, "group-anyone"); } @Test public void select_groups_by_query_does_not_return_anyone_when_group_roles_is_empty() { GroupDto group = groupDb.insertGroup(); - PermissionQuery.Builder dbQuery = PermissionQuery.builder(); - List<String> groupNames = selectGroupNames(dbQuery); - int countNames = countGroupNames(dbQuery); - - assertThat(groupNames) - .doesNotContain(DefaultGroups.ANYONE) - .containsExactly(group.getName()); - assertThat(countNames).isEqualTo(1); + assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, + PermissionQuery.builder().build())) + .doesNotContain(ANYONE) + .containsExactly(group.getName()); } - private List<String> selectGroupNames(PermissionQuery.Builder dbQuery) { - return underTest.selectGroupNamesByPermissionQuery(dbSession, dbQuery.build()); - } + @Test + public void select_group_permissions_by_group_names_on_global_permissions() { + GroupDto group1 = groupDb.insertGroup(newGroupDto().setName("Group-1")); + permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, group1.getId()); + + GroupDto group2 = groupDb.insertGroup(newGroupDto().setName("Group-2")); + ComponentDto project = componentDb.insertComponent(newProjectDto()); + permissionDb.addProjectPermissionToGroup(UserRole.ADMIN, group2.getId(), project.getId()); + + GroupDto group3 = groupDb.insertGroup(newGroupDto().setName("Group-3")); + permissionDb.addGlobalPermissionToGroup(SYSTEM_ADMIN, group3.getId()); - private int countGroupNames(PermissionQuery.Builder dbQuery) { - return underTest.countGroupsByPermissionQuery(dbSession, dbQuery.build()); + // Anyone + permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, null); + permissionDb.addGlobalPermissionToGroup(PROVISIONING, null); + + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1"), null)) + .extracting(GroupRoleDto::getGroupId, GroupRoleDto::getRole, GroupRoleDto::getResourceId) + .containsOnly(tuple(group1.getId(), SCAN_EXECUTION, null)); + + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-2"), null)).isEmpty(); + + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-3"), null)) + .extracting(GroupRoleDto::getGroupId, GroupRoleDto::getRole, GroupRoleDto::getResourceId) + .containsOnly(tuple(group3.getId(), SYSTEM_ADMIN, null)); + + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Anyone"), null)) + .extracting(GroupRoleDto::getGroupId, GroupRoleDto::getRole, GroupRoleDto::getResourceId) + .containsOnly( + tuple(0L, SCAN_EXECUTION, null), + tuple(0L, PROVISIONING, null)); + + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1", "Group-2", "Anyone"), null)).hasSize(3); + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Unknown"), null)).isEmpty(); + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, Collections.emptyList(), null)).isEmpty(); } - private List<GroupRoleDto> selectGroupPermissions(PermissionQuery.Builder dbQuery) { - return underTest.selectGroupPermissionsByQuery(dbSession, dbQuery.build()); + @Test + public void select_group_permissions_by_group_names_on_project_permissions() { + GroupDto group1 = groupDb.insertGroup(newGroupDto().setName("Group-1")); + permissionDb.addGlobalPermissionToGroup(PROVISIONING, group1.getId()); + + GroupDto group2 = groupDb.insertGroup(newGroupDto().setName("Group-2")); + ComponentDto project = componentDb.insertComponent(newProjectDto()); + permissionDb.addProjectPermissionToGroup(USER, group2.getId(), project.getId()); + + GroupDto group3 = groupDb.insertGroup(newGroupDto().setName("Group-3")); + permissionDb.addProjectPermissionToGroup(USER, group3.getId(), project.getId()); + + // Anyone group + permissionDb.addGlobalPermissionToGroup(SCAN_EXECUTION, null); + permissionDb.addProjectPermissionToGroup(PROVISIONING, null, project.getId()); + + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1"), project.getId())).isEmpty(); + + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-2"), project.getId())) + .extracting(GroupRoleDto::getGroupId, GroupRoleDto::getRole, GroupRoleDto::getResourceId) + .containsOnly(tuple(group2.getId(), USER, project.getId())); + + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-3"), project.getId())) + .extracting(GroupRoleDto::getGroupId, GroupRoleDto::getRole, GroupRoleDto::getResourceId) + .containsOnly(tuple(group3.getId(), USER, project.getId())); + + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Anyone"), project.getId())) + .extracting(GroupRoleDto::getGroupId, GroupRoleDto::getRole, GroupRoleDto::getResourceId) + .containsOnly(tuple(0L, PROVISIONING, project.getId())); + + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1", "Group-2", "Anyone"), project.getId())).hasSize(2); + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Unknown"), project.getId())).isEmpty(); + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1"), 123L)).isEmpty(); + assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, Collections.emptyList(), project.getId())).isEmpty(); } + } diff --git a/sonar-db/src/test/java/org/sonar/db/permission/UserWithPermissionDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/UserWithPermissionDaoTest.java index 1ac2d220287..a6963d1a349 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/UserWithPermissionDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/UserWithPermissionDaoTest.java @@ -20,6 +20,7 @@ package org.sonar.db.permission; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -35,6 +36,7 @@ import org.sonar.db.user.UserDto; import org.sonar.db.user.UserPermissionDto; import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; import static org.sonar.api.web.UserRole.ADMIN; @@ -197,26 +199,55 @@ public class UserWithPermissionDaoTest { } @Test - public void select_user_permissions_by_logins() { + public void select_user_permissions_by_logins_with_global_permissions() { UserDto firstUser = userDb.insertUser(newUserDto()); + permissionDb.addGlobalPermissionToUser(ADMIN, firstUser.getId()); + UserDto secondUser = userDb.insertUser(newUserDto()); + permissionDb.addGlobalPermissionToUser(USER, secondUser.getId()); + UserDto thirdUser = userDb.insertUser(newUserDto()); + ComponentDto project = componentDb.insertComponent(newProjectDto()); + permissionDb.addProjectPermissionToUser(ADMIN, thirdUser.getId(), project.getId()); + assertThat(underTest.selectUserPermissionsByLoginsAnProject(session, (asList(firstUser.getLogin(), secondUser.getLogin(), thirdUser.getLogin())), null)) + .extracting(UserPermissionDto::getUserId, UserPermissionDto::getPermission, UserPermissionDto::getComponentId) + .containsOnly( + tuple(firstUser.getId(), ADMIN, null), + tuple(secondUser.getId(), USER, null)); + + assertThat(underTest.selectUserPermissionsByLoginsAnProject(session, singletonList(thirdUser.getLogin()), null)).isEmpty(); + assertThat(underTest.selectUserPermissionsByLoginsAnProject(session, singletonList("unknown"), null)).isEmpty(); + assertThat(underTest.selectUserPermissionsByLoginsAnProject(session, Collections.emptyList(), null)).isEmpty(); + } + + @Test + public void select_user_permissions_by_logins_with_project_permissions() { + UserDto firstUser = userDb.insertUser(newUserDto()); ComponentDto project = componentDb.insertComponent(newProjectDto()); permissionDb.addProjectPermissionToUser(ADMIN, firstUser.getId(), project.getId()); + + UserDto secondUser = userDb.insertUser(newUserDto()); permissionDb.addProjectPermissionToUser(USER, secondUser.getId(), project.getId()); + UserDto thirdUser = userDb.insertUser(newUserDto()); ComponentDto anotherProject = componentDb.insertComponent(newProjectDto()); permissionDb.addProjectPermissionToUser(ADMIN, thirdUser.getId(), anotherProject.getId()); - List<UserPermissionDto> result = underTest.selectUserPermissionsByLogins(session, (asList(firstUser.getLogin(), secondUser.getLogin(), thirdUser.getLogin()))); - - assertThat(result) + assertThat(underTest.selectUserPermissionsByLoginsAnProject(session, (asList(firstUser.getLogin(), secondUser.getLogin(), thirdUser.getLogin())), project.getId())) .extracting(UserPermissionDto::getUserId, UserPermissionDto::getPermission, UserPermissionDto::getComponentId) .containsOnly( tuple(firstUser.getId(), ADMIN, project.getId()), - tuple(secondUser.getId(), USER, project.getId()), + tuple(secondUser.getId(), USER, project.getId())); + + assertThat(underTest.selectUserPermissionsByLoginsAnProject(session, (asList(firstUser.getLogin(), secondUser.getLogin(), thirdUser.getLogin())), anotherProject.getId())) + .extracting(UserPermissionDto::getUserId, UserPermissionDto::getPermission, UserPermissionDto::getComponentId) + .containsOnly( tuple(thirdUser.getId(), ADMIN, anotherProject.getId())); + + assertThat(underTest.selectUserPermissionsByLoginsAnProject(session, singletonList(thirdUser.getLogin()), project.getId())).isEmpty(); + assertThat(underTest.selectUserPermissionsByLoginsAnProject(session, singletonList("unknown"), project.getId())).isEmpty(); + assertThat(underTest.selectUserPermissionsByLoginsAnProject(session, Collections.emptyList(), project.getId())).isEmpty(); } @Test |