feat: Add acl support

This commit is contained in:
Joachim Van Herwegen
2020-08-07 11:54:27 +02:00
parent 011822e859
commit 0545ca121e
14 changed files with 524 additions and 13 deletions

View File

@@ -0,0 +1,16 @@
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
/**
* Handles the identification of containers in which a resource is contained.
*/
export interface ContainerManager {
/**
* Finds the corresponding container.
* Should throw an error if there is no such container (in the case of root).
*
* @param id - Identifier to find container of.
*
* @returns The identifier of the container this resource is in.
*/
getContainer: (id: ResourceIdentifier) => Promise<ResourceIdentifier>;
}

View File

@@ -0,0 +1,34 @@
import { ContainerManager } from './ContainerManager';
import { ensureTrailingSlash } from '../util/Util';
import { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
/**
* Determines containers based on URL decomposition.
*/
export class UrlContainerManager implements ContainerManager {
private readonly root: string;
public constructor(root: string) {
this.root = this.canonicalUrl(root);
}
public async getContainer(id: ResourceIdentifier): Promise<ResourceIdentifier> {
const path = this.canonicalUrl(id.path);
if (this.root === path) {
throw new Error('Root does not have a container.');
}
const parentPath = new URL('..', path).toString();
// This probably means there is an issue with the root
if (parentPath === path) {
throw new Error('URL root reached.');
}
return { path: parentPath };
}
private canonicalUrl(path: string): string {
return ensureTrailingSlash(new URL(path).toString());
}
}