From: Simon Brandhof Date: Tue, 18 Oct 2016 11:57:08 +0000 (+0200) Subject: SONAR-8261 Support organizations in web service api/permissions/groups X-Git-Tag: 6.2-RC1~337 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1e663e71492dc7a9a1d9bd1a33e22b34545748e0;p=sonarqube.git SONAR-8261 Support organizations in web service api/permissions/groups --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/authentication/UserIdentityAuthenticator.java b/server/sonar-server/src/main/java/org/sonar/server/authentication/UserIdentityAuthenticator.java index 1653c01626e..53b9dcd309c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/authentication/UserIdentityAuthenticator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/authentication/UserIdentityAuthenticator.java @@ -37,6 +37,7 @@ import org.sonar.db.DbSession; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.db.user.UserGroupDto; +import org.sonar.server.organization.DefaultOrganization; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.user.ExternalIdentity; import org.sonar.server.user.NewUser; @@ -124,7 +125,8 @@ public class UserIdentityAuthenticator { Collection groupsToRemove = Sets.difference(userGroups, identityGroups); Collection allGroups = new ArrayList<>(groupsToAdd); allGroups.addAll(groupsToRemove); - Map groupsByName = from(dbClient.groupDao().selectByNames(dbSession, allGroups)).uniqueIndex(GroupDtoToName.INSTANCE); + DefaultOrganization defaultOrganization = defaultOrganizationProvider.get(); + Map groupsByName = from(dbClient.groupDao().selectByNames(dbSession, defaultOrganization.getUuid(), allGroups)).uniqueIndex(GroupDtoToName.INSTANCE); addGroups(dbSession, userDto, groupsToAdd, groupsByName); removeGroups(dbSession, userDto, groupsToRemove, groupsByName); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java index 49a8715ec50..0f1ea915a43 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/GroupsAction.java @@ -25,15 +25,16 @@ import com.google.common.collect.TreeMultimap; import com.google.common.io.Resources; import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; import org.sonar.api.security.DefaultGroups; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.utils.Paging; +import org.sonar.core.util.stream.Collectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.organization.OrganizationDto; import org.sonar.db.permission.GroupPermissionDto; import org.sonar.db.permission.PermissionQuery; import org.sonar.db.user.GroupDto; @@ -47,9 +48,11 @@ import static org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE; import static org.sonar.db.permission.PermissionQuery.RESULTS_MAX_SIZE; import static org.sonar.db.permission.PermissionQuery.SEARCH_QUERY_MIN_LENGTH; import static org.sonar.server.permission.PermissionPrivilegeChecker.checkProjectAdmin; +import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createOrganizationParameter; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createPermissionParameter; import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createProjectParameters; import static org.sonar.server.ws.WsUtils.writeProtobuf; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY; import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION; public class GroupsAction implements PermissionsWsAction { @@ -78,7 +81,7 @@ public class GroupsAction implements PermissionsWsAction { .setResponseExample(Resources.getResource(getClass(), "groups-example.json")) .setHandler(this); - // TODO add orgKey + createOrganizationParameter(action); createPermissionParameter(action).setRequired(false); createProjectParameters(action); } @@ -86,14 +89,15 @@ public class GroupsAction implements PermissionsWsAction { @Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { + OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION_KEY)); Optional projectId = support.findProject(dbSession, request); - checkProjectAdmin(userSession, projectId); + checkProjectAdmin(userSession, org.getUuid(), projectId); PermissionQuery query = buildPermissionQuery(request, projectId); // TODO validatePermission(groupsRequest.getPermission(), wsProjectRef); - List groups = findGroups(dbSession, query); - int total = dbClient.groupPermissionDao().countGroupsByPermissionQuery(dbSession, query); - List groupsWithPermission = findGroupPermissions(dbSession, groups, projectId); + List groups = findGroups(dbSession, org, query); + int total = dbClient.groupPermissionDao().countGroupsByQuery(dbSession, org.getUuid(), query); + List groupsWithPermission = findGroupPermissions(dbSession, org, groups, projectId); Paging paging = Paging.forPageIndex(request.mandatoryParamAsInt(Param.PAGE)).withPageSize(query.getPageSize()).andTotal(total); WsGroupsResponse groupsResponse = buildResponse(groups, groupsWithPermission, paging); writeProtobuf(groupsResponse, request, response); @@ -141,22 +145,20 @@ public class GroupsAction implements PermissionsWsAction { return response.build(); } - private List findGroups(DbSession dbSession, PermissionQuery dbQuery) { - // TODO support organizations - List orderedNames = dbClient.groupPermissionDao().selectGroupNamesByPermissionQuery(dbSession, dbQuery); - List groups = dbClient.groupDao().selectByNames(dbSession, orderedNames); + private List findGroups(DbSession dbSession, OrganizationDto org, PermissionQuery dbQuery) { + List orderedNames = dbClient.groupPermissionDao().selectGroupNamesByQuery(dbSession, org.getUuid(), dbQuery); + List groups = dbClient.groupDao().selectByNames(dbSession, org.getUuid(), orderedNames); if (orderedNames.contains(DefaultGroups.ANYONE)) { - groups.add(0, new GroupDto().setId(0L).setName(DefaultGroups.ANYONE)); + groups.add(0, new GroupDto().setId(0L).setName(DefaultGroups.ANYONE).setOrganizationUuid(org.getUuid())); } return Ordering.explicit(orderedNames).onResultOf(GroupDto::getName).immutableSortedCopy(groups); } - private List findGroupPermissions(DbSession dbSession, List groups, Optional project) { + private List findGroupPermissions(DbSession dbSession, OrganizationDto org, List groups, Optional project) { if (groups.isEmpty()) { return emptyList(); } - // TODO use groupId - List names = groups.stream().map(GroupDto::getName).collect(Collectors.toList()); - return dbClient.groupPermissionDao().selectGroupPermissionsByGroupNamesAndProject(dbSession, names, project.isPresent() ? project.get().getId() : null); + List ids = groups.stream().map(GroupDto::getId).collect(Collectors.toList(groups.size())); + return dbClient.groupPermissionDao().selectByGroupIds(dbSession, org.getUuid(), ids, project.isPresent() ? project.get().getId() : null); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/TemplateGroupsAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/TemplateGroupsAction.java index 3c2519050b1..77c4c25b228 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/TemplateGroupsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/TemplateGroupsAction.java @@ -143,7 +143,7 @@ public class TemplateGroupsAction implements PermissionsWsAction { private List findGroups(DbSession dbSession, PermissionQuery dbQuery, PermissionTemplateDto template) { List orderedNames = dbClient.permissionTemplateDao().selectGroupNamesByQueryAndTemplate(dbSession, dbQuery, template.getId()); - List groups = dbClient.groupDao().selectByNames(dbSession, orderedNames); + List groups = dbClient.groupDao().selectByNames(dbSession, template.getOrganizationUuid(), orderedNames); if (orderedNames.contains(DefaultGroups.ANYONE)) { groups.add(0, new GroupDto().setId(0L).setName(DefaultGroups.ANYONE)); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/UserPermissionChangerTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/UserPermissionChangerTest.java index 19fb94398fb..441d16a1b7f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/UserPermissionChangerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/UserPermissionChangerTest.java @@ -27,10 +27,11 @@ import org.sonar.api.utils.System2; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.organization.OrganizationDto; -import org.sonar.db.organization.OrganizationTesting; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.server.exceptions.BadRequestException; +import org.sonar.server.organization.DefaultOrganizationProvider; +import org.sonar.server.organization.TestDefaultOrganizationProvider; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.api.web.UserRole.ISSUE_ADMIN; @@ -50,7 +51,8 @@ public class UserPermissionChangerTest { @Rule public ExpectedException expectedException = ExpectedException.none(); - private UserPermissionChanger underTest = new UserPermissionChanger(db.getDbClient()); + private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); + private UserPermissionChanger underTest = new UserPermissionChanger(db.getDbClient(), defaultOrganizationProvider); private OrganizationDto org1; private OrganizationDto org2; private UserDto user1; @@ -59,8 +61,8 @@ public class UserPermissionChangerTest { @Before public void setUp() throws Exception { - org1 = OrganizationTesting.insert(db, newOrganizationDto()); - org2 = OrganizationTesting.insert(db, newOrganizationDto()); + org1 = db.organizations().insert(newOrganizationDto()); + org2 = db.organizations().insert(newOrganizationDto()); user1 = db.users().insertUser(); user2 = db.users().insertUser(); project = db.components().insertProject(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/GroupsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/GroupsActionTest.java index 400a9c44d3e..aa03a2523c2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/GroupsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/GroupsActionTest.java @@ -74,7 +74,7 @@ public class GroupsActionTest extends BasePermissionWsTest { @Test public void search_for_groups_with_one_permission() throws Exception { - loginAsAdmin(); + loginAsAdminOnDefaultOrganization(); newRequest() .setParam(PARAM_PERMISSION, SCAN_EXECUTION) .execute() @@ -111,7 +111,7 @@ public class GroupsActionTest extends BasePermissionWsTest { @Test public void search_with_selection() throws Exception { - loginAsAdmin(); + loginAsAdminOnDefaultOrganization(); String result = newRequest() .setParam(PARAM_PERMISSION, SCAN_EXECUTION) .execute() @@ -122,7 +122,7 @@ public class GroupsActionTest extends BasePermissionWsTest { @Test public void search_groups_with_pagination() throws Exception { - loginAsAdmin(); + loginAsAdminOnDefaultOrganization(); String result = newRequest() .setParam(PARAM_PERMISSION, SCAN_EXECUTION) .setParam(PAGE_SIZE, "1") @@ -137,7 +137,7 @@ public class GroupsActionTest extends BasePermissionWsTest { @Test public void search_groups_with_query() throws Exception { - loginAsAdmin(); + loginAsAdminOnDefaultOrganization(); String result = newRequest() .setParam(PARAM_PERMISSION, SCAN_EXECUTION) .setParam(TEXT_QUERY, "group-") @@ -182,7 +182,7 @@ public class GroupsActionTest extends BasePermissionWsTest { GroupDto groupWithoutPermission = db.users().insertGroup(db.getDefaultOrganization(), "group-without-permission"); GroupDto anotherGroup = db.users().insertGroup(db.getDefaultOrganization(), "another-group"); - loginAsAdmin(); + loginAsAdminOnDefaultOrganization(); String result = newRequest() .setParam(PARAM_PERMISSION, ISSUE_ADMIN) .setParam(PARAM_PROJECT_ID, "project-uuid") @@ -203,7 +203,7 @@ public class GroupsActionTest extends BasePermissionWsTest { GroupDto groupWithoutPermission = db.users().insertGroup(db.getDefaultOrganization(), "group-without-permission"); - loginAsAdmin(); + loginAsAdminOnDefaultOrganization(); String result = newRequest() .setParam(PARAM_PERMISSION, ISSUE_ADMIN) .setParam(PARAM_PROJECT_ID, project.uuid()) @@ -220,7 +220,7 @@ public class GroupsActionTest extends BasePermissionWsTest { GroupDto group = db.users().insertGroup(db.getDefaultOrganization(), "group-with-permission"); db.users().insertProjectPermissionOnGroup(group, ISSUE_ADMIN, project); - loginAsAdmin(); + loginAsAdminOnDefaultOrganization(); String result = newRequest() .setParam(PARAM_PROJECT_ID, project.uuid()) .setParam(TEXT_QUERY, "nyo") @@ -236,7 +236,7 @@ public class GroupsActionTest extends BasePermissionWsTest { GroupDto group = db.users().insertGroup(db.getDefaultOrganization(), "project-group-name"); db.users().insertProjectPermissionOnGroup(group, ISSUE_ADMIN, view); - loginAsAdmin(); + loginAsAdminOnDefaultOrganization(); String result = newRequest() .setParam(PARAM_PERMISSION, ISSUE_ADMIN) .setParam(PARAM_PROJECT_ID, "view-uuid") @@ -275,7 +275,7 @@ public class GroupsActionTest extends BasePermissionWsTest { expectedException.expect(BadRequestException.class); - loginAsAdmin(); + loginAsAdminOnDefaultOrganization(); newRequest() .setParam(PARAM_PERMISSION, SCAN_EXECUTION) .setParam(PARAM_PROJECT_ID, "project-uuid") @@ -286,8 +286,4 @@ public class GroupsActionTest extends BasePermissionWsTest { private WsTester.TestRequest newRequest() { return wsTester.newPostRequest(CONTROLLER, "groups"); } - - private void loginAsAdmin() { - userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN); - } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java index 9a216e68b7a..aa115e7e94b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java @@ -239,7 +239,7 @@ public class ApplyTemplateActionTest extends BasePermissionWsTest selectProjectPermissionGroups(ComponentDto project, String permission) { PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build(); - return db.getDbClient().groupPermissionDao().selectGroupNamesByPermissionQuery(db.getSession(), query); + return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), db.getDefaultOrganization().getUuid(), query); } private List selectProjectPermissionUsers(ComponentDto project, String permission) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/BulkApplyTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/BulkApplyTemplateActionTest.java index cec65a28e1b..d4210ba743a 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/BulkApplyTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/BulkApplyTemplateActionTest.java @@ -208,7 +208,7 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest selectProjectPermissionGroups(ComponentDto project, String permission) { PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build(); - return db.getDbClient().groupPermissionDao().selectGroupNamesByPermissionQuery(db.getSession(), query); + return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), db.getDefaultOrganization().getUuid(), query); } private List selectProjectPermissionUsers(ComponentDto project, String permission) { diff --git a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java index 18bff43b55e..2fb928549ab 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionDao.java @@ -52,27 +52,17 @@ public class GroupPermissionDao implements Dao { /** * @return group names, sorted in alphabetical order - * @deprecated not compatible with organizations. */ - @Deprecated - public List selectGroupNamesByPermissionQuery(DbSession dbSession, PermissionQuery query) { - return mapper(dbSession).selectGroupNamesByPermissionQuery(query, new RowBounds(query.getPageOffset(), query.getPageSize())); + public List selectGroupNamesByQuery(DbSession dbSession, String organizationUuid, PermissionQuery query) { + return mapper(dbSession).selectGroupNamesByQuery(organizationUuid, query, new RowBounds(query.getPageOffset(), query.getPageSize())); } - /** - * @deprecated not compatible with organizations. - */ - @Deprecated - public int countGroupsByPermissionQuery(DbSession dbSession, PermissionQuery query) { - return mapper(dbSession).countGroupsByPermissionQuery(query); + public int countGroupsByQuery(DbSession dbSession, String organizationUuid, PermissionQuery query) { + return mapper(dbSession).countGroupsByQuery(organizationUuid, query); } - /** - * @deprecated group name parameter is not enough to identify a group. It is not compatible with organizations. - */ - @Deprecated - public List selectGroupPermissionsByGroupNamesAndProject(DbSession dbSession, List groupNames, @Nullable Long projectId) { - return executeLargeInputs(groupNames, groups -> mapper(dbSession).selectGroupPermissionByGroupNames(groups, projectId)); + public List selectByGroupIds(DbSession dbSession, String organizationUuid, List groupIds, @Nullable Long projectId) { + return executeLargeInputs(groupIds, groups -> mapper(dbSession).selectByGroupIds(organizationUuid, groups, projectId)); } /** @@ -99,7 +89,6 @@ public class GroupPermissionDao implements Dao { return mapper(session).selectGlobalPermissionsOfGroup(organizationUuid, groupId); } - /** * Selects the permissions granted to group and project. An empty list is returned if the * group or project do not exist. diff --git a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java index 28c9af27a76..7bd77e82349 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/GroupPermissionMapper.java @@ -34,23 +34,11 @@ public interface GroupPermissionMapper { @Deprecated int countGroups(Map parameters); - /** - * @deprecated does not support organizations - */ - @Deprecated - List selectGroupNamesByPermissionQuery(@Param("query") PermissionQuery query, RowBounds rowBounds); + List selectGroupNamesByQuery(@Param("organizationUuid") String organizationUuid, @Param("query") PermissionQuery query, RowBounds rowBounds); - /** - * @deprecated does not support organizations - */ - @Deprecated - int countGroupsByPermissionQuery(@Param("query") PermissionQuery query); + int countGroupsByQuery(@Param("organizationUuid") String organizationUuid, @Param("query") PermissionQuery query); - /** - * @deprecated does not support organizations - */ - @Deprecated - List selectGroupPermissionByGroupNames(@Param("groupNames") List groupNames, @Nullable @Param("projectId") Long projectId); + List selectByGroupIds(@Param("organizationUuid") String organizationUuid, @Param("groupIds") List groupIds, @Nullable @Param("projectId") Long projectId); void groupsCountByProjectIdAndPermission(Map parameters, ResultHandler resultHandler); diff --git a/sonar-db/src/main/java/org/sonar/db/user/GroupDao.java b/sonar-db/src/main/java/org/sonar/db/user/GroupDao.java index 524597f270b..9a0d2824e08 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/GroupDao.java +++ b/sonar-db/src/main/java/org/sonar/db/user/GroupDao.java @@ -63,12 +63,8 @@ public class GroupDao implements Dao { return Optional.ofNullable(mapper(dbSession).selectByName(organizationUuid, name)); } - /** - * @deprecated organization should be added as a parameter - */ - @Deprecated - public List selectByNames(DbSession session, Collection names) { - return executeLargeInputs(names, mapper(session)::selectByNames); + public List selectByNames(DbSession dbSession, String organizationUuid, Collection names) { + return executeLargeInputs(names, pageOfNames -> mapper(dbSession).selectByNames(organizationUuid, pageOfNames)); } @CheckForNull @@ -76,6 +72,10 @@ public class GroupDao implements Dao { return mapper(dbSession).selectById(groupId); } + public List selectByIds(DbSession dbSession, List ids) { + return executeLargeInputs(ids, mapper(dbSession)::selectByIds); + } + public void deleteById(DbSession dbSession, long groupId) { mapper(dbSession).deleteById(groupId); } diff --git a/sonar-db/src/main/java/org/sonar/db/user/GroupMapper.java b/sonar-db/src/main/java/org/sonar/db/user/GroupMapper.java index ddf3097edb1..3d258876b9b 100644 --- a/sonar-db/src/main/java/org/sonar/db/user/GroupMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/user/GroupMapper.java @@ -35,7 +35,7 @@ public interface GroupMapper { List selectByUserLogin(String userLogin); - List selectByNames(@Param("names") List names); + List selectByNames(@Param("organizationUuid") String organizationUuid, @Param("names") List names); void insert(GroupDto groupDto); @@ -59,4 +59,6 @@ public interface GroupMapper { int updateNonRootUsersOfGroup(@Param("groupId") long groupId, @Param("defaultOrganizationUuid") String defaultOrganizationUuid, @Param("now") long now); + + List selectByIds(@Param("ids") List ids); } diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml index fc3c7b8d2ea..56f22ea1954 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/GroupPermissionMapper.xml @@ -53,74 +53,87 @@ ) groups - + select distinct sub.name, lower(sub.name), sub.groupId - order by lower(gr.name), gr.name, gr.group_id + order by lower(sub.name), sub.name, sub.groupId - select count(1) from ( - select distinct gr.group_id + select distinct sub.groupId ) g 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 - from groups g + select g.id as groupId, g.name as name, gr.role as permission, gr.resource_id as componentId, gr.id as id + from groups g left join group_roles gr on g.id = gr.group_id - UNION ALL - select 0 as group_id, 'Anyone' as name, gr.role as permission, gr.resource_id as resource_id, gr.id as id + where + g.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} + + union all + + select 0 as groupId, 'Anyone' as name, gr.role as permission, gr.resource_id as componentId, gr.id as id from group_roles gr - where gr.group_id is null + where + gr.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and + gr.group_id is null - ) gr - left join projects p on gr.resource_id = p.id + + ) sub + left join projects p on sub.componentId = p.id - and lower(gr.name) like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/' + and lower(sub.name) like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/' - and gr.permission is not null + and sub.permission is not null - and gr.resource_id is null + and sub.componentId is null - and p.uuid=#{query.componentUuid,jdbcType=VARCHAR} + and p.uuid = #{query.componentUuid,jdbcType=VARCHAR} - and gr.permission=#{query.permission,jdbcType=VARCHAR} + and sub.permission = #{query.permission,jdbcType=VARCHAR} - + select sub.groupId as groupId, sub.componentId as resourceId, sub.permission as role, sub.organizationUuid as organizationUuid + 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 + select gr.group_id as groupId, gr.resource_id as componentId, gr.role as permission, g.name as name, gr.organization_uuid as organizationUuid + from group_roles gr + inner join groups g ON g.id = gr.group_id + where gr.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and + gr.group_id is not null + + union all + + select 0 as groupId, gr.resource_id as componentId, gr.role as permission, 'Anyone' as name, gr.organization_uuid as organizationUuid + from group_roles gr + where + gr.group_id is null and + gr.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} ) sub where - sub.name IN - - #{name} + sub.groupId in + + #{groupId,jdbcType=BIGINT} - - AND sub.resourceId=#{projectId,jdbcType=BIGINT} + + and sub.componentId=#{projectId,jdbcType=BIGINT} - AND sub.resourceId IS NULL + and sub.componentId is null diff --git a/sonar-db/src/main/resources/org/sonar/db/user/GroupMapper.xml b/sonar-db/src/main/resources/org/sonar/db/user/GroupMapper.xml index 2898980a517..45cfbc85797 100644 --- a/sonar-db/src/main/resources/org/sonar/db/user/GroupMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/user/GroupMapper.xml @@ -20,6 +20,16 @@ where g.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and g.name = #{name,jdbcType=VARCHAR} + + diff --git a/sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java index 535fd5bb5d2..35bc6077431 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/GroupPermissionDaoTest.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.stream.IntStream; +import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.System2; @@ -47,17 +48,27 @@ import static org.sonar.db.component.ComponentTesting.newProjectDto; import static org.sonar.db.user.GroupTesting.newGroupDto; public class GroupPermissionDaoTest { + + private static final long ANYONE_ID = 0L; + private static final long MISSING_ID = -1L; + @Rule public DbTester db = DbTester.create(System2.INSTANCE); private DbSession dbSession = db.getSession(); private GroupPermissionDao underTest = new GroupPermissionDao(); + private String defaultOrganizationUuid; + + @Before + public void setUp() throws Exception { + defaultOrganizationUuid = db.getDefaultOrganization().getUuid(); + } @Test public void group_count_by_permission_and_component_id() { - GroupDto group1 = db.users().insertGroup(newGroupDto()); - GroupDto group2 = db.users().insertGroup(newGroupDto()); - GroupDto group3 = db.users().insertGroup(newGroupDto()); + GroupDto group1 = db.users().insertGroup(); + GroupDto group2 = db.users().insertGroup(); + GroupDto group3 = db.users().insertGroup(); ComponentDto project1 = db.components().insertProject(); ComponentDto project2 = db.components().insertProject(); ComponentDto project3 = db.components().insertProject(); @@ -82,52 +93,41 @@ public class GroupPermissionDaoTest { } @Test - public void select_groups_by_query() { - GroupDto group1 = db.users().insertGroup(newGroupDto()); - GroupDto group2 = db.users().insertGroup(newGroupDto()); - GroupDto group3 = db.users().insertGroup(newGroupDto()); + public void selectGroupNamesByQuery_is_ordered_by_group_names() { + GroupDto group2 = addGroupOnDefaultOrganization("Group-2"); + GroupDto group3 = addGroupOnDefaultOrganization("Group-3"); + GroupDto group1 = addGroupOnDefaultOrganization("Group-1"); db.users().insertPermissionOnAnyone(SCAN_EXECUTION); - List groupNames = underTest.selectGroupNamesByPermissionQuery(dbSession, PermissionQuery.builder().build()); - assertThat(groupNames).containsOnly("Anyone", group1.getName(), group2.getName(), group3.getName()); + assertThat(underTest.selectGroupNamesByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().build())).containsExactly(ANYONE, group1.getName(), group2.getName(), group3.getName()); } @Test - public void select_groups_by_query_is_ordered_by_group_names() { - db.users().insertGroup(newGroupDto().setName("Group-2")); - db.users().insertGroup(newGroupDto().setName("Group-3")); - db.users().insertGroup(newGroupDto().setName("Group-1")); - db.users().insertPermissionOnAnyone(SCAN_EXECUTION); - - assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, - PermissionQuery.builder().build())).containsExactly("Anyone", "Group-1", "Group-2", "Group-3"); - } - - @Test - public void count_groups_by_query() { - 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")); + public void countGroupsByQuery() { + GroupDto group1 = addGroupOnDefaultOrganization("Group-1"); + addGroupOnDefaultOrganization("Group-2"); + addGroupOnDefaultOrganization("Group-3"); db.users().insertPermissionOnAnyone(SCAN_EXECUTION); db.users().insertPermissionOnGroup(group1, PROVISIONING); - 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().withAtLeastOnePermission().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); + assertThat(underTest.countGroupsByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().build())).isEqualTo(4); + assertThat(underTest.countGroupsByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().setPermission(PROVISIONING).build())).isEqualTo(1); + assertThat(underTest.countGroupsByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().withAtLeastOnePermission().build())).isEqualTo(2); + assertThat(underTest.countGroupsByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().setSearchQuery("Group-").build())).isEqualTo(3); + assertThat(underTest.countGroupsByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().setSearchQuery("Any").build())).isEqualTo(1); } @Test - public void select_groups_by_query_with_global_permission() { - 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")); + public void selectGroupNamesByQuery_with_global_permission() { + GroupDto group1 = addGroupOnDefaultOrganization("Group-1"); + GroupDto group2 = addGroupOnDefaultOrganization("Group-2"); + GroupDto group3 = addGroupOnDefaultOrganization("Group-3"); ComponentDto project = db.components().insertComponent(newProjectDto()); @@ -137,24 +137,24 @@ public class GroupPermissionDaoTest { db.users().insertPermissionOnGroup(group3, SYSTEM_ADMIN); db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, project); - assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, - PermissionQuery.builder().setPermission(SCAN_EXECUTION).build())).containsExactly(ANYONE, "Group-1"); + assertThat(underTest.selectGroupNamesByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().setPermission(SCAN_EXECUTION).build())).containsExactly(ANYONE, group1.getName()); - assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, - PermissionQuery.builder().setPermission(SYSTEM_ADMIN).build())).containsExactly("Group-3"); + assertThat(underTest.selectGroupNamesByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().setPermission(SYSTEM_ADMIN).build())).containsExactly(group3.getName()); - assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, - PermissionQuery.builder().setPermission(PROVISIONING).build())).containsExactly(ANYONE); + assertThat(underTest.selectGroupNamesByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().setPermission(PROVISIONING).build())).containsExactly(ANYONE); } @Test public void select_groups_by_query_with_project_permissions() { - GroupDto group1 = db.users().insertGroup(newGroupDto()); - GroupDto group2 = db.users().insertGroup(newGroupDto()); - GroupDto group3 = db.users().insertGroup(newGroupDto()); + GroupDto group1 = db.users().insertGroup(); + GroupDto group2 = db.users().insertGroup(); + GroupDto group3 = db.users().insertGroup(); - ComponentDto project = db.components().insertComponent(newProjectDto()); - ComponentDto anotherProject = db.components().insertComponent(newProjectDto()); + ComponentDto project = db.components().insertProject(); + ComponentDto anotherProject = db.components().insertProject(); db.users().insertProjectPermissionOnGroup(group1, SCAN_EXECUTION, project); db.users().insertProjectPermissionOnGroup(group1, PROVISIONING, project); @@ -166,113 +166,114 @@ public class GroupPermissionDaoTest { db.users().insertPermissionOnGroup(group2, SCAN_EXECUTION); PermissionQuery.Builder builderOnComponent = PermissionQuery.builder().setComponentUuid(project.uuid()); - assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, - builderOnComponent.withAtLeastOnePermission().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); + assertThat(underTest.selectGroupNamesByQuery(dbSession, + defaultOrganizationUuid, builderOnComponent.withAtLeastOnePermission().build())).containsOnlyOnce(group1.getName()); + assertThat(underTest.selectGroupNamesByQuery(dbSession, + defaultOrganizationUuid, builderOnComponent.setPermission(SCAN_EXECUTION).build())).containsOnlyOnce(group1.getName()); + assertThat(underTest.selectGroupNamesByQuery(dbSession, + defaultOrganizationUuid, builderOnComponent.setPermission(USER).build())).containsOnlyOnce(ANYONE); } @Test - public void select_groups_by_query_paginated() { - IntStream.rangeClosed(0, 9).forEach(i -> db.users().insertGroup(newGroupDto().setName(i + "-name"))); + public void selectGroupNamesByQuery_is_paginated() { + IntStream.rangeClosed(0, 9).forEach(i -> addGroupOnDefaultOrganization(i + "-name")); - assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, - PermissionQuery.builder().setPageIndex(2).setPageSize(3).build())).containsExactly("3-name", "4-name", "5-name"); + List groupNames = underTest.selectGroupNamesByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().setPageIndex(2).setPageSize(3).build()); + assertThat(groupNames).containsExactly("3-name", "4-name", "5-name"); } @Test - public void select_groups_by_query_with_search_query() { - GroupDto group = db.users().insertGroup(newGroupDto().setName("group-anyone")); - db.users().insertGroup(newGroupDto().setName("unknown")); + public void selectGroupNamesByQuery_with_search_query() { + GroupDto group = addGroupOnDefaultOrganization("group-anyone"); + addGroupOnDefaultOrganization("unknown"); db.users().insertPermissionOnGroup(group, SCAN_EXECUTION); - assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, - PermissionQuery.builder().setSearchQuery("any").build())).containsOnlyOnce(ANYONE, "group-anyone"); + assertThat(underTest.selectGroupNamesByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().setSearchQuery("any").build())).containsOnlyOnce(ANYONE, group.getName()); } @Test - public void select_groups_by_query_does_not_return_anyone_when_group_roles_is_empty() { - GroupDto group = db.users().insertGroup(newGroupDto()); + public void selectGroupNamesByQuery_does_not_return_anyone_when_group_roles_is_empty() { + GroupDto group = db.users().insertGroup(); - assertThat(underTest.selectGroupNamesByPermissionQuery(dbSession, - PermissionQuery.builder().build())) + assertThat(underTest.selectGroupNamesByQuery(dbSession, + defaultOrganizationUuid, PermissionQuery.builder().build())) .doesNotContain(ANYONE) .containsExactly(group.getName()); } @Test - public void select_group_permissions_by_group_names_on_global_permissions() { - GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1")); + public void selectByGroupIds_on_global_permissions() { + GroupDto group1 = addGroupOnDefaultOrganization("Group-1"); db.users().insertPermissionOnGroup(group1, SCAN_EXECUTION); - GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2")); + GroupDto group2 = addGroupOnDefaultOrganization("Group-2"); ComponentDto project = db.components().insertComponent(newProjectDto()); db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, project); - GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3")); + GroupDto group3 = addGroupOnDefaultOrganization("Group-3"); db.users().insertPermissionOnGroup(group3, SYSTEM_ADMIN); // Anyone db.users().insertPermissionOnAnyone(SCAN_EXECUTION); db.users().insertPermissionOnAnyone(PROVISIONING); - assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1"), null)) + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group1.getId()), null)) .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::getResourceId) .containsOnly(tuple(group1.getId(), SCAN_EXECUTION, null)); - assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-2"), null)).isEmpty(); + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group2.getId()), null)).isEmpty(); - assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-3"), null)) + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group3.getId()), null)) .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::getResourceId) .containsOnly(tuple(group3.getId(), SYSTEM_ADMIN, null)); - assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Anyone"), null)) + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(ANYONE_ID), null)) .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::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(); + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group1.getId(), group2.getId(), ANYONE_ID), null)).hasSize(3); + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(MISSING_ID), null)).isEmpty(); + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, Collections.emptyList(), null)).isEmpty(); } @Test - public void select_group_permissions_by_group_names_on_project_permissions() { - GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1")); + public void selectByGroupIds_on_projects() { + GroupDto group1 = addGroupOnDefaultOrganization("Group-1"); db.users().insertPermissionOnGroup(group1, PROVISIONING); - GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2")); + GroupDto group2 = addGroupOnDefaultOrganization("Group-2"); ComponentDto project = db.components().insertComponent(newProjectDto()); db.users().insertProjectPermissionOnGroup(group2, USER, project); - GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3")); + GroupDto group3 = addGroupOnDefaultOrganization("Group-3"); db.users().insertProjectPermissionOnGroup(group3, USER, project); // Anyone group db.users().insertPermissionOnAnyone(SCAN_EXECUTION); db.users().insertProjectPermissionOnAnyone(PROVISIONING, project); - assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-1"), project.getId())).isEmpty(); + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group1.getId()), project.getId())).isEmpty(); - assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-2"), project.getId())) + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group2.getId()), project.getId())) .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::getResourceId) .containsOnly(tuple(group2.getId(), USER, project.getId())); - assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Group-3"), project.getId())) + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group3.getId()), project.getId())) .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::getResourceId) .containsOnly(tuple(group3.getId(), USER, project.getId())); - assertThat(underTest.selectGroupPermissionsByGroupNamesAndProject(dbSession, asList("Anyone"), project.getId())) + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(ANYONE_ID), project.getId())) .extracting(GroupPermissionDto::getGroupId, GroupPermissionDto::getRole, GroupPermissionDto::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(); + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group1.getId(), group2.getId(), ANYONE_ID), project.getId())).hasSize(2); + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(MISSING_ID), project.getId())).isEmpty(); + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, asList(group1.getId()), 123L)).isEmpty(); + assertThat(underTest.selectByGroupIds(dbSession, defaultOrganizationUuid, Collections.emptyList(), project.getId())).isEmpty(); } @Test @@ -435,4 +436,8 @@ public class GroupPermissionDaoTest { private void assertThatNoPermission(String permission) { assertThat(db.countSql("select count(id) from group_roles where role='" + permission + "'")).isEqualTo(0); } + + private GroupDto addGroupOnDefaultOrganization(String name) { + return db.users().insertGroup(db.getDefaultOrganization(), name); + } } diff --git a/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java b/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java index 4210dd4b5a0..44ca8844390 100644 --- a/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/permission/UserPermissionDaoTest.java @@ -33,7 +33,6 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.organization.OrganizationDto; -import org.sonar.db.organization.OrganizationTesting; import org.sonar.db.user.UserDto; import static java.util.Arrays.asList; @@ -295,7 +294,7 @@ public class UserPermissionDaoTest { @Test public void selectGlobalPermissionsOfUser() { - OrganizationDto org = OrganizationTesting.insert(dbTester, newOrganizationDto()); + OrganizationDto org = dbTester.organizations().insert(newOrganizationDto()); addGlobalPermissionOnDefaultOrganization("perm1", user1); addGlobalPermissionOnDefaultOrganization("perm2", user2); addGlobalPermission(org, "perm3", user1); @@ -310,7 +309,7 @@ public class UserPermissionDaoTest { @Test public void selectProjectPermissionsOfUser() { - OrganizationDto org = OrganizationTesting.insert(dbTester, newOrganizationDto()); + OrganizationDto org = dbTester.organizations().insert(newOrganizationDto()); ComponentDto project3 = dbTester.components().insertProject(); addGlobalPermission(org, "perm1", user1); addProjectPermission(org, "perm2", user1, project1); diff --git a/sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java index 3a8db595d9f..99ee0be3115 100644 --- a/sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/user/GroupDaoTest.java @@ -33,7 +33,6 @@ import org.sonar.db.organization.OrganizationDto; import static java.util.Arrays.asList; import static java.util.Arrays.stream; -import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -43,6 +42,7 @@ import static org.sonar.db.user.GroupTesting.newGroupDto; public class GroupDaoTest { private static final long NOW = 1_500_000L; + private static final long MISSING_ID = -1L; private static final OrganizationDto AN_ORGANIZATION = new OrganizationDto() .setKey("an-org") .setName("An Org") @@ -54,9 +54,8 @@ public class GroupDaoTest { @Rule public DbTester db = DbTester.create(system2); - - private final DbSession dbSession = db.getSession(); - private GroupDao underTest = new GroupDao(system2); + private DbSession dbSession = db.getSession(); + private GroupDao underTest = db.getDbClient().groupDao(); // not static as group id is changed in each test private final GroupDto aGroup = new GroupDto() @@ -101,15 +100,32 @@ public class GroupDaoTest { @Test public void selectByNames() { - underTest.insert(dbSession, newGroupDto().setName("group1")); - underTest.insert(dbSession, newGroupDto().setName("group2")); - underTest.insert(dbSession, newGroupDto().setName("group3")); + GroupDto group1InOrg1 = underTest.insert(dbSession, newGroupDto().setName("group1").setOrganizationUuid("org1")); + GroupDto group2InOrg1 = underTest.insert(dbSession, newGroupDto().setName("group2").setOrganizationUuid("org1")); + GroupDto group1InOrg2 = underTest.insert(dbSession, newGroupDto().setName("group1").setOrganizationUuid("org2")); + GroupDto group3InOrg2 = underTest.insert(dbSession, newGroupDto().setName("group3").setOrganizationUuid("org2")); dbSession.commit(); - assertThat(underTest.selectByNames(dbSession, asList("group1", "group2", "group3"))).hasSize(3); - assertThat(underTest.selectByNames(dbSession, singletonList("group1"))).hasSize(1); - assertThat(underTest.selectByNames(dbSession, asList("group1", "unknown"))).hasSize(1); - assertThat(underTest.selectByNames(dbSession, Collections.emptyList())).isEmpty(); + assertThat(underTest.selectByNames(dbSession, "org1", asList("group1", "group2", "group3", "missingGroup"))).extracting(GroupDto::getId) + .containsOnly(group1InOrg1.getId(), group2InOrg1.getId()); + + assertThat(underTest.selectByNames(dbSession, "org1", Collections.emptyList())).isEmpty(); + assertThat(underTest.selectByNames(dbSession, "missingOrg", asList("group1"))).isEmpty(); + } + + @Test + public void selectByIds() { + GroupDto group1 = db.users().insertGroup(); + GroupDto group2 = db.users().insertGroup(); + GroupDto group3 = db.users().insertGroup(); + + assertThat(underTest.selectByIds(dbSession, asList(group1.getId(), group2.getId()))) + .extracting(GroupDto::getId).containsOnly(group1.getId(), group2.getId()); + + assertThat(underTest.selectByIds(dbSession, asList(group1.getId(), MISSING_ID))) + .extracting(GroupDto::getId).containsOnly(group1.getId()); + + assertThat(underTest.selectByIds(dbSession, Collections.emptyList())).isEmpty(); } @Test @@ -371,7 +387,7 @@ public class GroupDaoTest { }; UserDto[] adminPerGroupPermissionUsers = { db.users().makeRoot(db.users().insertUser()), - db.users().insertUser() // incorrectly not root + db.users().insertUser() // incorrectly not root }; OrganizationDto otherOrganization = db.organizations().insert(); GroupDto nonAdminGroup = db.users().insertGroup(otherOrganization);