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 pathPermissionUtils.ts
164 lines (148 loc) · 5.39 KB
/
PermissionUtils.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { filter } from "./functions/filter";
import { map } from "./functions/map";
import { reduce } from "./functions/reduce";
import { values } from "./functions/values";
import { LogService } from "./LogService";
import { TestCallback, TestCallbackNonStandard } from "./types/TestCallback";
import { ExplainCallback } from "./types/ExplainCallback";
import { isString } from "./types/String";
import { keys } from "./functions/keys";
import { everyValue } from "./functions/everyValue";
import { explainArrayOf, isArrayOf } from "./types/Array";
const LOG = LogService.createLogger('PermissionUtils');
export type PermissionString<T extends string> = T;
export type PermissionList<T extends string> = readonly PermissionString<T>[];
export function isPermissionList<T extends string> (
value : any,
isItem : TestCallback | undefined = undefined,
minLength : number | undefined = undefined,
maxLength : number | undefined = undefined
) : boolean {
return isArrayOf<T>(
value,
isItem,
minLength,
maxLength
);
}
export function explainPermissionList<T extends string> (
itemTypeName : string,
itemExplain : ExplainCallback,
value : any,
isItem : TestCallback | undefined = undefined,
minLength : number | undefined = undefined,
maxLength : number | undefined = undefined
) : string {
return explainArrayOf<T>(
itemTypeName,
itemExplain,
value,
isItem,
minLength,
maxLength
);
}
export type PermissionObject = {readonly [key: string]: boolean};
export interface PermissionValueObject<T extends string> {
readonly permission : T;
readonly value : boolean;
}
export class PermissionUtils {
/**
* Validate a permission against a list of permissions
*
* @param permission The permission to check for
* @param acceptedPermissionList List of permissions that are accepted
* @return `true` if `permission` is included in the `permissionList`
*/
public static checkPermission<T extends string> (
permission: PermissionString<T>,
acceptedPermissionList: PermissionList<T>
) : boolean {
if (!isArrayOf<string>(acceptedPermissionList)) {
LOG.warn(`Provided argument was not an array of strings: `, acceptedPermissionList);
return false;
}
return acceptedPermissionList.includes(permission);
}
/**
* Checks against multiple permissions
*
* @param checkPermissions List of permissions that will be included in the return object
* @param targetPermissions List of permissions the target has
* @returns PermissionObject State of each permission from `checkPermissions`
*/
public static checkPermissionList<T extends string> (
checkPermissions: PermissionList<T>,
targetPermissions: PermissionList<T>
) : PermissionObject {
return reduce(
checkPermissions,
(result: PermissionObject, permission: PermissionString<T>) : PermissionObject => {
return {
...result,
[permission]: PermissionUtils.checkPermission(
permission,
targetPermissions
)
};
},
{}
);
}
/**
* Verify permission object has all permissions enabled
*
* @param permissions
* @returns boolean `true` if every permission in the `permissions` object matches
*/
public static everyPermissionAccepted (permissions: PermissionObject) : boolean {
const permissionCount = values(permissions).length;
if (permissionCount === 0) return false;
return everyValue<boolean>(permissions, (item: boolean) : boolean => item);
}
/**
*
* @param permissions
* @param isPermissionKey
* @returns List of permissions that were enabled
*/
public static getAcceptedPermissionList<T extends string = string> (
permissions: PermissionObject,
isPermissionKey: TestCallbackNonStandard = isString
) : PermissionList<T> {
return filter(
keys(permissions),
(key: string) : boolean => isPermissionKey(key) && permissions[key]
) as unknown as PermissionList<T>;
}
/**
* Verify permission object has some permissions enabled
*
* @param permissions
* @returns boolean `true` if some permissions in the `permissions` object matches
*/
public static somePermissionAccepted (permissions: PermissionObject) : boolean {
const acceptedPermissions = this.getAcceptedPermissionList(permissions);
return acceptedPermissions.length !== 0;
}
/**
* Populates an array of permission value objects
*
* @param showPermissions
* @param enabledPermissions
*/
public static createPermissionValueObjectArray<T extends string> (
showPermissions: readonly T[],
enabledPermissions: readonly T[]
) : readonly PermissionValueObject<T>[] {
return map(
showPermissions,
(permission : T) : PermissionValueObject<T> => ({
permission,
value: enabledPermissions.includes(permission)
})
);
}
}