import type { ResourceStore, PermissionSet } from '../../src/'; import { BasicRepresentation } from '../../src/'; export class AclHelper { public readonly store: ResourceStore; public constructor(store: ResourceStore) { this.store = store; } public async setSimpleAcl( resource: string, options: { permissions: Partial; agentClass?: 'agent' | 'authenticated'; agent?: string; accessTo?: boolean; default?: boolean; }, ): Promise { if (!options.agentClass && !options.agent) { throw new Error('At least one of agentClass or agent have to be provided.'); } if (!options.accessTo && !options.default) { throw new Error('At least one of accessTo or default have to be true.'); } const acl: string[] = [ '@prefix acl: .\n', '@prefix foaf: .\n', ' a acl:Authorization', ]; for (const perm of [ 'Read', 'Append', 'Write', 'Control' ]) { if (options.permissions[perm.toLowerCase() as keyof PermissionSet]) { acl.push(`;\n acl:mode acl:${perm}`); } } if (options.accessTo) { acl.push(`;\n acl:accessTo <${resource}>`); } if (options.default) { acl.push(`;\n acl:default <${resource}>`); } if (options.agentClass) { acl.push( `;\n acl:agentClass ${ options.agentClass === 'agent' ? 'foaf:Agent' : 'foaf:AuthenticatedAgent' }`, ); } if (options.agent) { acl.push(`;\n acl:agent ${options.agent}`); } acl.push('.'); await this.store.setRepresentation({ path: `${resource}.acl` }, new BasicRepresentation(acl, 'text/turtle')); } }