Search in sources :

Example 1 with RolePermissionAction

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);
}
Also used : PageEntity(io.gravitee.rest.api.model.PageEntity) Response(javax.ws.rs.core.Response) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction) Test(org.junit.Test)

Example 2 with RolePermissionAction

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;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UuidString(io.gravitee.rest.api.service.common.UuidString) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction)

Example 3 with RolePermissionAction

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()));
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) RoleEntity(io.gravitee.rest.api.model.RoleEntity) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction)

Aggregations

RolePermissionAction (io.gravitee.rest.api.model.permissions.RolePermissionAction)3 Role (io.gravitee.repository.management.model.Role)1 PageEntity (io.gravitee.rest.api.model.PageEntity)1 RoleEntity (io.gravitee.rest.api.model.RoleEntity)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 UuidString (io.gravitee.rest.api.service.common.UuidString)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1