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;
Collection<String> groupsToRemove = Sets.difference(userGroups, identityGroups);
Collection<String> allGroups = new ArrayList<>(groupsToAdd);
allGroups.addAll(groupsToRemove);
- Map<String, GroupDto> groupsByName = from(dbClient.groupDao().selectByNames(dbSession, allGroups)).uniqueIndex(GroupDtoToName.INSTANCE);
+ DefaultOrganization defaultOrganization = defaultOrganizationProvider.get();
+ Map<String, GroupDto> groupsByName = from(dbClient.groupDao().selectByNames(dbSession, defaultOrganization.getUuid(), allGroups)).uniqueIndex(GroupDtoToName.INSTANCE);
addGroups(dbSession, userDto, groupsToAdd, groupsByName);
removeGroups(dbSession, userDto, groupsToRemove, groupsByName);
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;
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 {
.setResponseExample(Resources.getResource(getClass(), "groups-example.json"))
.setHandler(this);
- // TODO add orgKey
+ createOrganizationParameter(action);
createPermissionParameter(action).setRequired(false);
createProjectParameters(action);
}
@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> projectId = support.findProject(dbSession, request);
- checkProjectAdmin(userSession, projectId);
+ checkProjectAdmin(userSession, org.getUuid(), projectId);
PermissionQuery query = buildPermissionQuery(request, projectId);
// TODO validatePermission(groupsRequest.getPermission(), wsProjectRef);
- List<GroupDto> groups = findGroups(dbSession, query);
- int total = dbClient.groupPermissionDao().countGroupsByPermissionQuery(dbSession, query);
- List<GroupPermissionDto> groupsWithPermission = findGroupPermissions(dbSession, groups, projectId);
+ List<GroupDto> groups = findGroups(dbSession, org, query);
+ int total = dbClient.groupPermissionDao().countGroupsByQuery(dbSession, org.getUuid(), query);
+ List<GroupPermissionDto> 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);
return response.build();
}
- private List<GroupDto> findGroups(DbSession dbSession, PermissionQuery dbQuery) {
- // TODO support organizations
- List<String> orderedNames = dbClient.groupPermissionDao().selectGroupNamesByPermissionQuery(dbSession, dbQuery);
- List<GroupDto> groups = dbClient.groupDao().selectByNames(dbSession, orderedNames);
+ private List<GroupDto> findGroups(DbSession dbSession, OrganizationDto org, PermissionQuery dbQuery) {
+ List<String> orderedNames = dbClient.groupPermissionDao().selectGroupNamesByQuery(dbSession, org.getUuid(), dbQuery);
+ List<GroupDto> 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<GroupPermissionDto> findGroupPermissions(DbSession dbSession, List<GroupDto> groups, Optional<ProjectId> project) {
+ private List<GroupPermissionDto> findGroupPermissions(DbSession dbSession, OrganizationDto org, List<GroupDto> groups, Optional<ProjectId> project) {
if (groups.isEmpty()) {
return emptyList();
}
- // TODO use groupId
- List<String> names = groups.stream().map(GroupDto::getName).collect(Collectors.toList());
- return dbClient.groupPermissionDao().selectGroupPermissionsByGroupNamesAndProject(dbSession, names, project.isPresent() ? project.get().getId() : null);
+ List<Long> 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);
}
}
private List<GroupDto> findGroups(DbSession dbSession, PermissionQuery dbQuery, PermissionTemplateDto template) {
List<String> orderedNames = dbClient.permissionTemplateDao().selectGroupNamesByQueryAndTemplate(dbSession, dbQuery, template.getId());
- List<GroupDto> groups = dbClient.groupDao().selectByNames(dbSession, orderedNames);
+ List<GroupDto> groups = dbClient.groupDao().selectByNames(dbSession, template.getOrganizationUuid(), orderedNames);
if (orderedNames.contains(DefaultGroups.ANYONE)) {
groups.add(0, new GroupDto().setId(0L).setName(DefaultGroups.ANYONE));
}
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;
@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;
@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();
@Test
public void search_for_groups_with_one_permission() throws Exception {
- loginAsAdmin();
+ loginAsAdminOnDefaultOrganization();
newRequest()
.setParam(PARAM_PERMISSION, SCAN_EXECUTION)
.execute()
@Test
public void search_with_selection() throws Exception {
- loginAsAdmin();
+ loginAsAdminOnDefaultOrganization();
String result = newRequest()
.setParam(PARAM_PERMISSION, SCAN_EXECUTION)
.execute()
@Test
public void search_groups_with_pagination() throws Exception {
- loginAsAdmin();
+ loginAsAdminOnDefaultOrganization();
String result = newRequest()
.setParam(PARAM_PERMISSION, SCAN_EXECUTION)
.setParam(PAGE_SIZE, "1")
@Test
public void search_groups_with_query() throws Exception {
- loginAsAdmin();
+ loginAsAdminOnDefaultOrganization();
String result = newRequest()
.setParam(PARAM_PERMISSION, SCAN_EXECUTION)
.setParam(TEXT_QUERY, "group-")
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")
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())
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")
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")
expectedException.expect(BadRequestException.class);
- loginAsAdmin();
+ loginAsAdminOnDefaultOrganization();
newRequest()
.setParam(PARAM_PERMISSION, SCAN_EXECUTION)
.setParam(PARAM_PROJECT_ID, "project-uuid")
private WsTester.TestRequest newRequest() {
return wsTester.newPostRequest(CONTROLLER, "groups");
}
-
- private void loginAsAdmin() {
- userSession.login("login").setGlobalPermissions(SYSTEM_ADMIN);
- }
}
private List<String> 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<String> selectProjectPermissionUsers(ComponentDto project, String permission) {
private List<String> 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<String> selectProjectPermissionUsers(ComponentDto project, String permission) {
/**
* @return group names, sorted in alphabetical order
- * @deprecated not compatible with organizations.
*/
- @Deprecated
- public List<String> selectGroupNamesByPermissionQuery(DbSession dbSession, PermissionQuery query) {
- return mapper(dbSession).selectGroupNamesByPermissionQuery(query, new RowBounds(query.getPageOffset(), query.getPageSize()));
+ public List<String> 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<GroupPermissionDto> selectGroupPermissionsByGroupNamesAndProject(DbSession dbSession, List<String> groupNames, @Nullable Long projectId) {
- return executeLargeInputs(groupNames, groups -> mapper(dbSession).selectGroupPermissionByGroupNames(groups, projectId));
+ public List<GroupPermissionDto> selectByGroupIds(DbSession dbSession, String organizationUuid, List<Long> groupIds, @Nullable Long projectId) {
+ return executeLargeInputs(groupIds, groups -> mapper(dbSession).selectByGroupIds(organizationUuid, groups, projectId));
}
/**
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.
@Deprecated
int countGroups(Map<String, Object> parameters);
- /**
- * @deprecated does not support organizations
- */
- @Deprecated
- List<String> selectGroupNamesByPermissionQuery(@Param("query") PermissionQuery query, RowBounds rowBounds);
+ List<String> 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<GroupPermissionDto> selectGroupPermissionByGroupNames(@Param("groupNames") List<String> groupNames, @Nullable @Param("projectId") Long projectId);
+ List<GroupPermissionDto> selectByGroupIds(@Param("organizationUuid") String organizationUuid, @Param("groupIds") List<Long> groupIds, @Nullable @Param("projectId") Long projectId);
void groupsCountByProjectIdAndPermission(Map<String, Object> parameters, ResultHandler resultHandler);
return Optional.ofNullable(mapper(dbSession).selectByName(organizationUuid, name));
}
- /**
- * @deprecated organization should be added as a parameter
- */
- @Deprecated
- public List<GroupDto> selectByNames(DbSession session, Collection<String> names) {
- return executeLargeInputs(names, mapper(session)::selectByNames);
+ public List<GroupDto> selectByNames(DbSession dbSession, String organizationUuid, Collection<String> names) {
+ return executeLargeInputs(names, pageOfNames -> mapper(dbSession).selectByNames(organizationUuid, pageOfNames));
}
@CheckForNull
return mapper(dbSession).selectById(groupId);
}
+ public List<GroupDto> selectByIds(DbSession dbSession, List<Long> ids) {
+ return executeLargeInputs(ids, mapper(dbSession)::selectByIds);
+ }
+
public void deleteById(DbSession dbSession, long groupId) {
mapper(dbSession).deleteById(groupId);
}
List<GroupDto> selectByUserLogin(String userLogin);
- List<GroupDto> selectByNames(@Param("names") List<String> names);
+ List<GroupDto> selectByNames(@Param("organizationUuid") String organizationUuid, @Param("names") List<String> names);
void insert(GroupDto groupDto);
int updateNonRootUsersOfGroup(@Param("groupId") long groupId,
@Param("defaultOrganizationUuid") String defaultOrganizationUuid,
@Param("now") long now);
+
+ List<GroupDto> selectByIds(@Param("ids") List<Long> ids);
}
) groups
</select>
- <select id="selectGroupNamesByPermissionQuery" parameterType="map" resultType="string">
- select distinct gr.name, lower(gr.name), gr.group_id
+ <select id="selectGroupNamesByQuery" parameterType="map" resultType="string">
+ select distinct sub.name, lower(sub.name), sub.groupId
<include refid="groupsByQuery" />
- order by lower(gr.name), gr.name, gr.group_id
+ order by lower(sub.name), sub.name, sub.groupId
</select>
- <select id="countGroupsByPermissionQuery" parameterType="map" resultType="int">
+ <select id="countGroupsByQuery" parameterType="map" resultType="int">
select count(1)
from (
- select distinct gr.group_id
+ select distinct sub.groupId
<include refid="groupsByQuery" />) g
</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
- 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
<if test="query.withAtLeastOnePermission()">
- where gr.group_id is null
+ where
+ gr.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
+ gr.group_id is null
</if>
- ) gr
- left join projects p on gr.resource_id = p.id
+
+ ) sub
+ left join projects p on sub.componentId = p.id
<where>
<if test="query.searchQueryToSql != null">
- and lower(gr.name) like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/'
+ and lower(sub.name) like #{query.searchQueryToSql,jdbcType=VARCHAR} ESCAPE '/'
</if>
<!-- filter rows with group permissions -->
<if test="query.withAtLeastOnePermission()">
- and gr.permission is not null
+ and sub.permission is not null
<if test="query.componentUuid==null">
- and gr.resource_id is null
+ and sub.componentId is null
</if>
<if test="query.componentUuid!=null">
- and p.uuid=#{query.componentUuid,jdbcType=VARCHAR}
+ and p.uuid = #{query.componentUuid,jdbcType=VARCHAR}
</if>
<if test="query.permission!=null">
- and gr.permission=#{query.permission,jdbcType=VARCHAR}
+ and sub.permission = #{query.permission,jdbcType=VARCHAR}
</if>
</if>
</where>
</sql>
- <select id="selectGroupPermissionByGroupNames" parameterType="map" resultType="GroupPermission">
- SELECT sub.id, sub.groupId, sub.resourceId, sub.role
- FROM
+ <select id="selectByGroupIds" parameterType="map" resultType="GroupPermission">
+ 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
- <foreach collection="groupNames" open="(" close=")" item="name" separator=",">
- #{name}
+ sub.groupId in
+ <foreach collection="groupIds" open="(" close=")" item="groupId" separator=",">
+ #{groupId,jdbcType=BIGINT}
</foreach>
- <if test="projectId!=null">
- AND sub.resourceId=#{projectId,jdbcType=BIGINT}
+ <if test="projectId != null">
+ and sub.componentId=#{projectId,jdbcType=BIGINT}
</if>
<if test="projectId==null">
- AND sub.resourceId IS NULL
+ and sub.componentId is null
</if>
</select>
where g.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and g.name = #{name,jdbcType=VARCHAR}
</select>
+ <select id="selectByIds" parameterType="long" resultType="Group">
+ select
+ <include refid="groupColumns"/>
+ from groups g
+ where g.id in
+ <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
+ #{id,jdbcType=BIGINT}
+ </foreach>
+ </select>
+
<select id="selectByKey" parameterType="string" resultType="Group">
SELECT
<include refid="groupColumns"/>
</select>
<select id="selectByNames" parameterType="map" resultType="Group">
- SELECT
+ select
<include refid="groupColumns"/>
- FROM groups g
- <where>
- g.name IN
+ from groups g
+ where
+ g.organization_uuid = #{organizationUuid,jdbcType=VARCHAR} and
+ g.name in
<foreach item="name" index="index" collection="names" open="(" separator="," close=")">
- #{name}
+ #{name,jdbcType=VARCHAR}
</foreach>
- </where>
</select>
<insert id="insert" parameterType="Group" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
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;
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();
}
@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<String> 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());
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);
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<String> 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
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);
+ }
}
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;
@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);
@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);
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;
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")
@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()
@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
};
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);