use of io.gravitee.am.service.model.NewMembership in project gravitee-access-management by gravitee-io.
the class MembershipServiceTest method shouldNotAddEnvironmentUserRole_userAlreadyHasMembership.
@Test
public void shouldNotAddEnvironmentUserRole_userAlreadyHasMembership() {
NewMembership membership = new NewMembership();
membership.setMemberType(MemberType.USER);
membership.setMemberId("user#1");
DefaultUser principal = new DefaultUser("username");
principal.setId("user#1");
User user = new User();
user.setReferenceId(ORGANIZATION_ID);
user.setReferenceType(ReferenceType.ORGANIZATION);
Role environmentUserRole = new Role();
environmentUserRole.setId("role#1");
when(membershipRepository.findByCriteria(eq(ReferenceType.ENVIRONMENT), eq("env#1"), any(MembershipCriteria.class))).thenReturn(Flowable.just(new Membership()));
TestObserver<Void> completable = membershipService.addEnvironmentUserRoleIfNecessary("orga#1", "env#1", membership, principal).test();
completable.awaitTerminalEvent();
completable.assertNoErrors();
completable.assertComplete();
verify(membershipRepository, times(0)).create(any());
verifyZeroInteractions(auditService);
}
use of io.gravitee.am.service.model.NewMembership 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.service.model.NewMembership in project gravitee-access-management by gravitee-io.
the class MembersResourceTest method shouldNotAddMember_organizationNotFound.
@Test
public void shouldNotAddMember_organizationNotFound() {
final String organizationId = "orga-1";
doReturn(Single.error(new OrganizationNotFoundException(organizationId))).when(organizationService).findById(organizationId);
NewMembership newMembership = new NewMembership();
newMembership.setMemberId("member#1");
newMembership.setMemberType(MemberType.USER);
newMembership.setRole("role#1");
final Response response = target("/organizations").path(organizationId).path("members").request().post(Entity.json(newMembership));
assertEquals(HttpStatusCode.NOT_FOUND_404, response.getStatus());
}
use of io.gravitee.am.service.model.NewMembership 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.service.model.NewMembership in project gravitee-access-management by gravitee-io.
the class MembersResource method addOrUpdateMember.
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Add or update an organization member", notes = "User must have ORGANIZATION_MEMBER[READ] 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, @Valid @NotNull NewMembership newMembership, @Suspended final AsyncResponse response) {
final User authenticatedUser = getAuthenticatedUser();
final Membership membership = convert(newMembership);
membership.setReferenceId(organizationId);
membership.setReferenceType(ReferenceType.ORGANIZATION);
checkPermission(ReferenceType.ORGANIZATION, organizationId, Permission.ORGANIZATION_MEMBER, Acl.CREATE).andThen(organizationService.findById(organizationId).flatMap(organization -> membershipService.addOrUpdate(organizationId, membership, authenticatedUser)).map(membership1 -> Response.created(URI.create("/organizations/" + organizationId + "/members/" + membership1.getId())).entity(membership1).build())).subscribe(response::resume, response::resume);
}
Aggregations