Search in sources :

Example 1 with MembershipPayload

use of io.gravitee.cockpit.api.command.membership.MembershipPayload in project gravitee-management-rest-api by gravitee-io.

the class MembershipCommandHandler method handle.

@Override
public Single<MembershipReply> handle(MembershipCommand command) {
    MembershipPayload membershipPayload = command.getPayload();
    GraviteeContext.setCurrentOrganization(membershipPayload.getOrganizationId());
    try {
        RoleScope roleScope;
        MembershipReferenceType membershipReferenceType;
        try {
            roleScope = RoleScope.valueOf(membershipPayload.getReferenceType());
            membershipReferenceType = MembershipReferenceType.valueOf(membershipPayload.getReferenceType());
        } catch (Exception e) {
            logger.error("Invalid referenceType [{}].", membershipPayload.getReferenceType());
            return Single.just(new MembershipReply(command.getId(), CommandStatus.ERROR));
        }
        final UserEntity userEntity = userService.findBySource(COCKPIT_SOURCE, membershipPayload.getUserId(), false);
        final RoleEntity roleEntity = findRole(roleScope, membershipPayload.getRole());
        final MembershipService.MembershipReference membershipReference = new MembershipService.MembershipReference(membershipReferenceType, membershipPayload.getReferenceId());
        final MembershipService.MembershipMember membershipMember = new MembershipService.MembershipMember(userEntity.getId(), null, MembershipMemberType.USER);
        final MembershipService.MembershipRole membershipRole = new MembershipService.MembershipRole(roleEntity.getScope(), roleEntity.getName());
        membershipService.updateRolesToMemberOnReference(membershipReference, membershipMember, Collections.singletonList(membershipRole), COCKPIT_SOURCE, false);
        logger.info("Role [{}] assigned on {} [{}] for user [{}] and organization [{}].", membershipPayload.getRole(), membershipPayload.getReferenceType(), membershipPayload.getReferenceId(), userEntity.getId(), membershipPayload.getOrganizationId());
        return Single.just(new MembershipReply(command.getId(), CommandStatus.SUCCEEDED));
    } catch (Exception e) {
        logger.error("Error occurred when trying to assign role [{}] on {} [{}] for cockpit user [{}] and organization [{}].", membershipPayload.getRole(), membershipPayload.getReferenceType(), membershipPayload.getReferenceId(), membershipPayload.getUserId(), membershipPayload.getOrganizationId(), e);
        return Single.just(new MembershipReply(command.getId(), CommandStatus.ERROR));
    } finally {
        GraviteeContext.cleanContext();
    }
}
Also used : MembershipReply(io.gravitee.cockpit.api.command.membership.MembershipReply) RoleNotFoundException(io.gravitee.rest.api.service.exceptions.RoleNotFoundException) UserEntity(io.gravitee.rest.api.model.UserEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) RoleScope(io.gravitee.rest.api.model.permissions.RoleScope) MembershipService(io.gravitee.rest.api.service.MembershipService) MembershipPayload(io.gravitee.cockpit.api.command.membership.MembershipPayload) MembershipReferenceType(io.gravitee.rest.api.model.MembershipReferenceType)

Example 2 with MembershipPayload

use of io.gravitee.cockpit.api.command.membership.MembershipPayload in project gravitee-management-rest-api by gravitee-io.

the class MembershipCommandHandlerTest method handleWithRole.

