use of io.gravitee.management.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.
the class GroupService_IsUserAuthorizedToAccessTest method shouldBeAuthorizedForPrivateApiWithDirectMember.
@Test
public void shouldBeAuthorizedForPrivateApiWithDirectMember() throws TechnicalException {
when(api.getVisibility()).thenReturn(Visibility.PRIVATE);
when(api.getId()).thenReturn("apiId");
when(api.getGroups()).thenReturn(Collections.singleton("grp1"));
when(membershipService.getMember(MembershipReferenceType.API, api.getId(), "user", RoleScope.API)).thenReturn(new MemberEntity());
boolean userAuthorizedToAccess = groupService.isUserAuthorizedToAccessApiData(api, Collections.singletonList("grp1"), "user");
assertTrue(userAuthorizedToAccess);
verify(membershipService, times(1)).getMember(any(), any(), any(), any());
verify(membershipService, times(1)).getMember(MembershipReferenceType.API, api.getId(), "user", RoleScope.API);
verify(api, never()).getGroups();
verify(groupRepository, never()).findAll();
}
use of io.gravitee.management.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_AddOrUpdateMemberTest method shouldUpdateApiGroupMembership.
@Test
public void shouldUpdateApiGroupMembership() throws Exception {
UserEntity userEntity = new UserEntity();
userEntity.setId("my name");
userEntity.setUsername("my name");
userEntity.setEmail("me@mail.com");
Membership membership = new Membership();
membership.setUserId(userEntity.getUsername());
membership.setReferenceType(MembershipReferenceType.GROUP);
membership.setReferenceId(GROUP_ID);
Map<Integer, String> roles = new HashMap<>();
roles.put(RoleScope.API.getId(), "USER");
membership.setRoles(roles);
Membership newMembership = new Membership();
newMembership.setUserId(userEntity.getUsername());
newMembership.setReferenceType(MembershipReferenceType.GROUP);
newMembership.setReferenceId(GROUP_ID);
GroupEntity groupEntityMock = mock(GroupEntity.class);
when(groupEntityMock.getName()).thenReturn("foo");
RoleEntity role = mock(RoleEntity.class);
when(role.getScope()).thenReturn(io.gravitee.management.model.permissions.RoleScope.API);
when(roleService.findById(any(), any())).thenReturn(role);
when(userService.findById(userEntity.getId())).thenReturn(userEntity);
when(groupService.findById(GROUP_ID)).thenReturn(groupEntityMock);
when(membershipRepository.findById(userEntity.getId(), MembershipReferenceType.GROUP, GROUP_ID)).thenReturn(of(membership));
when(membershipRepository.update(any())).thenReturn(newMembership);
MemberEntity updateMember = membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(userEntity.getUsername(), null), new MembershipService.MembershipRole(RoleScope.API, "OWNER"));
verify(userService, times(2)).findById(userEntity.getId());
verify(membershipRepository, times(2)).findById(userEntity.getId(), MembershipReferenceType.GROUP, GROUP_ID);
verify(membershipRepository, never()).create(any());
verify(membershipRepository, times(1)).update(any());
verify(emailService, never()).sendAsyncEmailNotification(any());
}
use of io.gravitee.management.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.
the class GroupMembersResource method getMembers.
@GET
@Produces(io.gravitee.common.http.MediaType.APPLICATION_JSON)
@ApiOperation(value = "List Group members")
@ApiResponses({ @ApiResponse(code = 200, message = "List of Group's members", response = MemberEntity.class, responseContainer = "List"), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.MANAGEMENT_GROUP, acls = RolePermissionAction.READ) })
public List<GroupMemberEntity> getMembers(@PathParam("group") String group) {
// check that group exists
groupService.findById(group);
Map<String, List<MemberEntity>> membersWithApplicationRole = membershipService.getMembers(MembershipReferenceType.GROUP, group, RoleScope.APPLICATION).stream().filter(Objects::nonNull).collect(Collectors.groupingBy(MemberEntity::getId));
Map<String, List<MemberEntity>> membersWithApiRole = membershipService.getMembers(MembershipReferenceType.GROUP, group, RoleScope.API).stream().filter(Objects::nonNull).collect(Collectors.groupingBy(MemberEntity::getId));
Set<String> ids = new HashSet<>();
ids.addAll(membersWithApiRole.keySet());
ids.addAll(membersWithApplicationRole.keySet());
return ids.stream().map(id -> {
MemberEntity memberWithApiRole = Objects.isNull(membersWithApiRole.get(id)) ? null : membersWithApiRole.get(id).get(0);
MemberEntity memberWithApplicationRole = Objects.isNull(membersWithApplicationRole.get(id)) ? null : membersWithApplicationRole.get(id).get(0);
GroupMemberEntity groupMemberEntity = new GroupMemberEntity(Objects.nonNull(memberWithApiRole) ? memberWithApiRole : memberWithApplicationRole);
groupMemberEntity.setRoles(new HashMap<>());
if (Objects.nonNull(memberWithApiRole)) {
groupMemberEntity.getRoles().put(RoleScope.API.name(), memberWithApiRole.getRole());
}
if (Objects.nonNull(memberWithApplicationRole)) {
groupMemberEntity.getRoles().put(RoleScope.APPLICATION.name(), memberWithApplicationRole.getRole());
}
return groupMemberEntity;
}).sorted(Comparator.comparing(GroupMemberEntity::getId)).collect(Collectors.toList());
}
use of io.gravitee.management.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.
the class ApplicationMembersResource method addOrUpdateApplicationMember.
@POST
@ApiOperation(value = "Add or update an application 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.APPLICATION_MEMBER, acls = RolePermissionAction.CREATE), @Permission(value = RolePermission.APPLICATION_MEMBER, acls = RolePermissionAction.UPDATE) })
public Response addOrUpdateApplicationMember(@PathParam("application") String application, @Valid @NotNull ApplicationMembership applicationMembership) {
if (PRIMARY_OWNER.name().equals(applicationMembership.getRole())) {
throw new SinglePrimaryOwnerException(RoleScope.APPLICATION);
}
applicationService.findById(application);
MemberEntity membership = membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.APPLICATION, application), new MembershipService.MembershipUser(applicationMembership.getId(), applicationMembership.getReference()), new MembershipService.MembershipRole(RoleScope.APPLICATION, applicationMembership.getRole()));
return Response.created(URI.create("/applications/" + application + "/members/" + membership.getId())).build();
}
Aggregations