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);
}
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());
}
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());
}
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()));
}
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);
}
Aggregations