Search in sources :

Example 1 with VfsPermissionChecker

use of org.xwiki.vfs.VfsPermissionChecker in project xwiki-platform by xwiki.

the class PathConverterTest method convertWhenNoPermission.

@Test
public void convertWhenNoPermission() throws Exception {
    ResourceReferenceSerializer<VfsResourceReference, URI> serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, URI.class), "truevfs");
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:Sandbox.WebHome@my.zip"), "a/b/c");
    when(serializer.serialize(reference)).thenReturn(URI.create("attach://xwiki:Sandbox.WebHome/my.zip/a/b/c"));
    VfsPermissionChecker permissionChecker = this.mocker.getInstance(VfsPermissionChecker.class, "cascading");
    doThrow(new VfsException("unauthorized")).when(permissionChecker).checkPermission(reference);
    try {
        this.mocker.getComponentUnderTest().convert(new DefaultParameterizedType(null, Path.class), "attach:Sandbox.WebHome@my.zip/a/b/c");
        fail("Should have thrown an exception here");
    } catch (ConversionException expected) {
        assertEquals("Failed to convert [attach:Sandbox.WebHome@my.zip/a/b/c] to a Path object", expected.getMessage());
        assertEquals("VfsException: unauthorized", ExceptionUtils.getRootCauseMessage(expected));
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) TPath(net.java.truevfs.access.TPath) Path(java.nio.file.Path) ConversionException(org.xwiki.properties.converter.ConversionException) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsPermissionChecker(org.xwiki.vfs.VfsPermissionChecker) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) URI(java.net.URI) Test(org.junit.Test)

Example 2 with VfsPermissionChecker

use of org.xwiki.vfs.VfsPermissionChecker in project xwiki-platform by xwiki.

the class VfsResourceReferenceHandlerTest method handleWhenNoGenericPermissionForScheme.

@Test
public void handleWhenNoGenericPermissionForScheme() throws Exception {
    setUp("customscheme", "wiki3", "space3", "page3", "test.zip", Arrays.asList("test.txt"));
    // Don't allow permission for "customscheme"
    VfsPermissionChecker checker = this.mocker.getInstance(VfsPermissionChecker.class, "cascading");
    doThrow(new VfsException("no permission")).when(checker).checkPermission(this.reference);
    try {
        this.mocker.getComponentUnderTest().handle(this.reference, mock(ResourceReferenceHandlerChain.class));
        fail("Should have thrown exception here");
    } catch (ResourceReferenceHandlerException expected) {
        assertEquals("VfsException: no permission", ExceptionUtils.getRootCauseMessage(expected));
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) ResourceReferenceHandlerException(org.xwiki.resource.ResourceReferenceHandlerException) VfsPermissionChecker(org.xwiki.vfs.VfsPermissionChecker) ResourceReferenceHandlerChain(org.xwiki.resource.ResourceReferenceHandlerChain) Test(org.junit.Test)

Example 3 with VfsPermissionChecker

use of org.xwiki.vfs.VfsPermissionChecker in project xwiki-platform by xwiki.

the class CascadingVfsPermissionChecker method checkPermission.

@Override
public void checkPermission(VfsResourceReference resourceReference) throws VfsException {
    // Prevent using a VFS scheme that correspond to the hint of this component
    String scheme = resourceReference.getURI().getScheme();
    if (HINT.equals(scheme)) {
        throw new VfsException(String.format("[%s] is a reserved VFS URI scheme and cannot be used.", HINT));
    }
    // Look for a scheme-specific permission checker
    VfsPermissionChecker resolvedChecker;
    ComponentManager componentManager = this.componentManagerProvider.get();
    try {
        resolvedChecker = componentManager.getInstance(VfsPermissionChecker.class, scheme);
    } catch (ComponentLookupException e) {
        // Use the Generic permission checker
        try {
            resolvedChecker = componentManager.getInstance(VfsPermissionChecker.class);
        } catch (ComponentLookupException ee) {
            throw new VfsException(String.format("No VFS Permission Checked has been found in the system. " + "Refusing access to VFS URI scheme [%s]", scheme), ee);
        }
    }
    resolvedChecker.checkPermission(resourceReference);
}
Also used : VfsException(org.xwiki.vfs.VfsException) VfsPermissionChecker(org.xwiki.vfs.VfsPermissionChecker) ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Aggregations

VfsException (org.xwiki.vfs.VfsException)3 VfsPermissionChecker (org.xwiki.vfs.VfsPermissionChecker)3 Test (org.junit.Test)2 URI (java.net.URI)1 Path (java.nio.file.Path)1 TPath (net.java.truevfs.access.TPath)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)1 ConversionException (org.xwiki.properties.converter.ConversionException)1 ResourceReferenceHandlerChain (org.xwiki.resource.ResourceReferenceHandlerChain)1 ResourceReferenceHandlerException (org.xwiki.resource.ResourceReferenceHandlerException)1 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)1 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)1