Search in sources :

Example 1 with Membership

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

the class ApplicationMembersResource method addOrUpdateMember.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Add or update an application member", notes = "User must have APPLICATION_MEMBER[CREATE] permission on the specified application " + "or APPLICATION_MEMBER[CREATE] permission on the specified domain " + "or APPLICATION_MEMBER[CREATE] permission on the specified environment " + "or APPLICATION_MEMBER[CREATE] permission on the specified organization")
@ApiResponses({ @ApiResponse(code = 201, message = "Member has been added or updated successfully"), @ApiResponse(code = 400, message = "Membership parameter is not valid"), @ApiResponse(code = 500, message = "Internal server error") })
public void addOrUpdateMember(@PathParam("organizationId") String organizationId, @PathParam("environmentId") String environmentId, @PathParam("domain") String domain, @PathParam("application") String application, @Valid @NotNull NewMembership newMembership, @Suspended final AsyncResponse response) {
    final User authenticatedUser = getAuthenticatedUser();
    final Membership membership = convert(newMembership);
    membership.setDomain(domain);
    membership.setReferenceId(application);
    membership.setReferenceType(ReferenceType.APPLICATION);
    checkAnyPermission(organizationId, environmentId, domain, application, Permission.APPLICATION_MEMBER, Acl.CREATE).andThen(domainService.findById(domain).switchIfEmpty(Maybe.error(new DomainNotFoundException(domain))).flatMap(__ -> applicationService.findById(application)).switchIfEmpty(Maybe.error(new ApplicationNotFoundException(application))).flatMapSingle(__ -> membershipService.addOrUpdate(organizationId, membership, authenticatedUser)).flatMap(membership1 -> membershipService.addDomainUserRoleIfNecessary(organizationId, environmentId, domain, newMembership, authenticatedUser).andThen(Single.just(Response.created(URI.create("/organizations/" + organizationId + "/environments/" + environmentId + "/domains/" + domain + "/applications/" + application + "/members/" + membership1.getId())).entity(membership1).build())))).subscribe(response::resume, response::resume);
}
Also used : ApplicationService(io.gravitee.am.service.ApplicationService) Arrays(java.util.Arrays) Completable(io.reactivex.Completable) Permission(io.gravitee.am.model.permissions.Permission) Maybe(io.reactivex.Maybe) DomainService(io.gravitee.am.service.DomainService) Autowired(org.springframework.beans.factory.annotation.Autowired) MembershipListItem(io.gravitee.am.management.handlers.management.api.model.MembershipListItem) AbstractResource(io.gravitee.am.management.handlers.management.api.resources.AbstractResource) ApiResponses(io.swagger.annotations.ApiResponses) Single(io.reactivex.Single) Valid(javax.validation.Valid) Acl(io.gravitee.am.model.Acl) ApiOperation(io.swagger.annotations.ApiOperation) User(io.gravitee.am.identityprovider.api.User) NewMembership(io.gravitee.am.service.model.NewMembership) ReferenceType(io.gravitee.am.model.ReferenceType) MembershipCriteria(io.gravitee.am.repository.management.api.search.MembershipCriteria) URI(java.net.URI) MembershipService(io.gravitee.am.service.MembershipService) RoleService(io.gravitee.am.service.RoleService) Context(javax.ws.rs.core.Context) MemberType(io.gravitee.am.model.membership.MemberType) AsyncResponse(javax.ws.rs.container.AsyncResponse) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) Membership(io.gravitee.am.model.Membership) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException) NotNull(javax.validation.constraints.NotNull) Suspended(javax.ws.rs.container.Suspended) List(java.util.List) MediaType(io.gravitee.common.http.MediaType) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) ResourceContext(javax.ws.rs.container.ResourceContext) ApplicationNotFoundException(io.gravitee.am.service.exception.ApplicationNotFoundException) User(io.gravitee.am.identityprovider.api.User) ApplicationNotFoundException(io.gravitee.am.service.exception.ApplicationNotFoundException) NewMembership(io.gravitee.am.service.model.NewMembership) Membership(io.gravitee.am.model.Membership) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 2 with Membership

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

the class MembersResourceTest method shouldGetMembers.

@Test
public void shouldGetMembers() {
    Organization organization = new Organization();
    organization.setId("orga#1");
    Membership membership = new Membership();
    membership.setId("membership#1");
    doReturn(Single.just(organization)).when(organizationService).findById(organization.getId());
    doReturn(Flowable.just(Arrays.asList(membership))).when(membershipService).findByReference(organization.getId(), ReferenceType.ORGANIZATION);
    doReturn(Single.just(new HashMap<>())).when(membershipService).getMetadata(anyList());
    final Response response = target("organizations").path(organization.getId()).path("members").request().get();
    assertEquals(HttpStatusCode.OK_200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Organization(io.gravitee.am.model.Organization) HashMap(java.util.HashMap) Membership(io.gravitee.am.model.Membership) NewMembership(io.gravitee.am.service.model.NewMembership) Test(org.junit.Test) JerseySpringTest(io.gravitee.am.management.handlers.management.api.JerseySpringTest)

Example 3 with Membership

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

the class MembersResourceTest method shouldGetMembers_organizationNotFound.

@Test
public void shouldGetMembers_organizationNotFound() {
    Organization organization = new Organization();
    organization.setId("orga#1");
    Membership membership = new Membership();
    membership.setId("membership#1");
    doReturn(Single.error(new OrganizationNotFoundException(organization.getId()))).when(organizationService).findById(organization.getId());
    final Response response = target("organizations").path(organization.getId()).path("members").request().get();
    assertEquals(HttpStatusCode.NOT_FOUND_404, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) OrganizationNotFoundException(io.gravitee.am.service.exception.OrganizationNotFoundException) Organization(io.gravitee.am.model.Organization) Membership(io.gravitee.am.model.Membership) NewMembership(io.gravitee.am.service.model.NewMembership) Test(org.junit.Test) JerseySpringTest(io.gravitee.am.management.handlers.management.api.JerseySpringTest)

Example 4 with Membership

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

the class MembershipRepositoryTest method testFindByReference.

@Test
public void testFindByReference() {
    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.findByReference(ORGANIZATION_ID, ReferenceType.ORGANIZATION).toList().test();
    obs.awaitTerminalEvent();
    obs.assertComplete();
    obs.assertValue(m -> m.size() == 1 && m.get(0).getId().equals(createdMembership.getId()));
}
Also used : Membership(io.gravitee.am.model.Membership) List(java.util.List) Test(org.junit.Test) AbstractManagementTest(io.gravitee.am.repository.management.AbstractManagementTest)

Example 5 with Membership

use of io.gravitee.am.model.Membership 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)

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