Search in sources :

Example 1 with MemberEntity

use of io.gravitee.management.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.

the class MembershipService_GetMembersTest method shouldGetMembersWithMembership.

@Test
public void shouldGetMembersWithMembership() throws Exception {
    Membership membership = new Membership();
    membership.setReferenceId(API_ID);
    membership.setCreatedAt(new Date());
    membership.setUpdatedAt(membership.getCreatedAt());
    membership.setReferenceType(MembershipReferenceType.API);
    membership.setRoles(Collections.singletonMap(RoleScope.API.getId(), SystemRole.PRIMARY_OWNER.name()));
    membership.setUserId("user-id");
    UserEntity userEntity = new UserEntity();
    userEntity.setUsername(membership.getUserId());
    userEntity.setFirstname("John");
    userEntity.setLastname("Doe");
    RoleEntity po = mock(RoleEntity.class);
    po.setScope(io.gravitee.management.model.permissions.RoleScope.API);
    po.setName(SystemRole.PRIMARY_OWNER.name());
    when(membershipRepository.findByReferenceAndRole(MembershipReferenceType.API, API_ID, RoleScope.API, SystemRole.PRIMARY_OWNER.name())).thenReturn(Collections.singleton(membership));
    when(userService.findById(membership.getUserId())).thenReturn(userEntity);
    when(membershipRepository.findById(userEntity.getUsername(), MembershipReferenceType.API, API_ID)).thenReturn(of(membership));
    when(roleService.findById(RoleScope.API, SystemRole.PRIMARY_OWNER.name())).thenReturn(po);
    Set<MemberEntity> members = membershipService.getMembers(MembershipReferenceType.API, API_ID, RoleScope.API, SystemRole.PRIMARY_OWNER.name());
    Assert.assertNotNull(members);
    Assert.assertFalse("members must not be empty", members.isEmpty());
    verify(membershipRepository, times(1)).findByReferenceAndRole(MembershipReferenceType.API, API_ID, RoleScope.API, SystemRole.PRIMARY_OWNER.name());
    verify(userService, times(1)).findById(membership.getUserId());
}
Also used : RoleEntity(io.gravitee.management.model.RoleEntity) Membership(io.gravitee.repository.management.model.Membership) MemberEntity(io.gravitee.management.model.MemberEntity) Date(java.util.Date) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 2 with MemberEntity

use of io.gravitee.management.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.

the class MembershipService_GetMembersTest method shouldGetMembersWithoutMembership.

@Test
public void shouldGetMembersWithoutMembership() throws Exception {
    Membership membership = new Membership();
    membership.setReferenceId(API_ID);
    membership.setCreatedAt(new Date());
    membership.setUpdatedAt(membership.getCreatedAt());
    membership.setReferenceType(MembershipReferenceType.API);
    membership.setRoles(Collections.singletonMap(RoleScope.API.getId(), SystemRole.PRIMARY_OWNER.name()));
    membership.setUserId("user-id");
    UserEntity userEntity = new UserEntity();
    userEntity.setUsername(membership.getUserId());
    userEntity.setFirstname("John");
    userEntity.setLastname("Doe");
    RoleEntity po = mock(RoleEntity.class);
    po.setScope(io.gravitee.management.model.permissions.RoleScope.API);
    po.setName(SystemRole.PRIMARY_OWNER.name());
    when(membershipRepository.findByReferenceAndRole(MembershipReferenceType.API, API_ID, RoleScope.API, null)).thenReturn(Collections.singleton(membership));
    when(userService.findById(membership.getUserId())).thenReturn(userEntity);
    when(membershipRepository.findById(userEntity.getUsername(), MembershipReferenceType.API, API_ID)).thenReturn(of(membership));
    when(roleService.findById(RoleScope.API, SystemRole.PRIMARY_OWNER.name())).thenReturn(po);
    Set<MemberEntity> members = membershipService.getMembers(MembershipReferenceType.API, API_ID, RoleScope.API);
    Assert.assertNotNull(members);
    Assert.assertFalse("members must not be empty", members.isEmpty());
    verify(membershipRepository, times(1)).findByReferenceAndRole(MembershipReferenceType.API, API_ID, RoleScope.API, null);
    verify(userService, times(1)).findById(membership.getUserId());
}
Also used : RoleEntity(io.gravitee.management.model.RoleEntity) Membership(io.gravitee.repository.management.model.Membership) MemberEntity(io.gravitee.management.model.MemberEntity) Date(java.util.Date) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 3 with MemberEntity

use of io.gravitee.management.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.

