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);
}
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);
}
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());
}
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());
}
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;
}
Aggregations