Search in sources :

Example 6 with VfsException

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

the class CascadingVfsPermissionCheckerTest method checkPermissionWhenNoSpecificSchemeCheckerAndNotAllowed.

@Test
public void checkPermissionWhenNoSpecificSchemeCheckerAndNotAllowed() throws Exception {
    VfsResourceReference reference = new VfsResourceReference(URI.create("customscheme:whatever"), "whatever");
    AuthorizationManager authorizationManager = this.mocker.registerMockComponent(AuthorizationManager.class);
    when(authorizationManager.hasAccess(Right.PROGRAM, this.contextUser, null)).thenReturn(false);
    try {
        this.mocker.getComponentUnderTest().checkPermission(reference);
        fail("Should have raised exception");
    } catch (VfsException expected) {
        assertEquals("Current logged-in user ([" + this.contextUser + "]) needs to have Programming Rights to use the [customscheme] VFS", expected.getMessage());
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) AuthorizationManager(org.xwiki.security.authorization.AuthorizationManager) ContextualAuthorizationManager(org.xwiki.security.authorization.ContextualAuthorizationManager) Test(org.junit.Test)

Example 7 with VfsException

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

the class DefaultVfsManagerTest method getURLerror.

@Test
public void getURLerror() throws Exception {
    VfsResourceReference reference = new VfsResourceReference(URI.create("attach:xwiki:space.page@attachment"), "path1/path2/test.txt");
    ResourceReferenceSerializer<VfsResourceReference, ExtendedURL> serializer = this.mocker.getInstance(new DefaultParameterizedType(null, ResourceReferenceSerializer.class, VfsResourceReference.class, ExtendedURL.class));
    when(serializer.serialize(reference)).thenThrow(new SerializeResourceReferenceException("error"));
    try {
        this.mocker.getComponentUnderTest().getURL(reference);
        fail("Should have thrown an exception");
    } catch (VfsException expected) {
        assertEquals("Failed to compute URL for [uri = [attach:xwiki:space.page@attachment], " + "path = [path1/path2/test.txt], parameters = []]", expected.getMessage());
    }
}
Also used : VfsException(org.xwiki.vfs.VfsException) SerializeResourceReferenceException(org.xwiki.resource.SerializeResourceReferenceException) ResourceReferenceSerializer(org.xwiki.resource.ResourceReferenceSerializer) VfsResourceReference(org.xwiki.vfs.VfsResourceReference) ExtendedURL(org.xwiki.url.ExtendedURL) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Test(org.junit.Test)

Example 8 with VfsException

use of org.xwiki.vfs.VfsException 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)8 Test (org.junit.Test)6 VfsResourceReference (org.xwiki.vfs.VfsResourceReference)6 VfsPermissionChecker (org.xwiki.vfs.VfsPermissionChecker)3 URI (java.net.URI)2 TPath (net.java.truevfs.access.TPath)2 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)2 ResourceReferenceSerializer (org.xwiki.resource.ResourceReferenceSerializer)2 Path (java.nio.file.Path)1 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)1 ComponentManager (org.xwiki.component.manager.ComponentManager)1 ConversionException (org.xwiki.properties.converter.ConversionException)1 ResourceReferenceHandlerChain (org.xwiki.resource.ResourceReferenceHandlerChain)1 ResourceReferenceHandlerException (org.xwiki.resource.ResourceReferenceHandlerException)1 SerializeResourceReferenceException (org.xwiki.resource.SerializeResourceReferenceException)1 AuthorizationManager (org.xwiki.security.authorization.AuthorizationManager)1 ContextualAuthorizationManager (org.xwiki.security.authorization.ContextualAuthorizationManager)1 ExtendedURL (org.xwiki.url.ExtendedURL)1 VfsManager (org.xwiki.vfs.VfsManager)1