This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPermissionService.ts
68 lines (59 loc) · 2.39 KB
/
PermissionService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2022. Heusala Group Oy <[email protected]>. All rights reserved.
import { PermissionManager } from "./types/PermissionManager";
import { LogService } from "./LogService";
import { PermissionList, PermissionObject, PermissionUtils } from "./PermissionUtils";
import { LogLevel } from "./types/LogLevel";
const LOG = LogService.createLogger('PermissionService');
/**
* Service which handles permissions
*/
export class PermissionService<T extends string = string> {
public static setLogLevel (value : LogLevel) {
LOG.setLogLevel(value);
}
private readonly _manager : PermissionManager<T>;
/**
*
* @param manager
*/
public constructor (
manager: PermissionManager<T>
) {
this._manager = manager;
}
/**
* Fetch entity permissions
*
* @param entityId The entity who is performing the action
* @param targetId The entity which is the target of the action
* @returns Promise<PermissionList> Promise of a list of permissions this entity has against `targetId`
*/
public async getEntityPermissionList (
entityId : string,
targetId ?: string
) : Promise<PermissionList<T>> {
return await this._manager.getEntityPermissionList(entityId, targetId);
}
/**
* Check permissions in the list
*
* @param checkPermissions List of permissions which are checked
* @param entityId Entity who is performing the action
* @param targetId The entity which is the target of the action
*/
public async checkEntityPermission (
checkPermissions: PermissionList<T>,
entityId: string,
targetId ?: string
) : Promise<PermissionObject> {
LOG.debug(`checkEntityPermission: Checking entity "${entityId}" against permissions "${checkPermissions.join('|')}"`);
const entityPermissions = await this.getEntityPermissionList(entityId, targetId);
LOG.debug(`checkEntityPermission: Checking entity "${entityId}" [${entityPermissions.join('|')}] against permissions [${checkPermissions.join('|')}]`);
const result = PermissionUtils.checkPermissionList(
checkPermissions,
entityPermissions
);
LOG.info(`checkEntityPermission: Checking entity "${entityId}" [${entityPermissions.join('|')}] against result [${checkPermissions.join('|')}]: `, result);
return result;
}
}