Search in sources :

Example 1 with MembershipCriteria

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);
}
Also used : MembershipCriteria(io.gravitee.am.repository.management.api.search.MembershipCriteria) Criteria(org.springframework.data.relational.core.query.Criteria)

Example 2 with MembershipCriteria

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();
}
Also used : MembershipCriteria(io.gravitee.am.repository.management.api.search.MembershipCriteria) NewMembership(io.gravitee.am.service.model.NewMembership)

Example 3 with MembershipCriteria

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);
}
Also used : MembershipCriteria(io.gravitee.am.repository.management.api.search.MembershipCriteria) Membership(io.gravitee.am.model.Membership) Test(org.junit.Test) AbstractManagementTest(io.gravitee.am.repository.management.AbstractManagementTest)

Example 4 with MembershipCriteria

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);
        }
    }));
}
Also used : SystemRole(io.gravitee.am.model.permissions.SystemRole) TemplateException(freemarker.template.TemplateException) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Single(io.reactivex.Single) Value(org.springframework.beans.factory.annotation.Value) Strings(com.google.common.base.Strings) io.gravitee.am.management.service.impl.notifications(io.gravitee.am.management.service.impl.notifications) Flowable(io.reactivex.Flowable) NotificationDefinition(io.gravitee.node.api.notifier.NotificationDefinition) Map(java.util.Map) Email(io.gravitee.am.common.email.Email) MembershipCriteria(io.gravitee.am.repository.management.api.search.MembershipCriteria) io.gravitee.am.service(io.gravitee.am.service) EmailConfiguration(io.gravitee.am.service.spring.email.EmailConfiguration) NotifierService(io.gravitee.node.api.notifier.NotifierService) Logger(org.slf4j.Logger) CERTIFICATE_EXPIRY_TPL(io.gravitee.am.management.service.impl.notifications.ManagementUINotifierConfiguration.CERTIFICATE_EXPIRY_TPL) NotificationDefinitionUtils(io.gravitee.am.management.service.impl.notifications.NotificationDefinitionUtils) MemberType(io.gravitee.am.model.membership.MemberType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException) io.gravitee.am.model(io.gravitee.am.model) EmailService(io.gravitee.am.management.service.EmailService) Component(org.springframework.stereotype.Component) Optional(java.util.Optional) DomainNotifierService(io.gravitee.am.management.service.DomainNotifierService) MembershipCriteria(io.gravitee.am.repository.management.api.search.MembershipCriteria)

Example 5 with MembershipCriteria

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);
}
Also used : Document(org.bson.Document) MembershipMongo(io.gravitee.am.repository.mongodb.management.internal.model.MembershipMongo) MemberType(io.gravitee.am.model.membership.MemberType) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Membership(io.gravitee.am.model.Membership) RandomString(io.gravitee.am.common.utils.RandomString) Bson(org.bson.conversions.Bson) Filters(com.mongodb.client.model.Filters) MembershipRepository(io.gravitee.am.repository.management.api.MembershipRepository) Component(org.springframework.stereotype.Component) PostConstruct(javax.annotation.PostConstruct) ReferenceType(io.gravitee.am.model.ReferenceType) MembershipCriteria(io.gravitee.am.repository.management.api.search.MembershipCriteria) io.reactivex(io.reactivex) Bson(org.bson.conversions.Bson)

Aggregations

MembershipCriteria (io.gravitee.am.repository.management.api.search.MembershipCriteria)9 MemberType (io.gravitee.am.model.membership.MemberType)4 Component (org.springframework.stereotype.Component)4 io.gravitee.am.model (io.gravitee.am.model)3 DefaultRole (io.gravitee.am.model.permissions.DefaultRole)3 SystemRole (io.gravitee.am.model.permissions.SystemRole)3 Flowable (io.reactivex.Flowable)3 Maybe (io.reactivex.Maybe)3 Membership (io.gravitee.am.model.Membership)2 MembershipService (io.gravitee.am.service.MembershipService)2 RoleService (io.gravitee.am.service.RoleService)2 NewMembership (io.gravitee.am.service.model.NewMembership)2 Arrays (java.util.Arrays)2 List (java.util.List)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Strings (com.google.common.base.Strings)1 Filters (com.mongodb.client.model.Filters)1 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)1 TemplateException (freemarker.template.TemplateException)1 Email (io.gravitee.am.common.email.Email)1