mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00

This required AuxiliaryStrategy to have a new function indicating if the auxiliary resource just used its associated resource authorization or its own.
62 lines
1.8 KiB
TypeScript
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'));
|
|
}
|
|
}
|