Search in sources :

Example 1 with RolePermissionAction

use of io.gravitee.management.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.PORTAL);
    when(roleMock.getName()).thenReturn("name");
    when(roleMock.getPermissions()).thenReturn(new int[] { perm });
    when(mockRoleRepository.findById(RoleScope.PORTAL, "name")).thenReturn(Optional.of(roleMock));
    RoleEntity entity = roleService.findById(RoleScope.PORTAL, "name");
    assertNotNull("no entity found", entity);
    assertEquals("invalid scope", io.gravitee.management.model.permissions.RoleScope.PORTAL, 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()));
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) RoleEntity(io.gravitee.management.model.RoleEntity) RolePermissionAction(io.gravitee.management.model.permissions.RolePermissionAction)

Example 2 with RolePermissionAction

use of io.gravitee.management.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, false);
    doReturn(false).when(pageService).isDisplayable(apiMock, pageMock.isPublished(), USER_NAME);
    doReturn(true).when(roleService).hasPermission(any(), eq(ApiPermission.DOCUMENTATION), eq(new RolePermissionAction[] { RolePermissionAction.READ }));
    when(groupService.isUserAuthorizedToAccessApiData(any(), any(), any())).thenReturn(Boolean.FALSE);
    when(permissionService.hasPermission(any(), any(), any())).thenReturn(true);
    final Response response = target().request().get();
    assertEquals(UNAUTHORIZED_401, response.getStatus());
    verify(apiService, atLeastOnce()).findById(API_NAME);
    verify(pageService, times(1)).findById(PAGE_NAME, false);
    verify(pageService, times(1)).isDisplayable(apiMock, pageMock.isPublished(), USER_NAME);
}
Also used : PageEntity(io.gravitee.management.model.PageEntity) Response(javax.ws.rs.core.Response) ApiEntity(io.gravitee.management.model.ApiEntity) RolePermissionAction(io.gravitee.management.model.permissions.RolePermissionAction) Test(org.junit.Test)

Example 3 with RolePermissionAction

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

the class RoleServiceImpl method convertPermissions.

private Map<String, char[]> convertPermissions(io.gravitee.management.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;
}
Also used : RolePermissionAction(io.gravitee.management.model.permissions.RolePermissionAction)

Aggregations

RolePermissionAction (io.gravitee.management.model.permissions.RolePermissionAction)3 ApiEntity (io.gravitee.management.model.ApiEntity)1 PageEntity (io.gravitee.management.model.PageEntity)1 RoleEntity (io.gravitee.management.model.RoleEntity)1 Role (io.gravitee.repository.management.model.Role)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1