Search in sources :

Example 6 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class FnCollectionSecurityTest method cannotAccessCollectionInCollectionHierarchyWithDeniedReadAndExecuteAce.

@Test(expected = PermissionDeniedException.class)
public void cannotAccessCollectionInCollectionHierarchyWithDeniedReadAndExecuteAce() throws EXistException, AuthenticationException, PermissionDeniedException, XPathException {
    // as docTestUser1 user
    final String query = "fn:collection('" + TEST_SUB_COLLECTION_2_2 + "')";
    final BrokerPool pool = server.getBrokerPool();
    final SecurityManager securityManager = pool.getSecurityManager();
    final Subject testUser1 = securityManager.authenticate(TEST_USER_1, TEST_USER_1);
    try (final DBBroker broker = pool.get(Optional.of(testUser1));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final XQuery xqueryService = pool.getXQueryService();
        final Sequence result = xqueryService.execute(broker, query, null);
        fail("Expected PermissionDeniedException via XPathException");
        transaction.commit();
    } catch (final XPathException e) {
        if (e.getCause() != null && e.getCause() instanceof PermissionDeniedException) {
            throw (PermissionDeniedException) e.getCause();
        } else {
            throw e;
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 7 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class FnDocSecurityTest method cannotAccessRestrictedDocument.

@Test(expected = PermissionDeniedException.class)
public void cannotAccessRestrictedDocument() throws EXistException, AuthenticationException, PermissionDeniedException, XPathException, IOException, SAXException {
    // as docTestUser1 user
    final String query = "fn:doc('" + TEST_DOC_URI_SYSTEM_ONLY + "')";
    final BrokerPool pool = server.getBrokerPool();
    final SecurityManager securityManager = pool.getSecurityManager();
    final Subject testUser1 = securityManager.authenticate(TEST_USER_1, TEST_USER_1);
    try (final DBBroker broker = pool.get(Optional.of(testUser1));
        final Txn transaction = pool.getTransactionManager().beginTransaction()) {
        final XQuery xqueryService = pool.getXQueryService();
        final Sequence result = xqueryService.execute(broker, query, null);
        fail("Expected PermissionDeniedException via XPathException");
        transaction.commit();
    } catch (final XPathException e) {
        if (e.getCause() != null && e.getCause() instanceof PermissionDeniedException) {
            throw (PermissionDeniedException) e.getCause();
        } else {
            throw e;
        }
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XPathException(org.exist.xquery.XPathException) XQuery(org.exist.xquery.XQuery) Txn(org.exist.storage.txn.Txn) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) Test(org.junit.Test)

Example 8 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class PermissionsFunctionChownTest method changeOwner.

private void changeOwner(final Subject execAsUser, final boolean restricted, final XmldbURI uri, final String newOwnerGroup, final String expectedOwnerGroup) throws EXistException, PermissionDeniedException, XPathException {
    final BrokerPool pool = existWebServer.getBrokerPool();
    final boolean prevRestricted = setPosixChownRestricted(restricted);
    final String query = "import module namespace sm = 'http://exist-db.org/xquery/securitymanager';\n" + "sm:chown(xs:anyURI('" + uri.getRawCollectionPath() + "'), '" + newOwnerGroup + "'),\n" + "sm:get-permissions(xs:anyURI('" + uri.getRawCollectionPath() + "'))/sm:permission/(string(@owner), string(@group))";
    try (final DBBroker broker = pool.get(Optional.of(execAsUser))) {
        final XQuery xquery = existWebServer.getBrokerPool().getXQueryService();
        final Sequence result = xquery.execute(broker, query, null);
        assertEquals(2, result.getItemCount());
        final String[] expectedOwnerGroupParts = expectedOwnerGroup.split(":");
        assertEquals(expectedOwnerGroupParts[0], result.itemAt(0).getStringValue());
        if (expectedOwnerGroupParts.length == 2) {
            assertEquals(expectedOwnerGroupParts[1], result.itemAt(1).getStringValue());
        }
    } finally {
        setPosixChownRestricted(prevRestricted);
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) XQuery(org.exist.xquery.XQuery) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool)

Example 9 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class TransformTest method transform1.

private static void transform1(final XmldbURI collectionUri) throws EXistException, PermissionDeniedException, XPathException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xquery = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final Sequence sequence = xquery.execute(broker, getCountDescendantsXquery(collectionUri), null);
        assertNotNull(sequence);
        assertTrue(sequence.hasOne());
        final Source expected = Input.fromString("<count-descendants>1</count-descendants>").build();
        final Source actual = Input.fromDocument(sequence.itemAt(0).toJavaObject(Node.class).getOwnerDocument()).build();
        final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Diff(org.xmlunit.diff.Diff) XQuery(org.exist.xquery.XQuery) Node(org.w3c.dom.Node) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) StringInputSource(org.exist.util.StringInputSource) Source(javax.xml.transform.Source)

Example 10 with XQuery

use of org.exist.xquery.XQuery in project exist by eXist-db.

the class TransformTest method transform_twoNodesCountDescendants.

private static void transform_twoNodesCountDescendants() throws EXistException, PermissionDeniedException, XPathException {
    final BrokerPool pool = existEmbeddedServer.getBrokerPool();
    final XQuery xquery = pool.getXQueryService();
    try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()))) {
        final Sequence sequence = xquery.execute(broker, COUNT_DESCENDANTS_TWO_NODES_QUERY, null);
        assertNotNull(sequence);
        assertTrue(sequence.hasOne());
        final Source expected = Input.fromString("<counts><count1>2</count1><count2>1</count2></counts>").build();
        final Source actual = Input.fromDocument(sequence.itemAt(0).toJavaObject(Node.class).getOwnerDocument()).build();
        final Diff diff = DiffBuilder.compare(expected).withTest(actual).checkForSimilar().build();
        assertFalse(diff.toString(), diff.hasDifferences());
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) Diff(org.xmlunit.diff.Diff) XQuery(org.exist.xquery.XQuery) Node(org.w3c.dom.Node) Sequence(org.exist.xquery.value.Sequence) BrokerPool(org.exist.storage.BrokerPool) StringInputSource(org.exist.util.StringInputSource) Source(javax.xml.transform.Source)

Aggregations

XQuery (org.exist.xquery.XQuery)135 Sequence (org.exist.xquery.value.Sequence)108 DBBroker (org.exist.storage.DBBroker)107 BrokerPool (org.exist.storage.BrokerPool)105 CompiledXQuery (org.exist.xquery.CompiledXQuery)59 Test (org.junit.Test)36 XQueryContext (org.exist.xquery.XQueryContext)33 Txn (org.exist.storage.txn.Txn)32 XPathException (org.exist.xquery.XPathException)21 TransactionManager (org.exist.storage.txn.TransactionManager)17 Item (org.exist.xquery.value.Item)16 InputSource (org.xml.sax.InputSource)16 XQueryPool (org.exist.storage.XQueryPool)15 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)12 DocumentSet (org.exist.dom.persistent.DocumentSet)12 StringReader (java.io.StringReader)11 Properties (java.util.Properties)11 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)11 Modification (org.exist.xupdate.Modification)11 XUpdateProcessor (org.exist.xupdate.XUpdateProcessor)11