Skip to content

Instantly share code, notes, and snippets.

@michielbdejong
Last active November 29, 2019 15:06
Show Gist options
  • Save michielbdejong/858f83d6cf6ffadd71251b79db82a98f to your computer and use it in GitHub Desktop.
Save michielbdejong/858f83d6cf6ffadd71251b79db82a98f to your computer and use it in GitHub Desktop.
acl-edit.js
import { fetchDocument } from 'tripledoc';
async function createAclDoc(webId: string, resourceUri: string, otherAuthMode: string, otherAuthClass: string, recursive = false) {
const resourceDoc = await fetchDocument(resourceUri);
console.log('got doc', resourceUri);
const aclRef = resourceDoc.getAclRef();
console.log('got acl ref', webId, resourceUri, aclRef);
if (!aclRef) {
throw new Error('Could not determine ACL ref for friends list');
}
let aclDoc
try {
await getDocument(aclRef);
console.log('got acl doc');
} catch (e) {
console.log('creating acl doc');
aclDoc = createDocument(aclRef);
}
if (aclDoc) {
console.log('have aclDoc');
const ownerAuthSub = aclDoc.addSubject();
ownerAuthSub.addNodeRef(rdf.type, acl.Authorization);
ownerAuthSub.addNodeRef(acl.accessTo, resourceDoc.asNodeRef());
ownerAuthSub.addNodeRef(acl.default, resourceDoc.asNodeRef());
ownerAuthSub.addNodeRef(acl.mode, acl.Read);
ownerAuthSub.addNodeRef(acl.mode, acl.Write);
ownerAuthSub.addNodeRef(acl.mode, acl.Control);
ownerAuthSub.addNodeRef(acl.agent, webId);
APP_ORIGINS.forEach((origin: string) => {
ownerAuthSub.addNodeRef(acl.origin, origin);
});
const otherAuthSub = aclDoc.addSubject();
otherAuthSub.addNodeRef(rdf.type, acl.Authorization);
otherAuthSub.addNodeRef(acl.accessTo, resourceDoc.asNodeRef());
if (recursive) {
otherAuthSub.addNodeRef(acl.default, resourceDoc.asNodeRef());
}
otherAuthSub.addNodeRef(acl.mode, otherAuthMode);
otherAuthSub.addNodeRef(otherAuthPredicate, otherAuthObject);
if (otherAuthObject !== foaf.Agent) { // for public, origin isn't checked
APP_ORIGINS.forEach((origin: string) => {
otherAuthSub.addNodeRef(acl.origin, origin);
});
}
aclDoc.save();
} else {
console.log('have no aclDoc');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment