Joachim Van Herwegen 7f8b923399 feat: Replace acl specific permissions with generic permissions
This required AuxiliaryStrategy to have a new function
indicating if the auxiliary resource just used its associated resource authorization
or its own.
2021-09-28 13:06:38 +02:00

62 lines
1.8 KiB
TypeScript

import type { ResourceStore } from '../../src/';
import { BasicRepresentation } from '../../src/';
import type { AclPermission } from '../../src/ldp/permissions/AclPermission';
export class AclHelper {
public readonly store: ResourceStore;
public constructor(store: ResourceStore) {
this.store = store;
}
public async setSimpleAcl(
resource: string,
options: {
permissions: AclPermission;
agentClass?: 'agent' | 'authenticated';
agent?: string;
accessTo?: boolean;
default?: boolean;
},
): Promise<void> {
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: <http://www.w3.org/ns/auth/acl#>.\n',
'@prefix foaf: <http://xmlns.com/foaf/0.1/>.\n',
'<http://test.com/#auth> a acl:Authorization',
];
for (const perm of [ 'Read', 'Append', 'Write', 'Control' ]) {
if (options.permissions[perm.toLowerCase() as keyof AclPermission]) {
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'));
}
}