Search in sources :

Example 1 with NewMembership

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);
}
Also used : SystemRole(io.gravitee.am.model.permissions.SystemRole) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) NewMembership(io.gravitee.am.service.model.NewMembership) MembershipCriteria(io.gravitee.am.repository.management.api.search.MembershipCriteria) NewMembership(io.gravitee.am.service.model.NewMembership) Test(org.junit.Test)

Example 2 with NewMembership

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);
}
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 3 with NewMembership

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());
}
Also used : Response(javax.ws.rs.core.Response) OrganizationNotFoundException(io.gravitee.am.service.exception.OrganizationNotFoundException) NewMembership(io.gravitee.am.service.model.NewMembership) Test(org.junit.Test) JerseySpringTest(io.gravitee.am.management.handlers.management.api.JerseySpringTest)

Example 4 with NewMembership

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

Example 5 with NewMembership

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);
}
Also used : Permission(io.gravitee.am.model.permissions.Permission) 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) 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) URI(java.net.URI) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) MembershipService(io.gravitee.am.service.MembershipService) Context(javax.ws.rs.core.Context) AsyncResponse(javax.ws.rs.container.AsyncResponse) Membership(io.gravitee.am.model.Membership) NotNull(javax.validation.constraints.NotNull) Suspended(javax.ws.rs.container.Suspended) MediaType(io.gravitee.common.http.MediaType) OrganizationService(io.gravitee.am.service.OrganizationService) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) ResourceContext(javax.ws.rs.container.ResourceContext) User(io.gravitee.am.identityprovider.api.User) NewMembership(io.gravitee.am.service.model.NewMembership) Membership(io.gravitee.am.model.Membership) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

NewMembership (io.gravitee.am.service.model.NewMembership)14 Membership (io.gravitee.am.model.Membership)8 Test (org.junit.Test)7 MembershipCriteria (io.gravitee.am.repository.management.api.search.MembershipCriteria)6 Response (javax.ws.rs.core.Response)6 DefaultRole (io.gravitee.am.model.permissions.DefaultRole)5 DefaultUser (io.gravitee.am.identityprovider.api.DefaultUser)4 SystemRole (io.gravitee.am.model.permissions.SystemRole)4 User (io.gravitee.am.identityprovider.api.User)3 JerseySpringTest (io.gravitee.am.management.handlers.management.api.JerseySpringTest)3 MembershipListItem (io.gravitee.am.management.handlers.management.api.model.MembershipListItem)3 AbstractResource (io.gravitee.am.management.handlers.management.api.resources.AbstractResource)3 Acl (io.gravitee.am.model.Acl)3 ReferenceType (io.gravitee.am.model.ReferenceType)3 Permission (io.gravitee.am.model.permissions.Permission)3 MembershipService (io.gravitee.am.service.MembershipService)3 MediaType (io.gravitee.common.http.MediaType)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponse (io.swagger.annotations.ApiResponse)3 ApiResponses (io.swagger.annotations.ApiResponses)3