use of io.crnk.core.engine.filter.FilterBehavior in project crnk-framework by crnk-project.
the class ResourceFilterDirectoryImpl method get.
@Override
public FilterBehavior get(ResourceField field, HttpMethod method) {
Map<Object, FilterBehavior> map = getCache(method);
FilterBehavior behavior = map.get(field);
if (behavior != null) {
return behavior;
}
behavior = FilterBehavior.NONE;
for (ResourceFilter filter : filters) {
behavior = behavior.merge(filter.filterField(field, method));
if (behavior == FilterBehavior.FORBIDDEN) {
break;
}
}
if (field.getResourceFieldType() == ResourceFieldType.RELATIONSHIP) {
// for relationships opposite site must also be accessible (at least with GET)
String oppositeResourceType = field.getOppositeResourceType();
RegistryEntry oppositeRegistryEntry = resourceRegistry.getEntry(oppositeResourceType);
if (oppositeRegistryEntry != null) {
PreconditionUtil.assertNotNull(oppositeResourceType, oppositeRegistryEntry);
ResourceInformation oppositeResourceInformation = oppositeRegistryEntry.getResourceInformation();
// consider checking more than GET? intersection/union of multiple?
behavior = behavior.merge(get(oppositeResourceInformation, HttpMethod.GET));
} else {
LOGGER.warn("opposite side {} not found", oppositeResourceType);
}
}
map.put(field, behavior);
return behavior;
}
use of io.crnk.core.engine.filter.FilterBehavior in project crnk-framework by crnk-project.
the class ResourceFilterDirectoryImpl method getCache.
private Map<Object, FilterBehavior> getCache(HttpMethod method) {
String key = ResourceFilterDirectoryImpl.class.getSimpleName() + method;
HttpRequestContext requestContext = requestContextProvider.getRequestContext();
if (requestContext == null) {
// e.g. testing
return new HashMap<>();
}
Map<Object, FilterBehavior> cache = (Map<Object, FilterBehavior>) requestContext.getRequestAttribute(key);
if (cache == null) {
cache = new HashMap<>();
requestContext.setRequestAttribute(key, cache);
}
return cache;
}
use of io.crnk.core.engine.filter.FilterBehavior in project crnk-framework by crnk-project.
the class ResourceFilterDirectoryImpl method get.
@Override
public FilterBehavior get(ResourceInformation resourceInformation, HttpMethod method) {
Map<Object, FilterBehavior> map = getCache(method);
FilterBehavior behavior = map.get(resourceInformation);
if (behavior != null) {
return behavior;
}
behavior = FilterBehavior.NONE;
for (ResourceFilter filter : filters) {
behavior = behavior.merge(filter.filterResource(resourceInformation, method));
if (behavior == FilterBehavior.FORBIDDEN) {
break;
}
}
map.put(resourceInformation, behavior);
return behavior;
}
use of io.crnk.core.engine.filter.FilterBehavior in project crnk-framework by crnk-project.
the class ResourceUpsert method canModifyField.
/**
* Allows to check whether the given field can be written.
*
* @param field from the information model or null if is a dynamic field (like JsonAny).
*/
protected boolean canModifyField(ResourceInformation resourceInformation, String fieldName, ResourceField field) {
if (field == null) {
return true;
}
HttpMethod method = getHttpMethod();
ResourceFieldAccess access = field.getAccess();
boolean modifiable = method == HttpMethod.POST ? access.isPostable() : access.isPatchable();
FilterBehavior filterBehavior = modifiable ? FilterBehavior.NONE : getDefaultFilterBehavior();
filterBehavior = filterBehavior.merge(resourceFilterDirectory.get(field, method));
if (filterBehavior == FilterBehavior.NONE) {
return true;
} else if (filterBehavior == FilterBehavior.FORBIDDEN) {
throw new ForbiddenException("field '" + fieldName + "' cannot be modified");
} else {
PreconditionUtil.assertEquals("unknown behavior", FilterBehavior.IGNORED, filterBehavior);
return false;
}
}
Aggregations