use of org.exist.security.Permission in project exist by eXist-db.
the class RemoteUserManagementService method listResourcePermissions.
@Override
public Permission[] listResourcePermissions() throws XMLDBException {
try {
final List<Object> params = new ArrayList<>();
params.add(collection.getPath());
final Map result = (Map) collection.execute("listDocumentPermissions", params);
final Permission[] perm = new Permission[result.size()];
final String[] resources = collection.listResources();
Object[] t;
for (int i = 0; i < resources.length; i++) {
t = (Object[]) result.get(resources[i]);
final String owner = (String) t[0];
final String group = (String) t[1];
final int mode = (Integer) t[2];
final Stream<ACEAider> aces = extractAces(t[3]);
perm[i] = getPermission(owner, group, mode, aces);
}
return perm;
} catch (final PermissionDeniedException pde) {
throw new XMLDBException(ErrorCodes.PERMISSION_DENIED, pde.getMessage(), pde);
}
}
use of org.exist.security.Permission in project exist by eXist-db.
the class NativeBrokerTest method copyCollection_noDescendants_toNonExistingDest_cannotWriteDest.
/**
* When copying a Collection (/db/test/source) where
* we have execute+read access and
* which has no descendant documents or collections in it,
*
* to the destination /db/test/dest (which does not already exist)
* and we DO NOT have execute+write access on /db/test
* we should NOT be allowed to copy the Collection.
*/
@Test(expected = PermissionDeniedException.class)
public void copyCollection_noDescendants_toNonExistingDest_cannotWriteDest() throws LockException, PermissionDeniedException {
final XmldbURI src = XmldbURI.create("/db/test/source");
final XmldbURI dest = XmldbURI.create("/db/test");
final XmldbURI newName = XmldbURI.create("dest");
final Collection srcCollection = EasyMock.createStrictMock(Collection.class);
final Permission srcPermissions = EasyMock.createStrictMock(Permission.class);
final Collection destCollection = EasyMock.createStrictMock(Collection.class);
final Permission destPermissions = EasyMock.createStrictMock(Permission.class);
// EasyMock.createMock(Collection.class);
final Collection newDestCollection = null;
final NativeBroker broker = EasyMock.createMockBuilder(NativeBroker.class).addMockedMethod("getCollection").addMockedMethod("getCurrentSubject").createStrictMock();
final Subject subject = EasyMock.createStrictMock(Subject.class);
// grant EXECUTE and READ permissions on the src
expect(srcCollection.getPermissionsNoLock()).andReturn(srcPermissions);
expect(broker.getCurrentSubject()).andReturn(subject);
expect(srcPermissions.validate(subject, Permission.EXECUTE | Permission.READ)).andReturn(true);
// grant EXECUTE and WRITE permission on the dest
expect(destCollection.getURI()).andReturn(dest);
final Capture<XmldbURI> newDestURICapture = newCapture();
expect(broker.getCollection(capture(newDestURICapture))).andReturn(newDestCollection);
expect(destCollection.getPermissionsNoLock()).andReturn(destPermissions);
expect(broker.getCurrentSubject()).andReturn(subject);
expect(destPermissions.validate(subject, Permission.EXECUTE | Permission.WRITE)).andReturn(false);
// expectations for exception that should be thrown
expect(srcCollection.getURI()).andReturn(src);
expect(destCollection.getURI()).andReturn(dest);
expect(broker.getCurrentSubject()).andReturn(subject);
expect(subject.getName()).andReturn("Fake user");
// test below
replay(subject, destCollection, destPermissions, srcCollection, srcPermissions, broker);
// run the test
broker.checkPermissionsForCopy(srcCollection, destCollection, newName);
// not actually called, but here for showing intention
verify(subject, destCollection, destPermissions, srcCollection, srcPermissions, broker);
}
use of org.exist.security.Permission in project exist by eXist-db.
the class NativeBrokerTest method copyCollection_noDescendants_toNonExistingDest_canWriteDest.
/**
* When copying a Collection (/db/test/source) where
* we have execute+read access and
* which has no descendant documents or collections in it
* to the destination /db/test/dest (which does not already exist)
* and we have execute+write access on /db/test
* we should be allowed to copy the Collection.
*/
@Test
public void copyCollection_noDescendants_toNonExistingDest_canWriteDest() throws LockException, PermissionDeniedException {
final XmldbURI src = XmldbURI.create("/db/test/source");
final XmldbURI dest = XmldbURI.create("/db/test");
final XmldbURI newName = XmldbURI.create("dest");
final Collection srcCollection = EasyMock.createStrictMock(Collection.class);
final Permission srcPermissions = EasyMock.createStrictMock(Permission.class);
final Collection destCollection = EasyMock.createStrictMock(Collection.class);
final Permission destPermissions = EasyMock.createStrictMock(Permission.class);
final Collection newDestCollection = null;
final NativeBroker broker = EasyMock.createMockBuilder(NativeBroker.class).addMockedMethod("getCollection").addMockedMethod("getCurrentSubject").createStrictMock();
final Subject subject = EasyMock.createStrictMock(Subject.class);
// grant EXECUTE and READ permissions on the src
expect(srcCollection.getPermissionsNoLock()).andReturn(srcPermissions);
expect(broker.getCurrentSubject()).andReturn(subject);
expect(srcPermissions.validate(subject, Permission.EXECUTE | Permission.READ)).andReturn(true);
// grant EXECUTE and WRITE permission on the dest
expect(destCollection.getURI()).andReturn(dest);
final Capture<XmldbURI> newDestURICapture = newCapture();
expect(broker.getCollection(capture(newDestURICapture))).andReturn(newDestCollection);
expect(destCollection.getPermissionsNoLock()).andReturn(destPermissions);
expect(broker.getCurrentSubject()).andReturn(subject);
expect(destPermissions.validate(subject, Permission.EXECUTE | Permission.WRITE)).andReturn(true);
// no sub-documents
expect(srcCollection.iteratorNoLock(broker)).andReturn(Collections.emptyIterator());
// no sub-collections
expect(srcCollection.collectionIteratorNoLock(broker)).andReturn(Collections.emptyIterator());
// test below
replay(destCollection, destPermissions, srcCollection, srcPermissions, subject, broker);
// run the test
broker.checkPermissionsForCopy(srcCollection, destCollection, newName);
verify(destCollection, destPermissions, srcCollection, srcPermissions, subject, broker);
assertEquals(dest.append(newName), newDestURICapture.getValue());
}
use of org.exist.security.Permission in project exist by eXist-db.
the class NativeBrokerTest method copyCollection_oneSubDoc_toNonExistingDest_canWriteDest.
/**
* When copying a Collection (/db/test/source) where
* we have execute+read access and
* which has one descendant document (on which we have read access)
* in it,
*
* to the destination /db/test/dest (which does not already exist)
* and we have execute+write access on /db/test
* we should be allowed to copy the Collection.
*/
@Test
public void copyCollection_oneSubDoc_toNonExistingDest_canWriteDest() throws LockException, PermissionDeniedException {
final XmldbURI src = XmldbURI.create("/db/test/source");
final XmldbURI dest = XmldbURI.create("/db/test");
final XmldbURI newName = XmldbURI.create("dest");
final Collection srcCollection = EasyMock.createStrictMock(Collection.class);
final Permission srcPermissions = EasyMock.createStrictMock(Permission.class);
final DocumentImpl srcSubDocument = EasyMock.createStrictMock(DocumentImpl.class);
final Permission srcSubDocumentPermissions = EasyMock.createStrictMock(Permission.class);
final Collection destCollection = EasyMock.createStrictMock(Collection.class);
final Permission destPermissions = EasyMock.createStrictMock(Permission.class);
final Collection newDestCollection = null;
final NativeBroker broker = EasyMock.createMockBuilder(NativeBroker.class).addMockedMethod("getCollection").addMockedMethod("getCurrentSubject").createStrictMock();
final Subject subject = EasyMock.createStrictMock(Subject.class);
// grant EXECUTE and READ permissions on the src
expect(srcCollection.getPermissionsNoLock()).andReturn(srcPermissions);
expect(broker.getCurrentSubject()).andReturn(subject);
expect(srcPermissions.validate(subject, Permission.EXECUTE | Permission.READ)).andReturn(true);
// grant EXECUTE and WRITE permission on the dest
expect(destCollection.getURI()).andReturn(dest);
final Capture<XmldbURI> newDestURICapture = newCapture();
expect(broker.getCollection(capture(newDestURICapture))).andReturn(newDestCollection);
expect(destCollection.getPermissionsNoLock()).andReturn(destPermissions);
expect(broker.getCurrentSubject()).andReturn(subject);
expect(destPermissions.validate(subject, Permission.EXECUTE | Permission.WRITE)).andReturn(true);
// one sub-document with READ permission
expect(srcCollection.iteratorNoLock(broker)).andReturn(new ArrayIterator<>(srcSubDocument));
expect(srcSubDocument.getPermissions()).andReturn(srcSubDocumentPermissions);
expect(broker.getCurrentSubject()).andReturn(subject);
expect(srcSubDocumentPermissions.validate(subject, Permission.READ)).andReturn(true);
// no sub-collections
expect(srcCollection.collectionIteratorNoLock(broker)).andReturn(Collections.emptyIterator());
// test below
replay(srcSubDocumentPermissions, srcSubDocument, destCollection, destPermissions, srcCollection, srcPermissions, subject, broker);
// run the test
broker.checkPermissionsForCopy(srcCollection, destCollection, newName);
verify(srcSubDocumentPermissions, srcSubDocument, destCollection, destPermissions, srcCollection, srcPermissions, subject, broker);
assertEquals(dest.append(newName), newDestURICapture.getValue());
}
use of org.exist.security.Permission in project exist by eXist-db.
the class RemoteDatabaseImplTest method testGetCollection.
@Test
public void testGetCollection() throws ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException, SyntaxException, PermissionDeniedException {
Class<?> cl = Class.forName(DB_DRIVER);
Database database = (Database) cl.newInstance();
DatabaseManager.registerDatabase(database);
Collection rootCollection = DatabaseManager.getCollection(getUri() + XmldbURI.ROOT_COLLECTION, "admin", "");
CollectionManagementService cms = (CollectionManagementService) rootCollection.getService("CollectionManagementService", "1.0");
Collection adminCollection = cms.createCollection(ADMIN_COLLECTION_NAME);
UserManagementService ums = (UserManagementService) rootCollection.getService("UserManagementService", "1.0");
if (ums != null) {
Permission p = ums.getPermissions(adminCollection);
p.setMode(Permission.USER_STRING + "=+read,+write," + Permission.GROUP_STRING + "=-read,-write," + Permission.OTHER_STRING + "=-read,-write");
ums.setPermissions(adminCollection, p);
Collection guestCollection = DatabaseManager.getCollection(getUri() + XmldbURI.ROOT_COLLECTION + "/" + ADMIN_COLLECTION_NAME, "guest", "guest");
Resource resource = guestCollection.createResource("testguest", "BinaryResource");
resource.setContent("123".getBytes());
try {
guestCollection.storeResource(resource);
fail();
} catch (XMLDBException e) {
}
cms.removeCollection(ADMIN_COLLECTION_NAME);
}
}
Aggregations