Search in sources :

Example 1 with ForbiddenException

use of io.crnk.core.exception.ForbiddenException 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;
    }
}
Also used : ForbiddenException(io.crnk.core.exception.ForbiddenException) FilterBehavior(io.crnk.core.engine.filter.FilterBehavior) HttpMethod(io.crnk.core.engine.http.HttpMethod)

Example 2 with ForbiddenException

use of io.crnk.core.exception.ForbiddenException in project crnk-framework by crnk-project.

the class ResourcePatchTest method onPatchingReadOnlyFieldReturnBadRequestWithFailBehavior.

@Test
public void onPatchingReadOnlyFieldReturnBadRequestWithFailBehavior() throws Exception {
    // GIVEN
    Document requestDocument = new Document();
    Resource data = createTask();
    requestDocument.setData(Nullable.of((Object) data));
    JsonPath postPath = pathBuilder.build("/tasks");
    ResourcePost post = new ResourcePost(resourceRegistry, PROPERTIES_PROVIDER, typeParser, objectMapper, documentMapper, modificationFilters);
    post.handle(postPath, emptyTaskQuery, null, requestDocument);
    PropertiesProvider propertiesProvider = new PropertiesProvider() {

        @Override
        public String getProperty(String key) {
            if (CrnkProperties.RESOURCE_FIELD_IMMUTABLE_WRITE_BEHAVIOR.equals(key)) {
                return ResourceFieldImmutableWriteBehavior.FAIL.toString();
            }
            return null;
        }
    };
    ResourcePatch sut = new ResourcePatch(resourceRegistry, propertiesProvider, typeParser, objectMapper, documentMapper, modificationFilters);
    data.getAttributes().put("readOnlyValue", objectMapper.readTree("\"newValue\""));
    // WHEN
    try {
        JsonPath patchPath = pathBuilder.build("/tasks/" + data.getId());
        sut.handle(patchPath, emptyTaskQuery, null, requestDocument);
        Assert.fail("should not be allowed to update read-only field");
    } catch (ForbiddenException e) {
        Assert.assertEquals("field 'readOnlyValue' cannot be modified", e.getMessage());
    }
}
Also used : PropertiesProvider(io.crnk.core.engine.properties.PropertiesProvider) ForbiddenException(io.crnk.core.exception.ForbiddenException) Resource(io.crnk.core.engine.document.Resource) Document(io.crnk.core.engine.document.Document) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) ResourcePatch(io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch) ResourcePost(io.crnk.core.engine.internal.dispatcher.controller.ResourcePost) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Aggregations

ForbiddenException (io.crnk.core.exception.ForbiddenException)2 Document (io.crnk.core.engine.document.Document)1 Resource (io.crnk.core.engine.document.Resource)1 FilterBehavior (io.crnk.core.engine.filter.FilterBehavior)1 HttpMethod (io.crnk.core.engine.http.HttpMethod)1 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)1 ResourcePatch (io.crnk.core.engine.internal.dispatcher.controller.ResourcePatch)1 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)1 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)1 PropertiesProvider (io.crnk.core.engine.properties.PropertiesProvider)1 Test (org.junit.Test)1