Search in sources :

Example 26 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class MetaFilteringTest method checkReadOnlyResource.

@Test
public void checkReadOnlyResource() throws IOException {
    RegistryEntry entry = boot.getResourceRegistry().getEntry(Task.class);
    ResourceInformation resourceInformation = entry.getResourceInformation();
    Mockito.when(filter.filterResource(Mockito.eq(resourceInformation), Mockito.eq(HttpMethod.GET))).thenReturn(FilterBehavior.NONE);
    Mockito.when(filter.filterResource(Mockito.eq(resourceInformation), Mockito.eq(HttpMethod.POST))).thenReturn(FilterBehavior.FORBIDDEN);
    Mockito.when(filter.filterResource(Mockito.eq(resourceInformation), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.FORBIDDEN);
    Mockito.when(filter.filterResource(Mockito.eq(resourceInformation), Mockito.eq(HttpMethod.DELETE))).thenReturn(FilterBehavior.FORBIDDEN);
    checkResourceMeta(true, false, false, false);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Example 27 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class MetaFilteringTest method checkReadOnlyInsertableResource.

@Test
public void checkReadOnlyInsertableResource() throws IOException {
    RegistryEntry entry = boot.getResourceRegistry().getEntry(Task.class);
    ResourceInformation resourceInformation = entry.getResourceInformation();
    Mockito.when(filter.filterResource(Mockito.eq(resourceInformation), Mockito.eq(HttpMethod.GET))).thenReturn(FilterBehavior.NONE);
    Mockito.when(filter.filterResource(Mockito.eq(resourceInformation), Mockito.eq(HttpMethod.POST))).thenReturn(FilterBehavior.NONE);
    Mockito.when(filter.filterResource(Mockito.eq(resourceInformation), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.FORBIDDEN);
    Mockito.when(filter.filterResource(Mockito.eq(resourceInformation), Mockito.eq(HttpMethod.DELETE))).thenReturn(FilterBehavior.FORBIDDEN);
    checkResourceMeta(true, true, false, false);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Example 28 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class MetaFilteringTest method checkReadOnlyAttribute.

@Test
public void checkReadOnlyAttribute() throws IOException {
    RegistryEntry entry = boot.getResourceRegistry().getEntry(Task.class);
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
    Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.POST))).thenReturn(FilterBehavior.FORBIDDEN);
    Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.eq(HttpMethod.PATCH))).thenReturn(FilterBehavior.FORBIDDEN);
    QuerySpec querySpec = new QuerySpec(MetaResource.class);
    querySpec.addFilter(new FilterSpec(Arrays.asList("name"), FilterOperator.EQ, "Tasks"));
    ResourceList<MetaResource> list = repository.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    MetaResource taskMeta = list.get(0);
    Assert.assertTrue(taskMeta.hasAttribute("project"));
    MetaAttribute projectAttr = taskMeta.getAttribute("project");
    Assert.assertFalse(projectAttr.isInsertable());
    Assert.assertFalse(projectAttr.isUpdatable());
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaResource(io.crnk.meta.model.resource.MetaResource) MetaAttribute(io.crnk.meta.model.MetaAttribute) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Test(org.junit.Test)

Example 29 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class ResourceMetaFilter method getResourceInformation.

private ResourceInformation getResourceInformation(MetaResourceBase meta, boolean allowNull) {
    ResourceRegistry resourceRegistry = context.getModuleContext().getResourceRegistry();
    if (meta instanceof MetaResource) {
        RegistryEntry entry = resourceRegistry.getEntry(((MetaResource) meta).getResourceType());
        if (entry != null) {
            return entry.getResourceInformation();
        }
    }
    Class<?> resourceClass = meta.getImplementationClass();
    ResourceInformationProvider infoBuilder = context.getModuleContext().getResourceInformationBuilder();
    if (infoBuilder.accept(resourceClass)) {
        return infoBuilder.build(resourceClass);
    }
    if (allowNull) {
        return null;
    }
    throw new IllegalStateException("failed to get information for " + resourceClass.getName());
}
Also used : MetaResource(io.crnk.meta.model.resource.MetaResource) ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) ResourceInformationProvider(io.crnk.core.engine.information.resource.ResourceInformationProvider) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 30 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class ResourceMetaFilter method adjustResourceForRequest.

private MetaElement adjustResourceForRequest(MetaResource metaResource) {
    Module.ModuleContext moduleContext = context.getModuleContext();
    RegistryEntry entry = moduleContext.getResourceRegistry().getEntry(metaResource.getResourceType());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceFilterDirectory filterBehaviorProvider = moduleContext.getResourceFilterDirectory();
    boolean readable = metaResource.isReadable() && filterBehaviorProvider.get(resourceInformation, HttpMethod.GET) == FilterBehavior.NONE;
    boolean insertable = metaResource.isInsertable() && filterBehaviorProvider.get(resourceInformation, HttpMethod.POST) == FilterBehavior.NONE;
    boolean updatable = metaResource.isUpdatable() && filterBehaviorProvider.get(resourceInformation, HttpMethod.PATCH) == FilterBehavior.NONE;
    boolean deletable = metaResource.isDeletable() && filterBehaviorProvider.get(resourceInformation, HttpMethod.DELETE) == FilterBehavior.NONE;
    // hide element if no permission
    if (!readable && !insertable && !updatable && !deletable) {
        return null;
    }
    // update element if necessary
    if (metaResource.isReadable() != readable || metaResource.isUpdatable() != updatable || metaResource.isInsertable() != insertable || metaResource.isDeletable() != deletable) {
        MetaResource clone = (MetaResource) metaResource.duplicate();
        clone.setReadable(readable);
        clone.setInsertable(insertable);
        clone.setUpdatable(updatable);
        clone.setDeletable(deletable);
        return clone;
    }
    return metaResource;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaResource(io.crnk.meta.model.resource.MetaResource) Module(io.crnk.core.module.Module) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceFilterDirectory(io.crnk.core.engine.filter.ResourceFilterDirectory)

Aggregations

RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)119 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)60 Test (org.junit.Test)38 ResourceField (io.crnk.core.engine.information.resource.ResourceField)36 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)19 QuerySpec (io.crnk.core.queryspec.QuerySpec)18 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 Serializable (java.io.Serializable)14 Task (io.crnk.core.mock.models.Task)13 Response (io.crnk.core.engine.dispatcher.Response)12 Document (io.crnk.core.engine.document.Document)11 Resource (io.crnk.core.engine.document.Resource)11 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)10 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)9 FilterSpec (io.crnk.core.queryspec.FilterSpec)9 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)8 Before (org.junit.Before)8 Collection (java.util.Collection)7 HashSet (java.util.HashSet)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)6