use of io.gravitee.am.repository.management.api.search.MembershipCriteria in project gravitee-access-management by gravitee-io.
the class JdbcMembershipRepository method findByCriteria.
@Override
public Flowable<Membership> findByCriteria(ReferenceType referenceType, String referenceId, MembershipCriteria criteria) {
LOGGER.debug("findByCriteria({},{},{}", referenceType, referenceId, criteria);
Criteria whereClause = Criteria.empty();
Criteria groupClause = Criteria.empty();
Criteria userClause = Criteria.empty();
Criteria referenceClause = where("reference_id").is(referenceId).and(where("reference_type").is(referenceType.name()));
if (criteria.getGroupIds().isPresent()) {
groupClause = where("member_id").in(criteria.getGroupIds().get()).and(where("member_type").is(MemberType.GROUP.name()));
}
if (criteria.getUserId().isPresent()) {
userClause = where("member_id").is(criteria.getUserId().get()).and(where("member_type").is(MemberType.USER.name()));
}
if (criteria.getRoleId().isPresent()) {
userClause = where("role_id").is(criteria.getRoleId().get());
}
whereClause = whereClause.and(referenceClause.and(criteria.isLogicalOR() ? userClause.or(groupClause) : userClause.and(groupClause)));
return fluxToFlowable(template.select(Query.query(whereClause), JdbcMembership.class)).map(this::toEntity);
}
use of io.gravitee.am.repository.management.api.search.MembershipCriteria in project gravitee-access-management by gravitee-io.
the class MembershipServiceImpl method addEnvironmentUserRoleIfNecessary.
@Override
public Completable addEnvironmentUserRoleIfNecessary(String organizationId, String environmentId, NewMembership newMembership, User principal) {
MembershipCriteria criteria = convert(newMembership);
return this.findByCriteria(ReferenceType.ENVIRONMENT, environmentId, criteria).switchIfEmpty(Flowable.defer(() -> roleService.findDefaultRole(organizationId, DefaultRole.ENVIRONMENT_USER, ReferenceType.ENVIRONMENT).flatMapSingle(role -> {
final Membership environmentMembership = new Membership();
environmentMembership.setMemberId(newMembership.getMemberId());
environmentMembership.setMemberType(newMembership.getMemberType());
environmentMembership.setRoleId(role.getId());
environmentMembership.setReferenceId(environmentId);
environmentMembership.setReferenceType(ReferenceType.ENVIRONMENT);
return this.createInternal(environmentMembership, principal);
}).toFlowable())).ignoreElements();
}
use of io.gravitee.am.repository.management.api.search.MembershipCriteria in project gravitee-access-management by gravitee-io.
the class MembershipRepositoryTest method testFindByCriteria.
@Test
public void testFindByCriteria() {
Membership membership = new Membership();
membership.setRoleId("role#1");
membership.setReferenceType(ReferenceType.ORGANIZATION);
membership.setReferenceId(ORGANIZATION_ID);
membership.setMemberType(MemberType.USER);
membership.setMemberId("user#1");
Membership groupMembership = new Membership();
groupMembership.setRoleId("role#1");
groupMembership.setReferenceType(ReferenceType.ORGANIZATION);
groupMembership.setReferenceId(ORGANIZATION_ID);
groupMembership.setMemberType(MemberType.GROUP);
groupMembership.setMemberId("group#1");
membershipRepository.create(membership).blockingGet();
membershipRepository.create(groupMembership).blockingGet();
MembershipCriteria criteria = new MembershipCriteria();
TestSubscriber<Membership> obs = membershipRepository.findByCriteria(ReferenceType.ORGANIZATION, ORGANIZATION_ID, criteria).test();
obs.awaitTerminalEvent();
obs.assertValueCount(2);
criteria.setUserId("user#1");
obs = membershipRepository.findByCriteria(ReferenceType.ORGANIZATION, ORGANIZATION_ID, criteria).test();
obs.awaitTerminalEvent();
obs.assertValueCount(1);
obs.assertValue(m -> m.getMemberType() == MemberType.USER && m.getMemberId().equals("user#1"));
criteria.setUserId(null);
criteria.setGroupIds(Arrays.asList("group#1"));
obs = membershipRepository.findByCriteria(ReferenceType.ORGANIZATION, ORGANIZATION_ID, criteria).test();
obs.awaitTerminalEvent();
obs.assertValueCount(1);
obs.assertValue(m -> m.getMemberType() == MemberType.GROUP && m.getMemberId().equals("group#1"));
criteria.setUserId("user#1");
criteria.setGroupIds(Arrays.asList("group#1"));
obs = membershipRepository.findByCriteria(ReferenceType.ORGANIZATION, ORGANIZATION_ID, criteria).test();
obs.awaitTerminalEvent();
obs.assertNoValues();
obs.assertNoErrors();
criteria.setUserId("user#1");
criteria.setGroupIds(Arrays.asList("group#1"));
criteria.setLogicalOR(true);
obs = membershipRepository.findByCriteria(ReferenceType.ORGANIZATION, ORGANIZATION_ID, criteria).test();
obs.awaitTerminalEvent();
obs.assertValueCount(2);
}
use of io.gravitee.am.repository.management.api.search.MembershipCriteria in project gravitee-access-management by gravitee-io.
the class DomainNotifierServiceImpl method retrieveDomainOwners.
private Flowable<User> retrieveDomainOwners(Domain domain) {
return findEnvironment(domain).flatMapPublisher(env -> Maybe.concat(roleService.findSystemRole(SystemRole.DOMAIN_PRIMARY_OWNER, ReferenceType.DOMAIN), roleService.findDefaultRole(env.getOrganizationId(), DefaultRole.DOMAIN_OWNER, ReferenceType.DOMAIN)).map(role -> role.getId()).flatMap(roleId -> {
final MembershipCriteria criteria = new MembershipCriteria();
criteria.setRoleId(roleId);
return membershipService.findByCriteria(ReferenceType.DOMAIN, domain.getId(), criteria);
}).flatMap(membership -> {
if (membership.getMemberType() == MemberType.USER) {
return userService.findById(ReferenceType.ORGANIZATION, env.getOrganizationId(), membership.getMemberId()).toFlowable();
} else {
return readUsersFromGroup(membership, 0, 10);
}
}));
}
use of io.gravitee.am.repository.management.api.search.MembershipCriteria in project gravitee-access-management by gravitee-io.
the class MongoMembershipRepository method findByCriteria.
@Override
public Flowable<Membership> findByCriteria(ReferenceType referenceType, String referenceId, MembershipCriteria criteria) {
Bson eqReference = and(eq(FIELD_REFERENCE_TYPE, referenceType.name()), eq(FIELD_REFERENCE_ID, referenceId));
Bson eqGroupId = null;
Bson eqUserId = null;
if (criteria.getGroupIds().isPresent()) {
eqGroupId = and(eq(FIELD_MEMBER_TYPE, MemberType.GROUP.name()), in(FIELD_MEMBER_ID, criteria.getGroupIds().get()));
}
if (criteria.getUserId().isPresent()) {
eqUserId = and(eq(FIELD_MEMBER_TYPE, MemberType.USER.name()), eq(FIELD_MEMBER_ID, criteria.getUserId().get()));
}
if (criteria.getRoleId().isPresent()) {
eqUserId = eq(FIELD_ROLE, criteria.getRoleId().get());
}
return toBsonFilter(criteria.isLogicalOR(), eqGroupId, eqUserId).map(filter -> and(eqReference, filter)).switchIfEmpty(Single.just(eqReference)).flatMapPublisher(filter -> Flowable.fromPublisher(membershipsCollection.find(filter))).map(this::convert);
}
Aggregations