@Test
public void handleWithRole() {
    MembershipPayload membershipPayload = new MembershipPayload();
    membershipPayload.setUserId("user#1");
    membershipPayload.setOrganizationId("orga#1");
    membershipPayload.setReferenceType(MembershipReferenceType.ORGANIZATION.name());
    membershipPayload.setReferenceId("orga#1");
    membershipPayload.setRole("ORGANIZATION_OWNER");
    MembershipCommand command = new MembershipCommand(membershipPayload);
    UserEntity user = new UserEntity();
    user.setId(UUID.random().toString());
    RoleEntity role = new RoleEntity();
    role.setId(UUID.random().toString());
    role.setScope(RoleScope.ORGANIZATION);
    role.setName("ADMIN");
    when(userService.findBySource(COCKPIT_SOURCE, membershipPayload.getUserId(), false)).thenReturn(user);
    when(roleService.findByScopeAndName(RoleScope.ORGANIZATION, "ADMIN")).thenReturn(Optional.of(role));
    TestObserver<MembershipReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertNoErrors();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
    ArgumentCaptor<MembershipService.MembershipReference> membershipReference = ArgumentCaptor.forClass(MembershipService.MembershipReference.class);
    ArgumentCaptor<MembershipService.MembershipMember> membershipMember = ArgumentCaptor.forClass(MembershipService.MembershipMember.class);
    ArgumentCaptor<List<MembershipService.MembershipRole>> membershipRoles = ArgumentCaptor.forClass(List.class);
    verify(membershipService).updateRolesToMemberOnReference(membershipReference.capture(), membershipMember.capture(), membershipRoles.capture(), eq(COCKPIT_SOURCE), eq(false));
    assertEquals(MembershipReferenceType.ORGANIZATION, membershipReference.getValue().getType());
    assertEquals(membershipPayload.getReferenceId(), membershipReference.getValue().getId());
    assertEquals(MembershipMemberType.USER, membershipMember.getValue().getMemberType());
    assertEquals(user.getId(), membershipMember.getValue().getMemberId());
    assertTrue(membershipRoles.getValue().size() == 1 && membershipRoles.getValue().stream().allMatch(membershipRole -> membershipRole.getScope() == role.getScope() && membershipRole.getName().equals(role.getName())));
}
Also used : MembershipReply(io.gravitee.cockpit.api.command.membership.MembershipReply) UserEntity(io.gravitee.rest.api.model.UserEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipService(io.gravitee.rest.api.service.MembershipService) MembershipPayload(io.gravitee.cockpit.api.command.membership.MembershipPayload) List(java.util.List) MembershipCommand(io.gravitee.cockpit.api.command.membership.MembershipCommand) Test(org.junit.Test)

Example 3 with MembershipPayload

use of io.gravitee.cockpit.api.command.membership.MembershipPayload in project gravitee-management-rest-api by gravitee-io.

the class MembershipCommandHandlerTest method handleWithUserRole.

@Test
public void handleWithUserRole() {
    MembershipPayload membershipPayload = new MembershipPayload();
    membershipPayload.setUserId("user#1");
    membershipPayload.setOrganizationId("orga#1");
    membershipPayload.setReferenceType(MembershipReferenceType.ENVIRONMENT.name());
    membershipPayload.setReferenceId("env#1");
    membershipPayload.setRole("ENVIRONMENT_USER");
    MembershipCommand command = new MembershipCommand(membershipPayload);
    UserEntity user = new UserEntity();
    user.setId(UUID.random().toString());
    RoleEntity role = new RoleEntity();
    role.setId(UUID.random().toString());
    role.setScope(RoleScope.ENVIRONMENT);
    role.setName("USER");
    when(userService.findBySource(COCKPIT_SOURCE, membershipPayload.getUserId(), false)).thenReturn(user);
    when(roleService.findByScopeAndName(RoleScope.ENVIRONMENT, "USER")).thenReturn(Optional.of(role));
    TestObserver<MembershipReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertNoErrors();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
    ArgumentCaptor<MembershipService.MembershipReference> membershipReference = ArgumentCaptor.forClass(MembershipService.MembershipReference.class);
    ArgumentCaptor<MembershipService.MembershipMember> membershipMember = ArgumentCaptor.forClass(MembershipService.MembershipMember.class);
    ArgumentCaptor<List<MembershipService.MembershipRole>> membershipRoles = ArgumentCaptor.forClass(List.class);
    verify(membershipService).updateRolesToMemberOnReference(membershipReference.capture(), membershipMember.capture(), membershipRoles.capture(), eq(COCKPIT_SOURCE), eq(false));
    assertEquals(MembershipReferenceType.ENVIRONMENT, membershipReference.getValue().getType());
    assertEquals(membershipPayload.getReferenceId(), membershipReference.getValue().getId());
    assertEquals(MembershipMemberType.USER, membershipMember.getValue().getMemberType());
    assertEquals(user.getId(), membershipMember.getValue().getMemberId());
    assertTrue(membershipRoles.getValue().size() == 1 && membershipRoles.getValue().stream().allMatch(membershipRole -> membershipRole.getScope() == role.getScope() && membershipRole.getName().equals(role.getName())));
}
Also used : MembershipReply(io.gravitee.cockpit.api.command.membership.MembershipReply) UserEntity(io.gravitee.rest.api.model.UserEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipService(io.gravitee.rest.api.service.MembershipService) MembershipPayload(io.gravitee.cockpit.api.command.membership.MembershipPayload) List(java.util.List) MembershipCommand(io.gravitee.cockpit.api.command.membership.MembershipCommand) Test(org.junit.Test)

Example 4 with MembershipPayload

use of io.gravitee.cockpit.api.command.membership.MembershipPayload in project gravitee-management-rest-api by gravitee-io.

the class MembershipCommandHandlerTest method handleWithAdminRole.

@Test
public void handleWithAdminRole() {
    MembershipPayload membershipPayload = new MembershipPayload();
    membershipPayload.setUserId("user#1");
    membershipPayload.setOrganizationId("orga#1");
    membershipPayload.setReferenceType(MembershipReferenceType.ENVIRONMENT.name());
    membershipPayload.setReferenceId("env#1");
    membershipPayload.setRole("ENVIRONMENT_PRIMARY_OWNER");
    MembershipCommand command = new MembershipCommand(membershipPayload);
    UserEntity user = new UserEntity();
    user.setId(UUID.random().toString());
    RoleEntity role = new RoleEntity();
    role.setId(UUID.random().toString());
    role.setScope(RoleScope.ENVIRONMENT);
    role.setName("ADMIN");
    when(userService.findBySource(COCKPIT_SOURCE, membershipPayload.getUserId(), false)).thenReturn(user);
    when(roleService.findByScopeAndName(RoleScope.ENVIRONMENT, "ADMIN")).thenReturn(Optional.of(role));
    TestObserver<MembershipReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertNoErrors();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
    ArgumentCaptor<MembershipService.MembershipReference> membershipReference = ArgumentCaptor.forClass(MembershipService.MembershipReference.class);
    ArgumentCaptor<MembershipService.MembershipMember> membershipMember = ArgumentCaptor.forClass(MembershipService.MembershipMember.class);
    ArgumentCaptor<List<MembershipService.MembershipRole>> membershipRoles = ArgumentCaptor.forClass(List.class);
    verify(membershipService).updateRolesToMemberOnReference(membershipReference.capture(), membershipMember.capture(), membershipRoles.capture(), eq(COCKPIT_SOURCE), eq(false));
    assertEquals(MembershipReferenceType.ENVIRONMENT, membershipReference.getValue().getType());
    assertEquals(membershipPayload.getReferenceId(), membershipReference.getValue().getId());
    assertEquals(MembershipMemberType.USER, membershipMember.getValue().getMemberType());
    assertEquals(user.getId(), membershipMember.getValue().getMemberId());
    assertTrue(membershipRoles.getValue().size() == 1 && membershipRoles.getValue().stream().allMatch(membershipRole -> membershipRole.getScope() == role.getScope() && membershipRole.getName().equals(role.getName())));
}
Also used : MembershipReply(io.gravitee.cockpit.api.command.membership.MembershipReply) UserEntity(io.gravitee.rest.api.model.UserEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipService(io.gravitee.rest.api.service.MembershipService) MembershipPayload(io.gravitee.cockpit.api.command.membership.MembershipPayload) List(java.util.List) MembershipCommand(io.gravitee.cockpit.api.command.membership.MembershipCommand) Test(org.junit.Test)

Example 5 with MembershipPayload

use of io.gravitee.cockpit.api.command.membership.MembershipPayload in project gravitee-management-rest-api by gravitee-io.

the class MembershipCommandHandlerTest method handleWithUnknownRole.

@Test
public void handleWithUnknownRole() {
    MembershipPayload membershipPayload = new MembershipPayload();
    membershipPayload.setUserId("user#1");
    membershipPayload.setOrganizationId("orga#1");
    membershipPayload.setReferenceType(MembershipReferenceType.ENVIRONMENT.name());
    membershipPayload.setReferenceId("env#1");
    membershipPayload.setRole("UNKNOWN");
    MembershipCommand command = new MembershipCommand(membershipPayload);
    UserEntity user = new UserEntity();
    user.setId(UUID.random().toString());
    RoleEntity role = new RoleEntity();
    role.setId(UUID.random().toString());
    when(userService.findBySource(COCKPIT_SOURCE, membershipPayload.getUserId(), false)).thenReturn(user);
    when(roleService.findByScopeAndName(RoleScope.ENVIRONMENT, "UNKNOWN")).thenReturn(Optional.empty());
    TestObserver<MembershipReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertNoErrors();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.ERROR));
    verifyZeroInteractions(membershipService);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) MembershipReply(io.gravitee.cockpit.api.command.membership.MembershipReply) MembershipPayload(io.gravitee.cockpit.api.command.membership.MembershipPayload) UserEntity(io.gravitee.rest.api.model.UserEntity) MembershipCommand(io.gravitee.cockpit.api.command.membership.MembershipCommand) Test(org.junit.Test)

Aggregations

MembershipPayload (io.gravitee.cockpit.api.command.membership.MembershipPayload)6 MembershipReply (io.gravitee.cockpit.api.command.membership.MembershipReply)6 MembershipCommand (io.gravitee.cockpit.api.command.membership.MembershipCommand)5 RoleEntity (io.gravitee.rest.api.model.RoleEntity)5 UserEntity (io.gravitee.rest.api.model.UserEntity)5 Test (org.junit.Test)5 MembershipService (io.gravitee.rest.api.service.MembershipService)4 List (java.util.List)3 MembershipReferenceType (io.gravitee.rest.api.model.MembershipReferenceType)1 RoleScope (io.gravitee.rest.api.model.permissions.RoleScope)1 RoleNotFoundException (io.gravitee.rest.api.service.exceptions.RoleNotFoundException)1 UserNotFoundException (io.gravitee.rest.api.service.exceptions.UserNotFoundException)1