Search in sources :

Example 11 with Membership

use of io.gravitee.am.model.Membership in project gravitee-access-management by gravitee-io.

the class MongoMembershipRepository method convert.

private Membership convert(MembershipMongo membershipMongo) {
    Membership membership = new Membership();
    membership.setId(membershipMongo.getId());
    membership.setDomain(membershipMongo.getDomain());
    membership.setMemberId(membershipMongo.getMemberId());
    membership.setMemberType(MemberType.valueOf(membershipMongo.getMemberType()));
    membership.setReferenceId(membershipMongo.getReferenceId());
    membership.setReferenceType(ReferenceType.valueOf(membershipMongo.getReferenceType()));
    membership.setRoleId(membershipMongo.getRole());
    membership.setCreatedAt(membershipMongo.getCreatedAt());
    membership.setUpdatedAt(membershipMongo.getUpdatedAt());
    return membership;
}
Also used : Membership(io.gravitee.am.model.Membership)

Example 12 with Membership

use of io.gravitee.am.model.Membership in project gravitee-access-management by gravitee-io.

the class ApplicationServiceImpl method create0.

private Single<Application> create0(String domain, Application application, User principal) {
    // created and updated date
    application.setCreatedAt(new Date());
    application.setUpdatedAt(application.getCreatedAt());
    // check uniqueness
    return checkApplicationUniqueness(domain, application).andThen(validateApplicationMetadata(application)).flatMap(this::setDefaultCertificate).flatMap(applicationRepository::create).flatMap(application1 -> {
        if (principal == null || principal.getAdditionalInformation() == null || StringUtils.isEmpty(principal.getAdditionalInformation().get(Claims.organization))) {
            // There is no principal or we can not find the organization the user is attached to. Can't assign role.
            return Single.just(application1);
        }
        return roleService.findSystemRole(SystemRole.APPLICATION_PRIMARY_OWNER, ReferenceType.APPLICATION).switchIfEmpty(Single.error(new InvalidRoleException("Cannot assign owner to the application, owner role does not exist"))).flatMap(role -> {
            Membership membership = new Membership();
            membership.setDomain(application1.getDomain());
            membership.setMemberId(principal.getId());
            membership.setMemberType(MemberType.USER);
            membership.setReferenceId(application1.getId());
            membership.setReferenceType(ReferenceType.APPLICATION);
            membership.setRoleId(role.getId());
            return membershipService.addOrUpdate((String) principal.getAdditionalInformation().get(Claims.organization), membership).map(__ -> domain);
        });
    }).flatMap(application1 -> {
        Event event = new Event(Type.APPLICATION, new Payload(application.getId(), ReferenceType.DOMAIN, application.getDomain(), Action.CREATE));
        return eventService.create(event).flatMap(domain1 -> Single.just(application));
    }).doOnSuccess(application1 -> auditService.report(AuditBuilder.builder(ApplicationAuditBuilder.class).principal(principal).type(EventType.APPLICATION_CREATED).application(application1))).doOnError(throwable -> auditService.report(AuditBuilder.builder(ApplicationAuditBuilder.class).principal(principal).type(EventType.APPLICATION_CREATED).throwable(throwable)));
}
Also used : ApplicationRepository(io.gravitee.am.repository.management.api.ApplicationRepository) URISyntaxException(java.net.URISyntaxException) SecureRandomString(io.gravitee.am.common.utils.SecureRandomString) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ApplicationSettings(io.gravitee.am.model.application.ApplicationSettings) PatchApplication(io.gravitee.am.service.model.PatchApplication) Type(io.gravitee.am.common.event.Type) User(io.gravitee.am.identityprovider.api.User) AuditBuilder(io.gravitee.am.service.reporter.builder.AuditBuilder) ReferenceType(io.gravitee.am.model.ReferenceType) URI(java.net.URI) io.reactivex(io.reactivex) UriBuilder(io.gravitee.am.common.web.UriBuilder) Action(io.gravitee.am.common.event.Action) ClientAuthenticationMethod(io.gravitee.am.common.oidc.ClientAuthenticationMethod) ApplicationType(io.gravitee.am.model.application.ApplicationType) NewApplication(io.gravitee.am.service.model.NewApplication) MemberType(io.gravitee.am.model.membership.MemberType) EventType(io.gravitee.am.common.audit.EventType) Membership(io.gravitee.am.model.Membership) Collectors(java.util.stream.Collectors) RandomString(io.gravitee.am.common.utils.RandomString) Payload(io.gravitee.am.model.common.event.Payload) CollectionUtils(org.springframework.util.CollectionUtils) Claims(io.gravitee.am.common.jwt.Claims) Lazy(org.springframework.context.annotation.Lazy) Objects.nonNull(java.util.Objects.nonNull) AccountSettings(io.gravitee.am.model.account.AccountSettings) UriBuilder.isHttp(io.gravitee.am.common.web.UriBuilder.isHttp) Certificate(io.gravitee.am.model.Certificate) java.util(java.util) SystemRole(io.gravitee.am.model.permissions.SystemRole) Application(io.gravitee.am.model.Application) Event(io.gravitee.am.model.common.event.Event) io.gravitee.am.service.exception(io.gravitee.am.service.exception) OAuth2Exception(io.gravitee.am.common.exception.oauth2.OAuth2Exception) Observable(io.reactivex.Observable) ApplicationAuditBuilder(io.gravitee.am.service.reporter.builder.management.ApplicationAuditBuilder) AccountSettingsValidator(io.gravitee.am.service.validators.accountsettings.AccountSettingsValidator) io.gravitee.am.service(io.gravitee.am.service) GrantTypeUtils(io.gravitee.am.service.utils.GrantTypeUtils) GrantType(io.gravitee.am.common.oauth2.GrantType) Page(io.gravitee.am.model.common.Page) Logger(org.slf4j.Logger) ApplicationOAuthSettings(io.gravitee.am.model.application.ApplicationOAuthSettings) TopApplication(io.gravitee.am.service.model.TopApplication) Component(org.springframework.stereotype.Component) ApplicationScopeSettings(io.gravitee.am.model.application.ApplicationScopeSettings) StringUtils(org.springframework.util.StringUtils) Membership(io.gravitee.am.model.Membership) Event(io.gravitee.am.model.common.event.Event) Payload(io.gravitee.am.model.common.event.Payload) ApplicationAuditBuilder(io.gravitee.am.service.reporter.builder.management.ApplicationAuditBuilder)

