use of io.gravitee.rest.api.model.permissions.RolePermissionAction in project gravitee-management-rest-api by gravitee-io.
the class ApiPagesResourceNotAdminTest method shouldNotGetPrivateApiPublishedPage.
@Test
public void shouldNotGetPrivateApiPublishedPage() {
reset(apiService, pageService, membershipService);
final ApiEntity apiMock = mock(ApiEntity.class);
when(apiMock.getVisibility()).thenReturn(Visibility.PRIVATE);
when(apiMock.getName()).thenReturn(API_NAME);
doReturn(apiMock).when(apiService).findById(API_NAME);
final PageEntity pageMock = new PageEntity();
pageMock.setPublished(true);
pageMock.setName(PAGE_NAME);
doReturn(pageMock).when(pageService).findById(PAGE_NAME, null);
doReturn(true).when(roleService).hasPermission(any(), eq(ApiPermission.DOCUMENTATION), eq(new RolePermissionAction[] { RolePermissionAction.READ }));
when(permissionService.hasPermission(any(), any(), any())).thenReturn(true);
final Response response = envTarget().request().get();
assertEquals(UNAUTHORIZED_401, response.getStatus());
verify(apiService, atLeastOnce()).findById(API_NAME);
verify(pageService, times(1)).findById(PAGE_NAME, null);
}
use of io.gravitee.rest.api.model.permissions.RolePermissionAction in project gravitee-management-rest-api by gravitee-io.
the class RoleServiceImpl method convertPermissions.
private Map<String, char[]> convertPermissions(io.gravitee.rest.api.model.permissions.RoleScope scope, int[] perms) {
if (perms == null) {
return Collections.emptyMap();
}
Map<String, char[]> result = new HashMap<>();
Stream.of(Permission.findByScope(scope)).forEach(perm -> {
for (int action : perms) {
if (action / 100 == perm.getMask() / 100) {
List<Character> crud = new ArrayList<>();
for (RolePermissionAction rolePermissionAction : RolePermissionAction.values()) {
if (((action - perm.getMask()) & rolePermissionAction.getMask()) != 0) {
crud.add(rolePermissionAction.getId());
}
}
result.put(perm.getName(), ArrayUtils.toPrimitive(crud.toArray(new Character[crud.size()])));
}
}
});
return result;
}
use of io.gravitee.rest.api.model.permissions.RolePermissionAction in project gravitee-management-rest-api by gravitee-io.
the class RoleService_FindByIdTest method test_int_to_CRUD.
private void test_int_to_CRUD(int perm, RolePermissionAction... action) throws TechnicalException {
Role roleMock = mock(Role.class);
when(roleMock.getScope()).thenReturn(RoleScope.ENVIRONMENT);
when(roleMock.getId()).thenReturn("id");
when(roleMock.getName()).thenReturn("name");
when(roleMock.getPermissions()).thenReturn(new int[] { perm });
when(mockRoleRepository.findById("id")).thenReturn(Optional.of(roleMock));
RoleEntity entity = roleService.findById("id");
assertNotNull("no entity found", entity);
assertEquals("invalid id", "id", entity.getId());
assertEquals("invalid name", "name", entity.getName());
assertEquals("invalid scope", io.gravitee.rest.api.model.permissions.RoleScope.ENVIRONMENT, entity.getScope());
assertFalse("no permissions found", entity.getPermissions().isEmpty());
assertTrue("invalid Permission name", entity.getPermissions().containsKey(DOCUMENTATION.getName()));
char[] perms = entity.getPermissions().get(DOCUMENTATION.getName());
assertEquals("not enough permissions", action.length, perms.length);
for (RolePermissionAction rolePermissionAction : action) {
assertTrue("not the good permission", Arrays.asList(ArrayUtils.toObject(perms)).contains(rolePermissionAction.getId()));
}
}
Aggregations