-
Notifications
You must be signed in to change notification settings - Fork 181
/
permissions__cache.ts
290 lines (248 loc) · 7.15 KB
/
permissions__cache.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
export interface PermissionType {
key: string
}
export interface Project {
id: string
}
export interface Permission {
permission: PermissionType
global?: boolean | null | undefined
projects?: readonly Project[] | null | undefined
}
interface PermissionCacheItem {
global: boolean | null | undefined
projectIdSet: Record<string, boolean> | null
}
/**
* Converts an array of cached permissions to a a map of a permission key
* to the respective cached permission.
*
* @param { {
* permission: {key: string},
* global: boolean?,
* projects: {id: string}[]?
* }[] } permissions
* @param {function} namesConverter a function, which maps a server-side permission name to client-side permission name
* @return {object} permission cache
* @private
*/
export default class PermissionCache {
static GLOBAL_PROJECT_ID = 'global';
/**
* Convert an array of projects to a set of project ids.
*
* @param {object[]=} projects
* @return {object} a set of project ids
* @private
*/
private static _toProjectIdSet(projects: readonly Project[] | null | undefined) {
let projectIdSet: Record<string, boolean> | null = null;
if (projects) {
projectIdSet = {};
for (let i = 0; i < projects.length; i++) {
projectIdSet[projects[i].id] = true;
}
}
return projectIdSet;
}
namesConverter: (name: string) => string | null | undefined;
constructor(
permissions?: readonly Permission[] | null | undefined,
namesConverter?: ((name: string) => string | null | undefined) | null | undefined
) {
this.namesConverter = namesConverter || (key => key);
this.set(permissions);
}
private _permissions?: readonly Permission[] | null;
permissionCache?: Record<string, PermissionCacheItem>;
set(permissions?: readonly Permission[] | null | undefined) {
const permissionCache = (permissions || []).reduce((
_permissionCache: Record<string, PermissionCacheItem>,
permission
) => {
const key = this.namesConverter(permission.permission.key);
if (key) {
_permissionCache[key] = {
global: permission.global,
projectIdSet: (this.constructor as typeof PermissionCache)._toProjectIdSet(
permission.projects
)
};
}
return _permissionCache;
}, {});
this._permissions = permissions;
this.permissionCache = permissionCache;
return this;
}
get() {
return this._permissions;
}
/**
* Checks if the current user has the given permissions in the project with the given id.
*
* @param {string} permissions space separated list of permissions
* @param {string=} projectId optional projectId. If absent the method checks
* if the given permission is granted in any project.
*
* @return {boolean}
*/
has(permissions?: string | null | undefined, projectId?: string | null | undefined) {
const lexems = this.lex(permissions);
if (lexems.length === 0) {
return true;
}
try {
return this.or(lexems, projectId);
} catch (e) {
return false;
}
}
/**
* Lexes permission query string to an array of lexems.
*
* @param {string} query
* @return {string[]}
*/
lex(query?: string | null | undefined) {
const lexems = [];
if (query) {
let currentIdentifier = '';
for (let i = 0; i < query.length; i++) {
switch (query.charAt(i)) {
case ' ':
case '\t':
case '\n':
case '\r':
// Finish current token
if (currentIdentifier) {
lexems.push(currentIdentifier);
currentIdentifier = '';
}
// Skip space
break;
case '(':
case ')':
case '&':
case '|':
case '!':
// Finish current token
if (currentIdentifier) {
lexems.push(currentIdentifier);
currentIdentifier = '';
}
// Append operator
lexems.push(query.charAt(i));
break;
default:
currentIdentifier += query.charAt(i);
break;
}
}
if (currentIdentifier) {
lexems.push(currentIdentifier);
}
}
return lexems;
}
/*
or -> and ( '|' and )*
and -> not ( '&'? not )*
not -> '!'* term
term -> '(' or ')' | permission
permission -> [^()&|!\s]+
*/
/**
* @param {string[]} lexems
* @param {string=} projectId
* @return {boolean}
*/
or(lexems: string[], projectId?: string | null | undefined) {
let result = this.and(lexems, projectId);
while (lexems.length > 0 && lexems[0] !== ')') {
if (lexems.shift() !== '|') {
throw new Error('Operator \'|\' was expected');
}
result = this.and(lexems, projectId) || result;
}
return result;
}
/**
* @param {string[]} lexems
* @param {string=} projectId
* @return {boolean}
*/
and(lexems: string[], projectId?: string | null | undefined) {
let result = this.not(lexems, projectId);
while (lexems.length > 0 && lexems[0] !== ')' && lexems[0] !== '|') {
// Expect optional '&'
if (lexems[0] === '&') {
lexems.shift();
}
result = this.not(lexems, projectId) && result;
}
return result;
}
/**
* @param {string[]} lexems
* @param {string=} projectId
* @return {boolean}
*/
not(lexems: string[], projectId?: string | null | undefined) {
let notCounter = 0;
while (lexems.length > 0 && lexems[0] === '!') {
++notCounter;
lexems.shift();
}
const result = this.term(lexems, projectId);
return (notCounter % 2 === 0) ? result : !result;
}
/**
* @param {string[]} lexems
* @param {string=} projectId
* @return {boolean}
*/
term(lexems: string[], projectId?: string | null | undefined): boolean {
if (lexems.length === 0) {
throw new Error('Operand was expected');
}
const t = lexems.shift();
let result;
// Nested parenthesized expression
if (t === '(') {
result = this.or(lexems, projectId);
// Expect ')'
if (lexems.shift() !== ')') {
throw new Error('Operator \')\' was expected');
}
} else {
result = t != null && this.testPermission(t, projectId);
}
return result;
}
/**
* @param {string} permissionName
* @param {string=} projectId
* @return {boolean}
*/
testPermission(permissionName: string, projectId?: string | null | undefined) {
const permissionCache = this.permissionCache;
const convertedName = this.namesConverter(permissionName);
const cachedPermission = permissionCache?.[permissionName] ||
convertedName && permissionCache?.[convertedName];
// Hasn't the permission in any project
if (!cachedPermission) {
return false;
}
// The permission is global or is given in the global project
if (cachedPermission.global) {
return true;
}
if (projectId) {
// if projectId is specified check that the permission is given in the project
return cachedPermission.projectIdSet != null && (projectId in cachedPermission.projectIdSet);
} else {
return true;
}
}
}