Example 13 with Membership

use of io.gravitee.am.model.Membership in project gravitee-access-management by gravitee-io.

the class MembershipRepositoryTest method testFindByReferenceAndMember.

@Test
public void testFindByReferenceAndMember() {
    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 createdMembership = membershipRepository.create(membership).blockingGet();
    TestObserver<Membership> obs = membershipRepository.findByReferenceAndMember(ReferenceType.ORGANIZATION, ORGANIZATION_ID, membership.getMemberType(), membership.getMemberId()).test();
    obs.awaitTerminalEvent();
    obs.assertComplete();
    obs.assertValue(m -> m.getId().equals(createdMembership.getId()));
}
Also used : Membership(io.gravitee.am.model.Membership) Test(org.junit.Test) AbstractManagementTest(io.gravitee.am.repository.management.AbstractManagementTest)

Example 14 with Membership

use of io.gravitee.am.model.Membership in project gravitee-access-management by gravitee-io.

the class MembershipRepositoryTest method testFindById.

@Test
public void testFindById() {
    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 createdMembership = membershipRepository.create(membership).blockingGet();
    TestObserver<Membership> obs = membershipRepository.findById(createdMembership.getId()).test();
    obs.awaitTerminalEvent();
    obs.assertComplete();
    obs.assertValue(m -> m.getId().equals(createdMembership.getId()) && m.getRoleId().equals(membership.getRoleId()) && m.getReferenceType() == membership.getReferenceType() && m.getReferenceId().equals(membership.getReferenceId()) && m.getMemberType() == membership.getMemberType() && m.getMemberId().equals(membership.getMemberId()));
}
Also used : Membership(io.gravitee.am.model.Membership) Test(org.junit.Test) AbstractManagementTest(io.gravitee.am.repository.management.AbstractManagementTest)

Example 15 with Membership

use of io.gravitee.am.model.Membership in project gravitee-access-management by gravitee-io.

the class MembershipRepositoryTest method testFindByMember.

@Test
public void testFindByMember() {
    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 createdMembership = membershipRepository.create(membership).blockingGet();
    TestObserver<List<Membership>> obs = membershipRepository.findByMember("user#1", MemberType.USER).toList().test();
    obs.awaitTerminalEvent();
    obs.assertComplete();
    obs.assertValue(m -> m.size() == 1 && m.get(0).getMemberId().equals(createdMembership.getMemberId()));
}
Also used : Membership(io.gravitee.am.model.Membership) List(java.util.List) Test(org.junit.Test) AbstractManagementTest(io.gravitee.am.repository.management.AbstractManagementTest)

Aggregations

Membership (io.gravitee.am.model.Membership)24 Test (org.junit.Test)13 NewMembership (io.gravitee.am.service.model.NewMembership)10 ReferenceType (io.gravitee.am.model.ReferenceType)9 MemberType (io.gravitee.am.model.membership.MemberType)7 MembershipService (io.gravitee.am.service.MembershipService)7 Response (javax.ws.rs.core.Response)7 Single (io.reactivex.Single)6 List (java.util.List)6 User (io.gravitee.am.identityprovider.api.User)5 Organization (io.gravitee.am.model.Organization)5 User (io.gravitee.am.model.User)5 AbstractManagementTest (io.gravitee.am.repository.management.AbstractManagementTest)5 RoleService (io.gravitee.am.service.RoleService)5 Maybe (io.reactivex.Maybe)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 JerseySpringTest (io.gravitee.am.management.handlers.management.api.JerseySpringTest)4 DefaultRole (io.gravitee.am.model.permissions.DefaultRole)4 DomainService (io.gravitee.am.service.DomainService)4 URI (java.net.URI)4