the class GroupMembersResource method addOrUpdateMember.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Add or update a group member")
@ApiResponses({ @ApiResponse(code = 201, message = "Member has been added"), @ApiResponse(code = 200, message = "Member has been updated"), @ApiResponse(code = 400, message = "Membership is not valid"), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.MANAGEMENT_GROUP, acls = RolePermissionAction.CREATE), @Permission(value = RolePermission.MANAGEMENT_GROUP, acls = RolePermissionAction.UPDATE) })
public Response addOrUpdateMember(@PathParam("group") String group, @Valid @NotNull final GroupMembership membership) {
    // Check that group exists
    groupService.findById(group);
    RoleEntity previousApiRole = null, previousApplicationRole = null;
    if (membership.getId() != null) {
        previousApiRole = membershipService.getRole(MembershipReferenceType.GROUP, group, membership.getId(), RoleScope.API);
        previousApplicationRole = membershipService.getRole(MembershipReferenceType.GROUP, group, membership.getId(), RoleScope.APPLICATION);
    }
    // Process add / update before delete to avoid having a user without role
    if (membership.getRoles() != null && !membership.getRoles().isEmpty()) {
        MemberRoleEntity apiRole = membership.getRoles().stream().filter(r -> r.getRoleScope().equals(io.gravitee.management.model.permissions.RoleScope.API) && !r.getRoleName().isEmpty()).findFirst().orElse(null);
        MemberRoleEntity applicationRole = membership.getRoles().stream().filter(r -> r.getRoleScope().equals(io.gravitee.management.model.permissions.RoleScope.APPLICATION) && !r.getRoleName().isEmpty()).findFirst().orElse(null);
        MemberEntity updatedMembership = null;
        // Add / Update
        if (apiRole != null) {
            updatedMembership = membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, group), new MembershipService.MembershipUser(membership.getId(), membership.getReference()), new MembershipService.MembershipRole(RoleScope.API, apiRole.getRoleName()));
        }
        if (applicationRole != null) {
            updatedMembership = membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, group), new MembershipService.MembershipUser(membership.getId(), membership.getReference()), new MembershipService.MembershipRole(RoleScope.APPLICATION, applicationRole.getRoleName()));
        }
        // Delete
        if (apiRole == null && previousApiRole != null) {
            membershipService.removeRole(MembershipReferenceType.GROUP, group, updatedMembership.getId(), RoleScope.API);
        }
        if (applicationRole == null && previousApplicationRole != null) {
            membershipService.removeRole(MembershipReferenceType.GROUP, group, updatedMembership.getId(), RoleScope.APPLICATION);
        }
    }
    return Response.ok().build();
}
Also used : RoleEntity(io.gravitee.management.model.RoleEntity) MemberRoleEntity(io.gravitee.management.model.MemberRoleEntity) MemberRoleEntity(io.gravitee.management.model.MemberRoleEntity) GroupMemberEntity(io.gravitee.management.model.GroupMemberEntity) MemberEntity(io.gravitee.management.model.MemberEntity) ApiOperation(io.swagger.annotations.ApiOperation) Permissions(io.gravitee.management.rest.security.Permissions) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with MemberEntity

use of io.gravitee.management.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiMembersResource method addOrUpdateApiMember.

@POST
@ApiOperation(value = "Add or update an API member", notes = "User must have the MANAGE_MEMBERS permission to use this service")
@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") })
@Permissions({ @Permission(value = RolePermission.API_MEMBER, acls = RolePermissionAction.CREATE), @Permission(value = RolePermission.API_MEMBER, acls = RolePermissionAction.UPDATE) })
public Response addOrUpdateApiMember(@PathParam("api") String api, @Valid @NotNull ApiMembership apiMembership) {
    if (PRIMARY_OWNER.name().equals(apiMembership.getRole())) {
        throw new SinglePrimaryOwnerException(RoleScope.API);
    }
    apiService.findById(api);
    MemberEntity membership = membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.API, api), new MembershipService.MembershipUser(apiMembership.getId(), apiMembership.getReference()), new MembershipService.MembershipRole(RoleScope.API, apiMembership.getRole()));
    return Response.created(URI.create("/apis/" + api + "/members/" + membership.getId())).build();
}
Also used : SinglePrimaryOwnerException(io.gravitee.management.service.exceptions.SinglePrimaryOwnerException) MembershipService(io.gravitee.management.service.MembershipService) MemberEntity(io.gravitee.management.model.MemberEntity) Permissions(io.gravitee.management.rest.security.Permissions)

Example 5 with MemberEntity

use of io.gravitee.management.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.

the class GroupService_IsUserAuthorizedToAccessTest method shouldBeAuthorizedForPrivateApiIfMemberOfAuthorizedGroups.

@Test
public void shouldBeAuthorizedForPrivateApiIfMemberOfAuthorizedGroups() throws TechnicalException {
    when(api.getVisibility()).thenReturn(Visibility.PRIVATE);
    when(api.getId()).thenReturn("apiId");
    when(api.getGroups()).thenReturn(new HashSet<>(Arrays.asList("grp1", "grp2")));
    when(membershipService.getMember(MembershipReferenceType.API, api.getId(), "user", RoleScope.API)).thenReturn(null);
    when(membershipService.getMember(MembershipReferenceType.GROUP, "grp2", "user", RoleScope.API)).thenReturn(new MemberEntity());
    boolean userAuthorizedToAccess = groupService.isUserAuthorizedToAccessApiData(api, Collections.singletonList("grp1"), "user");
    assertTrue(userAuthorizedToAccess);
    verify(membershipService, times(2)).getMember(any(), any(), any(), any());
    verify(membershipService, times(1)).getMember(MembershipReferenceType.API, api.getId(), "user", RoleScope.API);
    verify(membershipService, times(1)).getMember(MembershipReferenceType.GROUP, "grp2", "user", RoleScope.API);
    verify(api, atLeast(2)).getGroups();
    verify(groupRepository, never()).findAll();
}
Also used : MemberEntity(io.gravitee.management.model.MemberEntity) Test(org.junit.Test)

Aggregations

MemberEntity (io.gravitee.management.model.MemberEntity)9 Test (org.junit.Test)5 RoleEntity (io.gravitee.management.model.RoleEntity)4 Permissions (io.gravitee.management.rest.security.Permissions)4 UserEntity (io.gravitee.management.model.UserEntity)3 Membership (io.gravitee.repository.management.model.Membership)3 GroupMemberEntity (io.gravitee.management.model.GroupMemberEntity)2 MembershipService (io.gravitee.management.service.MembershipService)2 SinglePrimaryOwnerException (io.gravitee.management.service.exceptions.SinglePrimaryOwnerException)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 Date (java.util.Date)2 GroupEntity (io.gravitee.management.model.GroupEntity)1 MemberRoleEntity (io.gravitee.management.model.MemberRoleEntity)1 HashMap (java.util.HashMap)1