mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Use PermissionReaders to determine available permissions
These readers will determine which permissions are available for the incoming credentials. Their results then get combined in a UnionReader and authorized in a PermissionBasedAuthorizer
This commit is contained in:
40
src/authorization/UnionPermissionReader.ts
Normal file
40
src/authorization/UnionPermissionReader.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import type { CredentialGroup } from '../authentication/Credentials';
|
||||
import type { Permission, PermissionSet } from '../ldp/permissions/Permissions';
|
||||
import { UnionHandler } from '../util/handlers/UnionHandler';
|
||||
import type { PermissionReader } from './PermissionReader';
|
||||
|
||||
/**
|
||||
* Combines the results of multiple PermissionReaders.
|
||||
* Every permission in every credential type is handled according to the rule `false` \> `true` \> `undefined`.
|
||||
*/
|
||||
export class UnionPermissionReader extends UnionHandler<PermissionReader> {
|
||||
public constructor(readers: PermissionReader[]) {
|
||||
super(readers);
|
||||
}
|
||||
|
||||
protected async combine(results: PermissionSet[]): Promise<PermissionSet> {
|
||||
const result: PermissionSet = {};
|
||||
for (const permissionSet of results) {
|
||||
for (const [ key, value ] of Object.entries(permissionSet) as [ CredentialGroup, Permission | undefined ][]) {
|
||||
result[key] = this.applyPermissions(value, result[key]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given permissions to the result object according to the combination rules of the class.
|
||||
*/
|
||||
private applyPermissions(permissions?: Permission, result: Permission = {}): Permission {
|
||||
if (!permissions) {
|
||||
return result;
|
||||
}
|
||||
|
||||
for (const [ key, value ] of Object.entries(permissions) as [ keyof Permission, boolean | undefined ][]) {
|
||||
if (typeof value !== 'undefined' && result[key] !== false) {
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user