Search in sources :

Example 6 with Membership

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

the class DomainNotificationServiceTest method shouldNotifyUserFromGroup.

@Test
public void shouldNotifyUserFromGroup() throws Exception {
    final Membership member = new Membership();
    member.setMemberType(MemberType.GROUP);
    member.setMemberId("groupId");
    when(membershipService.findByCriteria(eq(ReferenceType.DOMAIN), eq(DOMAIN_ID), any())).thenReturn(Flowable.just(member), Flowable.empty());
    final List<User> tenUsers = IntStream.range(0, 10).mapToObj(x -> {
        final User user = new User();
        user.setId("" + x);
        user.setEmail(x + "@acme.fr");
        return user;
    }).collect(Collectors.toList());
    final User singleUser = new User();
    singleUser.setId("single");
    singleUser.setEmail("single@acme.fr");
    when(groupService.findMembers(any(), any(), any(), anyInt(), anyInt())).thenReturn(Single.just(new Page<>(tenUsers, 0, 11)), Single.just(new Page<>(Arrays.asList(singleUser), 1, 11)));
    when(emailService.getFinalEmail(any(), any(), any(), any(), any())).thenReturn(new Email());
    cut.registerCertificateExpiration(certificate);
    // wait subscription execution
    Thread.sleep(1000);
    verify(notifierService, times(11)).register(any(), any(), any());
    verify(userService, never()).findById(any(), any(), any());
}
Also used : IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Certificate(io.gravitee.am.model.Certificate) EnvironmentService(io.gravitee.am.service.EnvironmentService) DomainNotifierServiceImpl(io.gravitee.am.management.service.impl.DomainNotifierServiceImpl) Arrays(java.util.Arrays) SystemRole(io.gravitee.am.model.permissions.SystemRole) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Role(io.gravitee.am.model.Role) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) DomainService(io.gravitee.am.service.DomainService) Environment(io.gravitee.am.model.Environment) Mockito.argThat(org.mockito.Mockito.argThat) Single(io.reactivex.Single) Flowable(io.reactivex.Flowable) After(org.junit.After) Email(io.gravitee.am.common.email.Email) ReferenceType(io.gravitee.am.model.ReferenceType) User(io.gravitee.am.model.User) GroupService(io.gravitee.am.service.GroupService) MembershipService(io.gravitee.am.service.MembershipService) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) RoleService(io.gravitee.am.service.RoleService) NotifierService(io.gravitee.node.api.notifier.NotifierService) Page(io.gravitee.am.model.common.Page) MemberType(io.gravitee.am.model.membership.MemberType) TYPE_EMAIL_NOTIFIER(io.gravitee.am.management.service.impl.notifications.NotificationDefinitionUtils.TYPE_EMAIL_NOTIFIER) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Domain(io.gravitee.am.model.Domain) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) UUID(java.util.UUID) Mockito.when(org.mockito.Mockito.when) Membership(io.gravitee.am.model.Membership) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) List(java.util.List) Mockito.never(org.mockito.Mockito.never) OrganizationUserService(io.gravitee.am.service.OrganizationUserService) Mockito.anyInt(org.mockito.Mockito.anyInt) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) EmailNotifierConfiguration(io.gravitee.am.management.service.impl.notifications.EmailNotifierConfiguration) TYPE_UI_NOTIFIER(io.gravitee.am.management.service.impl.notifications.NotificationDefinitionUtils.TYPE_UI_NOTIFIER) User(io.gravitee.am.model.User) Email(io.gravitee.am.common.email.Email) Membership(io.gravitee.am.model.Membership) Page(io.gravitee.am.model.common.Page) Test(org.junit.Test)

Example 7 with Membership

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

the class OrganizationUserServiceTest method shouldDeleteUser_with_memberships.

