@Test public void deleteById() { db.getDbClient().groupDao().insert(dbSession, aGroup); underTest.deleteById(dbSession, aGroup.getId()); assertThat(db.countRowsOfTable(dbSession, "groups")).isEqualTo(0); }
public GroupDto findGroupDto(DbSession dbSession, GroupWsRef ref) { if (ref.hasId()) { GroupDto group = dbClient.groupDao().selectById(dbSession, ref.getId()); checkFound(group, "No group with id '%s'", ref.getId()); return group; } OrganizationDto org = findOrganizationByKey(dbSession, ref.getOrganizationKey()); Optional<GroupDto> group = dbClient.groupDao().selectByName(dbSession, org.getUuid(), ref.getName()); checkFoundWithOptional(group, "No group with name '%s' in organization '%s'", ref.getName(), org.getKey()); return group.get(); }
@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(); }
public GroupDto create(DbSession dbSession, String organizationUuid) { Optional<GroupDto> existingMembersGroup = dbClient.groupDao().selectByName(dbSession, organizationUuid, DEFAULT_GROUP_NAME); checkArgument(!existingMembersGroup.isPresent(), "The group '%s' already exist on organization '%s'", DEFAULT_GROUP_NAME, organizationUuid); GroupDto defaultGroup = new GroupDto() .setName(DEFAULT_GROUP_NAME) .setDescription("All members of the organization") .setOrganizationUuid(organizationUuid); dbClient.groupDao().insert(dbSession, defaultGroup); dbClient.organizationDao().setDefaultGroupId(dbSession, organizationUuid, defaultGroup); return defaultGroup; }
@Test public void select_by_query_with_special_characters() { String groupNameWithSpecialCharacters = "group%_%/name"; underTest.insert(dbSession, newGroupDto().setName(groupNameWithSpecialCharacters).setOrganizationUuid("org1")); db.commit(); List<GroupDto> result = underTest.selectByQuery(dbSession, "org1", "roup%_%/nam", 0, 10); int resultCount = underTest.countByQuery(dbSession, "org1", "roup%_%/nam"); assertThat(result).hasSize(1); assertThat(result.get(0).getName()).isEqualTo(groupNameWithSpecialCharacters); assertThat(resultCount).isEqualTo(1); }
void checkNameDoesNotExist(DbSession dbSession, String organizationUuid, String name) { // There is no database constraint on column groups.name // because MySQL cannot create a unique index // on a UTF-8 VARCHAR larger than 255 characters on InnoDB checkRequest(!dbClient.groupDao().selectByName(dbSession, organizationUuid, name).isPresent(), "Group '%s' already exists", name); }
/** * Owners group has an hard coded name, a description based on the organization's name and has all global permissions. */ private GroupDto insertOwnersGroup(DbSession dbSession, OrganizationDto organization) { GroupDto group = dbClient.groupDao().insert(dbSession, new GroupDto() .setOrganizationUuid(organization.getUuid()) .setName(OWNERS_GROUP_NAME) .setDescription(format(OWNERS_GROUP_DESCRIPTION_PATTERN, organization.getName()))); permissionService.getAllOrganizationPermissions().forEach(p -> addPermissionToGroup(dbSession, group, p)); return group; }
public GroupDto findDefaultGroup(DbSession dbSession, String organizationUuid) { int defaultGroupId = dbClient.organizationDao().getDefaultGroupId(dbSession, organizationUuid) .orElseThrow(() -> new IllegalStateException(format("Default group cannot be found on organization '%s'", organizationUuid))); return requireNonNull(dbClient.groupDao().selectById(dbSession, defaultGroupId), format("Group '%s' cannot be found", defaultGroupId)); }
@Test public void selectByNames() { 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, "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(); }
@Override public void handle(Request request, Response response) throws Exception { int page = request.mandatoryParamAsInt(Param.PAGE); int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE); SearchOptions options = new SearchOptions() .setPage(page, pageSize); String query = defaultIfBlank(request.param(Param.TEXT_QUERY), ""); Set<String> fields = neededFields(request); try (DbSession dbSession = dbClient.openSession(false)) { OrganizationDto organization = groupWsSupport.findOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION_KEY)); userSession.checkLoggedIn().checkPermission(ADMINISTER, organization); GroupDto defaultGroup = defaultGroupFinder.findDefaultGroup(dbSession, organization.getUuid()); int limit = dbClient.groupDao().countByQuery(dbSession, organization.getUuid(), query); Paging paging = forPageIndex(page).withPageSize(pageSize).andTotal(limit); List<GroupDto> groups = dbClient.groupDao().selectByQuery(dbSession, organization.getUuid(), query, options.getOffset(), pageSize); List<Integer> groupIds = groups.stream().map(GroupDto::getId).collect(MoreCollectors.toList(groups.size())); Map<String, Integer> userCountByGroup = dbClient.groupMembershipDao().countUsersByGroups(dbSession, groupIds); writeProtobuf(buildResponse(groups, userCountByGroup, fields, paging, defaultGroup), request, response); } }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { int groupId = request.mandatoryParamAsInt(PARAM_GROUP_ID); GroupDto group = dbClient.groupDao().selectById(dbSession, groupId); checkFound(group, "Could not find a user group with id '%s'.", groupId); Optional<OrganizationDto> org = dbClient.organizationDao().selectByUuid(dbSession, group.getOrganizationUuid()); checkFoundWithOptional(org, "Could not find organization with id '%s'.", group.getOrganizationUuid()); userSession.checkPermission(ADMINISTER, org.get()); support.checkGroupIsNotDefault(dbSession, group); boolean changed = false; String newName = request.param(PARAM_GROUP_NAME); if (newName != null) { changed = true; UserGroupValidation.validateGroupName(newName); support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), newName); group.setName(newName); } String description = request.param(PARAM_GROUP_DESCRIPTION); if (description != null) { changed = true; group.setDescription(description); } if (changed) { dbClient.groupDao().update(dbSession, group); dbSession.commit(); } writeResponse(dbSession, request, response, org.get(), group); } }
private void syncGroups(DbSession dbSession, UserIdentity userIdentity, UserDto userDto) { if (!userIdentity.shouldSyncGroups()) { return; } String userLogin = userDto.getLogin(); Set<String> userGroups = new HashSet<>(dbClient.groupMembershipDao().selectGroupsByLogins(dbSession, singletonList(userLogin)).get(userLogin)); Set<String> identityGroups = userIdentity.getGroups(); LOGGER.debug("List of groups returned by the identity provider '{}'", identityGroups); Collection<String> groupsToAdd = Sets.difference(identityGroups, userGroups); Collection<String> groupsToRemove = Sets.difference(userGroups, identityGroups); Collection<String> allGroups = new ArrayList<>(groupsToAdd); allGroups.addAll(groupsToRemove); DefaultOrganization defaultOrganization = defaultOrganizationProvider.get(); Map<String, GroupDto> groupsByName = dbClient.groupDao().selectByNames(dbSession, defaultOrganization.getUuid(), allGroups) .stream() .collect(uniqueIndex(GroupDto::getName)); addGroups(dbSession, userDto, groupsToAdd, groupsByName); removeGroups(dbSession, userDto, groupsToRemove, groupsByName); }
@Override public void handle(Request request, Response response) throws Exception { try (DbSession dbSession = dbClient.openSession(false)) { GroupDto group = support.findGroupDto(dbSession, request); userSession.checkPermission(OrganizationPermission.ADMINISTER, group.getOrganizationUuid()); support.checkGroupIsNotDefault(dbSession, group); checkNotTryingToDeleteLastAdminGroup(dbSession, group); removeGroupPermissions(dbSession, group); removeFromPermissionTemplates(dbSession, group); removeGroupMembers(dbSession, group); dbClient.qProfileEditGroupsDao().deleteByGroup(dbSession, group); dbClient.groupDao().deleteById(dbSession, group.getId()); dbSession.commit(); response.noContent(); } }
private Collection<GroupDto> loadGroups() { if (this.userDto == null) { return Collections.emptyList(); } try (DbSession dbSession = dbClient.openSession(false)) { return dbClient.groupDao().selectByUserLogin(dbSession, userDto.getLogin()); } }
private void deleteGroups(DbSession dbSession, OrganizationDto organization) { dbClient.groupDao().deleteByOrganization(dbSession, organization.getUuid()); }
assertThat(underTest.selectByQuery(dbSession, "org1", null, 0, 10)) .hasSize(5) .extracting("name").containsOnly("customers-group1", "customers-group2", "customers-group3", "SONAR-ADMINS", "sonar-users"); assertThat(underTest.selectByQuery(dbSession, "org1", "", 0, 10)) .hasSize(5) .extracting("name").containsOnly("customers-group1", "customers-group2", "customers-group3", "SONAR-ADMINS", "sonar-users"); assertThat(underTest.selectByQuery(dbSession, "org1", "sonar", 0, 10)) .hasSize(2) .extracting("name").containsOnly("SONAR-ADMINS", "sonar-users"); assertThat(underTest.selectByQuery(dbSession, "org1", null, 0, 3)) .hasSize(3); assertThat(underTest.selectByQuery(dbSession, "org1", null, 3, 3)) .hasSize(2); assertThat(underTest.selectByQuery(dbSession, "org1", null, 6, 3)).isEmpty(); assertThat(underTest.selectByQuery(dbSession, "org1", null, 0, 5)) .hasSize(5); assertThat(underTest.selectByQuery(dbSession, "org1", null, 5, 5)).isEmpty();
@Test public void countByQuery() { OrganizationDto organization1 = db.organizations().insert(organizationDto -> organizationDto.setUuid("org1")); OrganizationDto organization2 = db.organizations().insert(organizationDto -> organizationDto.setUuid("org2")); db.users().insertGroup(organization1, "sonar-users"); db.users().insertGroup(organization1, "SONAR-ADMINS"); db.users().insertGroup(organization1, "customers-group1"); db.users().insertGroup(organization1, "customers-group2"); db.users().insertGroup(organization1, "customers-group3"); // Group on another organization db.users().insertGroup(organization2, "customers-group4"); // Null query assertThat(underTest.countByQuery(dbSession, "org1", null)).isEqualTo(5); // Empty query assertThat(underTest.countByQuery(dbSession, "org1", "")).isEqualTo(5); // Filter on name assertThat(underTest.countByQuery(dbSession, "org1", "sonar")).isEqualTo(2); }
GroupDto getGroup(DbSession dbSession, OrganizationDto organization, String groupName) { Optional<GroupDto> group = dbClient.groupDao().selectByName(dbSession, organization.getUuid(), groupName); checkFoundWithOptional(group, "No group with name '%s' in organization '%s'", groupName, organization.getKey()); return group.get(); }
@Test public void selectByName() { db.getDbClient().groupDao().insert(dbSession, aGroup); GroupDto group = underTest.selectByName(dbSession, AN_ORGANIZATION.getUuid(), aGroup.getName()).get(); assertThat(group.getId()).isNotNull(); assertThat(group.getOrganizationUuid()).isEqualTo(aGroup.getOrganizationUuid()); assertThat(group.getName()).isEqualTo(aGroup.getName()); assertThat(group.getDescription()).isEqualTo(aGroup.getDescription()); assertThat(group.getCreatedAt()).isEqualTo(new Date(NOW)); assertThat(group.getUpdatedAt()).isEqualTo(new Date(NOW)); }
public GroupDto insertGroup(GroupDto dto) { db.getDbClient().groupDao().insert(db.getSession(), dto); db.commit(); return dto; }