use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class XMLDBMove method evalWithCollection.
/* (non-Javadoc)
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
public Sequence evalWithCollection(Collection collection, Sequence[] args, Sequence contextSequence) throws XPathException {
final XmldbURI destination = new AnyURIValue(args[1].itemAt(0).getStringValue()).toXmldbURI();
if (getSignature().getArgumentCount() == 3) {
final XmldbURI doc = new AnyURIValue(args[2].itemAt(0).getStringValue()).toXmldbURI();
try {
final Resource resource = collection.getResource(doc.toString());
if (resource == null) {
logger.error("Resource {} not found", doc);
throw new XPathException(this, "Resource " + doc + " not found");
}
final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
service.moveResource(doc, destination, null);
} catch (final XMLDBException e) {
logger.error(e.getMessage());
throw new XPathException(this, "XMLDB exception caught: " + e.getMessage(), e);
}
} else {
try {
final EXistCollectionManagementService service = (EXistCollectionManagementService) collection.getService("CollectionManagementService", "1.0");
service.move(XmldbURI.xmldbUriFor(collection.getName()), destination, null);
} catch (final XMLDBException e) {
logger.error(e.getMessage());
throw new XPathException(this, "Cannot move collection: " + e.getMessage(), e);
} catch (final URISyntaxException e) {
logger.error(e.getMessage());
throw new XPathException(this, "URI exception: " + e.getMessage(), e);
}
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class XMLDBSecurityTest method noSetGid_copyResource_resourceGroupIsUsersPrimaryGroup.
@Test
public void noSetGid_copyResource_resourceGroupIsUsersPrimaryGroup() throws XMLDBException {
Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest2", "test1", "test1");
EXistCollectionManagementService cms = (EXistCollectionManagementService) test.getService("CollectionManagementService", "1.0");
// create the /db/securityTest2/test.xml resource
Resource resource = test.createResource("test.xml", XMLResource.RESOURCE_TYPE);
resource.setContent("<test/>");
test.storeResource(resource);
// create /db/securityTest2/parentCollection with owner "test1:users" and mode "rwxrwxrwx"
Collection parentCollection = cms.createCollection("parentCollection");
UserManagementService ums = (UserManagementService) parentCollection.getService("UserManagementService", "1.0");
ums.chmod("rwxrwxrwx");
// now copy /db/securityTest2/test.xml to /db/securityTest2/parentCollection/test.xml
// as user3, it should have it's group set to the primary group of user3 i.e. 'guest'
// as the collection is NOT setGid and it should not have the setGid bit
test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest2", "test3", "test3");
cms = (EXistCollectionManagementService) test.getService("CollectionManagementService", "1.0");
cms.copyResource("test.xml", "/db/securityTest2/parentCollection", "test.xml");
ums = (UserManagementService) parentCollection.getService("UserManagementService", "1.0");
parentCollection = test.getChildCollection("parentCollection");
resource = parentCollection.getResource("test.xml");
final Permission permissions = ums.getPermissions(resource);
assertEquals("guest", permissions.getGroup().getName());
assertFalse(permissions.isSetGid());
}
use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class XMLDBSecurityTest method copyCollection_doesPreservePermissionsOfSubCollections.
@Test
public void copyCollection_doesPreservePermissionsOfSubCollections() throws XMLDBException {
final Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest1", "test1", "test1");
EXistCollectionManagementService cms = (EXistCollectionManagementService) test.getService("CollectionManagementService", "1.0");
cms.copy(XmldbURI.create("/db/securityTest1"), XmldbURI.create("/db/securityTest3"), XmldbURI.create("copy-of-securityTest1"));
final Collection testCopy = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest3/copy-of-securityTest1", "test1", "test1");
final Collection sub1 = testCopy.getChildCollection("sub1");
final UserManagementService ums = (UserManagementService) sub1.getService("UserManagementService", "1.0");
final Permission permissions = ums.getPermissions(sub1);
assertEquals("test1", permissions.getOwner().getName());
assertEquals("users", permissions.getGroup().getName());
assertEquals(0777, permissions.getMode());
}
use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class XMLDBSecurityTest method copyCollectionWithResources_destResourceExists_destResourceIsNotWritable.
/**
* As the 'test1' user, creates the collection and resource:
*
* test1:users /db/securityTest3/source
* test1:users /db/securityTest3/source/source1.xml
* test1:users /db/securityTest3/source/source2.xml
*
* We then also create the Collection
* test1:users /db/securityTest3/copy-of-source (0777)
* so that the destination (for the copy we are about
* to do) already exists and is writable.
* We then create the resource
* test1:users /db/securityTest/copy-of-source/source1.xml
* and set it so that it is not accessible by anyone
* apart from 'test1' user...
*
* As the 'test3' user, copy the collection:
*
* /db/securityTest3/source
* -> /db/securityTest3/copy-of-source
*
* The test should prove that during a copy, existing
* documents in the dest are replaced as long as the
* dest collection has write permission and that the
* permissions on the dest resource must also be writable
*/
@Test(expected = XMLDBException.class)
public void copyCollectionWithResources_destResourceExists_destResourceIsNotWritable() throws XMLDBException {
Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest3", "test1", "test1");
EXistCollectionManagementService cms = (EXistCollectionManagementService) test.getService("CollectionManagementService", "1.0");
// create collection owned by "test1", and group "users" in /db/securityTest3
Collection source = cms.createCollection("source");
// create resource owned by "test1", and group "users" in /db/securityTest3/source
Resource resSource = source.createResource("source1.xml", XMLResource.RESOURCE_TYPE);
resSource.setContent("<test1/>");
source.storeResource(resSource);
resSource = source.createResource("source2.xml", XMLResource.RESOURCE_TYPE);
resSource.setContent("<test2/>");
source.storeResource(resSource);
// pre-create the destination and set writable by all
final Collection dest = cms.createCollection("copy-of-source");
UserManagementService ums = (UserManagementService) dest.getService("UserManagementService", "1.0");
ums.chmod(0777);
// pre-create a destination resource and set no access to group and others
Resource resDestSource1 = dest.createResource("source1.xml", XMLResource.RESOURCE_TYPE);
resDestSource1.setContent("<old/>");
dest.storeResource(resDestSource1);
ums.chmod(resDestSource1, 0700);
// as the 'test3' user copy the collection
test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest3", "test3", "test3");
cms = (EXistCollectionManagementService) test.getService("CollectionManagementService", "1.0");
cms.copy("/db/securityTest3/source", "/db/securityTest3", "copy-of-source");
final Collection copyOfSource = test.getChildCollection("copy-of-source");
assertNotNull(copyOfSource);
assertEquals(2, copyOfSource.listResources().length);
final Resource resCopyOfSource1 = copyOfSource.getResource("source1.xml");
assertEquals("<test1/>", resCopyOfSource1.getContent().toString());
final Resource resCopyOfSource2 = copyOfSource.getResource("source2.xml");
assertEquals("<test2/>", resCopyOfSource2.getContent().toString());
// TODO check perms are/areNot preserved? on the replaced resource
}
use of org.exist.xmldb.EXistCollectionManagementService in project exist by eXist-db.
the class XMLDBSecurityTest method setGid_copyResource_resourceGroupInheritedFromParent.
@Test
public void setGid_copyResource_resourceGroupInheritedFromParent() throws XMLDBException {
Collection test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest2", "test1", "test1");
EXistCollectionManagementService cms = (EXistCollectionManagementService) test.getService("CollectionManagementService", "1.0");
UserManagementService ums = (UserManagementService) test.getService("UserManagementService", "1.0");
// create the /db/securityTest2/test.xml resource
Resource resource = test.createResource("test.xml", XMLResource.RESOURCE_TYPE);
resource.setContent("<test/>");
test.storeResource(resource);
ums.chgrp(resource, "extusers");
// create /db/securityTest2/parentCollection with owner "test1:users" and mode "rwxrwsrwx"
Collection parentCollection = cms.createCollection("parentCollection");
ums = (UserManagementService) parentCollection.getService("UserManagementService", "1.0");
ums.chmod("rwxrwsrwx");
// now copy /db/securityTest2/test.xml to /db/securityTest2/parentCollection/test.xml
// as "user3:guest", it should inherit the group ownership 'users' from the parent collection which is setGid
// and it should NOT have its setGid bit set as it is a resource
test = DatabaseManager.getCollection(getBaseUri() + "/db/securityTest2", "test3", "test3");
cms = (EXistCollectionManagementService) test.getService("CollectionManagementService", "1.0");
cms.copyResource("test.xml", "/db/securityTest2/parentCollection", "test.xml");
ums = (UserManagementService) parentCollection.getService("UserManagementService", "1.0");
parentCollection = test.getChildCollection("parentCollection");
resource = parentCollection.getResource("test.xml");
final Permission permissions = ums.getPermissions(resource);
assertEquals("users", permissions.getGroup().getName());
assertFalse(permissions.isSetGid());
}
Aggregations