@Test
public void shouldDeleteUser_with_memberships() {
    String organization = "DEFAULT";
    String userId = "user-id";
    User user = new User();
    user.setId(userId);
    user.setSource("source-idp");
    Membership m1 = mock(Membership.class);
    when(m1.getId()).thenReturn("m1");
    Membership m2 = mock(Membership.class);
    when(m2.getId()).thenReturn("m2");
    Membership m3 = mock(Membership.class);
    when(m3.getId()).thenReturn("m3");
    when(commonUserService.findById(any(), any(), any())).thenReturn(Single.just(user));
    when(identityProviderManager.getUserProvider(any())).thenReturn(Maybe.empty());
    when(commonUserService.delete(anyString())).thenReturn(Completable.complete());
    when(membershipService.findByMember(any(), any())).thenReturn(Flowable.just(m1, m2, m3));
    when(membershipService.delete(anyString())).thenReturn(Completable.complete());
    organizationUserService.delete(ReferenceType.ORGANIZATION, organization, userId).test().assertComplete().assertNoErrors();
    verify(commonUserService, times(1)).delete(any());
    verify(membershipService, times(3)).delete(anyString());
}
Also used : User(io.gravitee.am.model.User) NewUser(io.gravitee.am.service.model.NewUser) Membership(io.gravitee.am.model.Membership) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 8 with Membership

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

Example 9 with Membership

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

the class ApplicationMembersResource method convert.

private Membership convert(NewMembership newMembership) {
    Membership membership = new Membership();
    membership.setMemberId(newMembership.getMemberId());
    membership.setMemberType(newMembership.getMemberType());
    membership.setRoleId(newMembership.getRole());
    return membership;
}
Also used : NewMembership(io.gravitee.am.service.model.NewMembership) Membership(io.gravitee.am.model.Membership)

Example 10 with Membership

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

the class MongoMembershipRepository method findByCriteria.

@Override
public Flowable<Membership> findByCriteria(ReferenceType referenceType, String referenceId, MembershipCriteria criteria) {
    Bson eqReference = and(eq(FIELD_REFERENCE_TYPE, referenceType.name()), eq(FIELD_REFERENCE_ID, referenceId));
    Bson eqGroupId = null;
    Bson eqUserId = null;
    if (criteria.getGroupIds().isPresent()) {
        eqGroupId = and(eq(FIELD_MEMBER_TYPE, MemberType.GROUP.name()), in(FIELD_MEMBER_ID, criteria.getGroupIds().get()));
    }
    if (criteria.getUserId().isPresent()) {
        eqUserId = and(eq(FIELD_MEMBER_TYPE, MemberType.USER.name()), eq(FIELD_MEMBER_ID, criteria.getUserId().get()));
    }
    if (criteria.getRoleId().isPresent()) {
        eqUserId = eq(FIELD_ROLE, criteria.getRoleId().get());
    }
    return toBsonFilter(criteria.isLogicalOR(), eqGroupId, eqUserId).map(filter -> and(eqReference, filter)).switchIfEmpty(Single.just(eqReference)).flatMapPublisher(filter -> Flowable.fromPublisher(membershipsCollection.find(filter))).map(this::convert);
}
Also used : Document(org.bson.Document) MembershipMongo(io.gravitee.am.repository.mongodb.management.internal.model.MembershipMongo) MemberType(io.gravitee.am.model.membership.MemberType) MongoCollection(com.mongodb.reactivestreams.client.MongoCollection) Membership(io.gravitee.am.model.Membership) RandomString(io.gravitee.am.common.utils.RandomString) Bson(org.bson.conversions.Bson) Filters(com.mongodb.client.model.Filters) MembershipRepository(io.gravitee.am.repository.management.api.MembershipRepository) Component(org.springframework.stereotype.Component) PostConstruct(javax.annotation.PostConstruct) ReferenceType(io.gravitee.am.model.ReferenceType) MembershipCriteria(io.gravitee.am.repository.management.api.search.MembershipCriteria) io.reactivex(io.reactivex) Bson(org.bson.conversions.Bson)

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