mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Simplify MethodPermissionsExtractor
This commit is contained in:
parent
ba8b3575b0
commit
389fb33334
@ -3,30 +3,24 @@ import { Operation } from '../operations/Operation';
|
|||||||
import { PermissionSet } from './PermissionSet';
|
import { PermissionSet } from './PermissionSet';
|
||||||
import { PermissionsExtractor } from './PermissionsExtractor';
|
import { PermissionsExtractor } from './PermissionsExtractor';
|
||||||
|
|
||||||
|
const READ_METHODS = new Set([ 'GET', 'HEAD' ]);
|
||||||
|
const WRITE_METHODS = new Set([ 'POST', 'PUT', 'DELETE' ]);
|
||||||
|
const SUPPORTED_METHODS = new Set([ ...READ_METHODS, ...WRITE_METHODS ]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates permissions for the base set of methods that always require the same permissions.
|
* Generates permissions for the base set of methods that always require the same permissions.
|
||||||
* Specifically: GET, HEAD, POST, PUT and DELETE.
|
* Specifically: GET, HEAD, POST, PUT and DELETE.
|
||||||
*/
|
*/
|
||||||
export class MethodPermissionsExtractor extends PermissionsExtractor {
|
export class MethodPermissionsExtractor extends PermissionsExtractor {
|
||||||
public async canHandle(input: Operation): Promise<void> {
|
public async canHandle({ method }: Operation): Promise<void> {
|
||||||
if (!/^(?:HEAD|GET|POST|PUT|DELETE)$/u.test(input.method)) {
|
if (!SUPPORTED_METHODS.has(method)) {
|
||||||
throw new UnsupportedHttpError(`Unsupported method: ${input.method}`);
|
throw new UnsupportedHttpError(`Cannot determine permissions of ${method}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async handle(input: Operation): Promise<PermissionSet> {
|
public async handle({ method }: Operation): Promise<PermissionSet> {
|
||||||
const requiredPermissions = {
|
const read = READ_METHODS.has(method);
|
||||||
read: /^(?:HEAD|GET)$/u.test(input.method),
|
const write = WRITE_METHODS.has(method);
|
||||||
append: false,
|
return { read, write, append: write };
|
||||||
write: /^(?:POST|PUT|DELETE)$/u.test(input.method),
|
|
||||||
};
|
|
||||||
|
|
||||||
const read = /^(?:HEAD|GET)$/u.test(input.method);
|
|
||||||
const write = /^(?:POST|PUT|DELETE)$/u.test(input.method);
|
|
||||||
|
|
||||||
// Since `append` is a specific type of write, it is true if `write` is true.
|
|
||||||
const append = requiredPermissions.write;
|
|
||||||
|
|
||||||
return { read, append, write };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user