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());
}
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());
}
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();
}
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();
}
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();
}
Aggregations