@via-profit/ability 2.0.0-rc.8 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,814 +0,0 @@
1
- /******/ (() => { // webpackBootstrap
2
- /******/ "use strict";
3
- /******/ var __webpack_modules__ = ({
4
-
5
- /***/ "./src/AbilityCode.ts":
6
- /*!****************************!*\
7
- !*** ./src/AbilityCode.ts ***!
8
- \****************************/
9
- /***/ ((__unused_webpack_module, exports) => {
10
-
11
-
12
- Object.defineProperty(exports, "__esModule", ({ value: true }));
13
- exports.AbilityCode = void 0;
14
- class AbilityCode {
15
- code;
16
- constructor(code) {
17
- this.code = code;
18
- }
19
- isEqual(compareWith) {
20
- return compareWith !== null && this.code === compareWith.code;
21
- }
22
- isNotEqual(compareWith) {
23
- return !this.isEqual(compareWith);
24
- }
25
- static fromLiteral(literal) {
26
- let Code = null;
27
- Object.keys(this).forEach(member => {
28
- if (member === literal) {
29
- Code = this[member];
30
- }
31
- });
32
- if (!Code) {
33
- throw new Error(`Mismatch error. The literal ${literal} can not be find as a member of this class`);
34
- }
35
- return new this(Code.code);
36
- }
37
- }
38
- exports.AbilityCode = AbilityCode;
39
- exports["default"] = AbilityCode;
40
-
41
-
42
- /***/ }),
43
-
44
- /***/ "./src/AbilityCompare.ts":
45
- /*!*******************************!*\
46
- !*** ./src/AbilityCompare.ts ***!
47
- \*******************************/
48
- /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
49
-
50
-
51
- var __importDefault = (this && this.__importDefault) || function (mod) {
52
- return (mod && mod.__esModule) ? mod : { "default": mod };
53
- };
54
- Object.defineProperty(exports, "__esModule", ({ value: true }));
55
- exports.AbilityCompare = void 0;
56
- const AbilityCode_1 = __importDefault(__webpack_require__(/*! ./AbilityCode */ "./src/AbilityCode.ts"));
57
- class AbilityCompare extends AbilityCode_1.default {
58
- static and = new AbilityCompare('and');
59
- static or = new AbilityCompare('or');
60
- }
61
- exports.AbilityCompare = AbilityCompare;
62
- exports["default"] = AbilityCompare;
63
-
64
-
65
- /***/ }),
66
-
67
- /***/ "./src/AbilityCondition.ts":
68
- /*!*********************************!*\
69
- !*** ./src/AbilityCondition.ts ***!
70
- \*********************************/
71
- /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
72
-
73
-
74
- var __importDefault = (this && this.__importDefault) || function (mod) {
75
- return (mod && mod.__esModule) ? mod : { "default": mod };
76
- };
77
- Object.defineProperty(exports, "__esModule", ({ value: true }));
78
- exports.AbilityCondition = void 0;
79
- const AbilityCode_1 = __importDefault(__webpack_require__(/*! ./AbilityCode */ "./src/AbilityCode.ts"));
80
- class AbilityCondition extends AbilityCode_1.default {
81
- static EQUAL = new AbilityCondition('=');
82
- static NOT_EQUAL = new AbilityCondition('<>');
83
- static MORE_THAN = new AbilityCondition('>');
84
- static LESS_THAN = new AbilityCondition('<');
85
- static LESS_OR_EQUAL = new AbilityCondition('<=');
86
- static MORE_OR_EQUAL = new AbilityCondition('>=');
87
- static IN = new AbilityCondition('in');
88
- static NOT_IN = new AbilityCondition('not in');
89
- }
90
- exports.AbilityCondition = AbilityCondition;
91
- exports["default"] = AbilityCondition;
92
-
93
-
94
- /***/ }),
95
-
96
- /***/ "./src/AbilityError.ts":
97
- /*!*****************************!*\
98
- !*** ./src/AbilityError.ts ***!
99
- \*****************************/
100
- /***/ ((__unused_webpack_module, exports) => {
101
-
102
-
103
- Object.defineProperty(exports, "__esModule", ({ value: true }));
104
- exports.PermissionError = exports.AbilityParserError = exports.AbilityError = void 0;
105
- class AbilityError extends Error {
106
- constructor(message) {
107
- super(message);
108
- }
109
- }
110
- exports.AbilityError = AbilityError;
111
- class AbilityParserError extends Error {
112
- constructor(message) {
113
- super(message);
114
- }
115
- }
116
- exports.AbilityParserError = AbilityParserError;
117
- class PermissionError extends Error {
118
- constructor(message) {
119
- super(message);
120
- }
121
- }
122
- exports.PermissionError = PermissionError;
123
-
124
-
125
- /***/ }),
126
-
127
- /***/ "./src/AbilityMatch.ts":
128
- /*!*****************************!*\
129
- !*** ./src/AbilityMatch.ts ***!
130
- \*****************************/
131
- /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
132
-
133
-
134
- var __importDefault = (this && this.__importDefault) || function (mod) {
135
- return (mod && mod.__esModule) ? mod : { "default": mod };
136
- };
137
- Object.defineProperty(exports, "__esModule", ({ value: true }));
138
- exports.AbilityMatch = void 0;
139
- const AbilityCode_1 = __importDefault(__webpack_require__(/*! ./AbilityCode */ "./src/AbilityCode.ts"));
140
- class AbilityMatch extends AbilityCode_1.default {
141
- static pending = new AbilityMatch('pending');
142
- static match = new AbilityMatch('match');
143
- static mismatch = new AbilityMatch('mismatch');
144
- }
145
- exports.AbilityMatch = AbilityMatch;
146
- exports["default"] = AbilityMatch;
147
-
148
-
149
- /***/ }),
150
-
151
- /***/ "./src/AbilityPolicy.ts":
152
- /*!******************************!*\
153
- !*** ./src/AbilityPolicy.ts ***!
154
- \******************************/
155
- /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
156
-
157
-
158
- var __importDefault = (this && this.__importDefault) || function (mod) {
159
- return (mod && mod.__esModule) ? mod : { "default": mod };
160
- };
161
- Object.defineProperty(exports, "__esModule", ({ value: true }));
162
- exports.AbilityPolicy = void 0;
163
- const AbilityRuleSet_1 = __importDefault(__webpack_require__(/*! ./AbilityRuleSet */ "./src/AbilityRuleSet.ts"));
164
- const AbilityMatch_1 = __importDefault(__webpack_require__(/*! ./AbilityMatch */ "./src/AbilityMatch.ts"));
165
- const AbilityCompare_1 = __importDefault(__webpack_require__(/*! ./AbilityCompare */ "./src/AbilityCompare.ts"));
166
- const AbilityPolicyEffect_1 = __importDefault(__webpack_require__(/*! ./AbilityPolicyEffect */ "./src/AbilityPolicyEffect.ts"));
167
- class AbilityPolicy {
168
- matchState = AbilityMatch_1.default.pending;
169
- /**
170
- * List of rules
171
- */
172
- ruleSet = [];
173
- /**
174
- * Policy effect
175
- */
176
- effect;
177
- /**
178
- * Rules compare method.\
179
- * For the «and» method the rule will be permitted if all\
180
- * rules will be returns «permit» status and for the «or» - if\
181
- * one of the rules returns as «permit»
182
- */
183
- compareMethod = AbilityCompare_1.default.and;
184
- /**
185
- * Policy name
186
- */
187
- name;
188
- /**
189
- * Policy ID
190
- */
191
- id;
192
- /**
193
- * Soon
194
- */
195
- action;
196
- constructor(params) {
197
- const { name, id, action, effect } = params;
198
- this.name = name;
199
- this.id = id;
200
- this.action = action;
201
- this.effect = effect;
202
- }
203
- /**
204
- * Add rule set to the policy
205
- * @param ruleSet - The rule set to add
206
- */
207
- addRuleSet(ruleSet) {
208
- this.ruleSet.push(ruleSet);
209
- return this;
210
- }
211
- /**
212
- * Check if the policy is matched
213
- * @param resources - The resource to check
214
- */
215
- check(resources) {
216
- this.matchState = AbilityMatch_1.default.mismatch;
217
- if (!this.ruleSet.length) {
218
- return this.matchState;
219
- }
220
- const rulesetCheckStates = this.ruleSet.reduce((collect, ruleSet) => {
221
- return collect.concat(ruleSet.check(resources));
222
- }, []);
223
- if (AbilityCompare_1.default.and.isEqual(this.compareMethod)) {
224
- if (rulesetCheckStates.every(ruleState => AbilityMatch_1.default.match.isEqual(ruleState))) {
225
- this.matchState = AbilityMatch_1.default.match;
226
- }
227
- }
228
- if (AbilityCompare_1.default.or.isEqual(this.compareMethod)) {
229
- if (rulesetCheckStates.some(ruleState => AbilityMatch_1.default.match.isEqual(ruleState))) {
230
- this.matchState = AbilityMatch_1.default.match;
231
- }
232
- }
233
- return this.matchState;
234
- }
235
- /**
236
- * Parse the config JSON format to Policy class instance
237
- */
238
- static parse(config) {
239
- const { id, name, ruleSet, compareMethod, action, effect } = config;
240
- // Create the empty policy
241
- const policy = new AbilityPolicy({
242
- name,
243
- id,
244
- action,
245
- effect: new AbilityPolicyEffect_1.default(effect),
246
- });
247
- policy.compareMethod = AbilityCompare_1.default.fromLiteral(compareMethod);
248
- ruleSet.forEach(ruleSetConfig => {
249
- policy.addRuleSet(AbilityRuleSet_1.default.parse(ruleSetConfig));
250
- });
251
- return policy;
252
- }
253
- export() {
254
- return {
255
- id: this.id.toString(),
256
- name: this.name.toString(),
257
- compareMethod: this.compareMethod.code.toString(),
258
- ruleSet: this.ruleSet.map(rule => rule.export()),
259
- action: this.action,
260
- effect: this.effect.code,
261
- };
262
- }
263
- }
264
- exports.AbilityPolicy = AbilityPolicy;
265
- exports["default"] = AbilityPolicy;
266
-
267
-
268
- /***/ }),
269
-
270
- /***/ "./src/AbilityPolicyEffect.ts":
271
- /*!************************************!*\
272
- !*** ./src/AbilityPolicyEffect.ts ***!
273
- \************************************/
274
- /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
275
-
276
-
277
- var __importDefault = (this && this.__importDefault) || function (mod) {
278
- return (mod && mod.__esModule) ? mod : { "default": mod };
279
- };
280
- Object.defineProperty(exports, "__esModule", ({ value: true }));
281
- exports.AbilityPolicyEffect = void 0;
282
- const AbilityCode_1 = __importDefault(__webpack_require__(/*! ./AbilityCode */ "./src/AbilityCode.ts"));
283
- class AbilityPolicyEffect extends AbilityCode_1.default {
284
- static deny = new AbilityPolicyEffect('deny');
285
- static permit = new AbilityPolicyEffect('permit');
286
- }
287
- exports.AbilityPolicyEffect = AbilityPolicyEffect;
288
- exports["default"] = AbilityPolicyEffect;
289
-
290
-
291
- /***/ }),
292
-
293
- /***/ "./src/AbilityResolver.ts":
294
- /*!********************************!*\
295
- !*** ./src/AbilityResolver.ts ***!
296
- \********************************/
297
- /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
298
-
299
-
300
- var __importDefault = (this && this.__importDefault) || function (mod) {
301
- return (mod && mod.__esModule) ? mod : { "default": mod };
302
- };
303
- Object.defineProperty(exports, "__esModule", ({ value: true }));
304
- exports.AbilityResolver = void 0;
305
- const AbilityPolicyEffect_1 = __importDefault(__webpack_require__(/*! ./AbilityPolicyEffect */ "./src/AbilityPolicyEffect.ts"));
306
- const AbilityMatch_1 = __importDefault(__webpack_require__(/*! ./AbilityMatch */ "./src/AbilityMatch.ts"));
307
- const AbilityError_1 = __webpack_require__(/*! ./AbilityError */ "./src/AbilityError.ts");
308
- class AbilityResolver {
309
- policies;
310
- constructor(policyOrListOfPolicies) {
311
- this.policies = Array.isArray(policyOrListOfPolicies)
312
- ? policyOrListOfPolicies
313
- : [policyOrListOfPolicies];
314
- }
315
- /**
316
- * Resolve policy for the resource and action
317
- *
318
- @param action - Action
319
- * @param resource - Resource
320
- */
321
- resolve(action, resource) {
322
- const filteredPolicies = this.policies.filter(policy => {
323
- return AbilityResolver.isInActionContain(policy.action, String(action));
324
- });
325
- filteredPolicies.map(policy => policy.check(resource));
326
- this.policies = filteredPolicies;
327
- return this;
328
- }
329
- enforce(action, resource) {
330
- const resolver = this.resolve(action, resource);
331
- if (resolver) {
332
- if (resolver.isDeny()) {
333
- throw new AbilityError_1.PermissionError(resolver.getMatchedPolicy()?.name?.toString() || 'Unknown permission error');
334
- }
335
- }
336
- }
337
- /**
338
- * Get the last effect of the policy
339
- *
340
- * @returns {AbilityPolicyEffect | null}
341
- */
342
- getEffect() {
343
- const effects = this.policies.reduce((collect, policy, _index) => {
344
- if (policy.matchState.isEqual(AbilityMatch_1.default.match)) {
345
- return collect.concat(policy.effect);
346
- }
347
- return collect;
348
- }, []);
349
- if (effects.length) {
350
- return effects[effects.length - 1];
351
- }
352
- return null;
353
- }
354
- isPermit() {
355
- const effect = this.getEffect();
356
- return effect !== null && effect.isEqual(AbilityPolicyEffect_1.default.permit);
357
- }
358
- isDeny() {
359
- const effect = this.getEffect();
360
- return effect !== null && effect.isEqual(AbilityPolicyEffect_1.default.deny);
361
- }
362
- getMatchedPolicy() {
363
- const matchedPolicies = this.policies.filter(policy => policy.matchState.isEqual(AbilityMatch_1.default.match));
364
- const lastPolicy = matchedPolicies.length ? matchedPolicies[matchedPolicies.length - 1] : null;
365
- return lastPolicy || null;
366
- }
367
- /**
368
- * Check if the action is contained in another action
369
- * @param actionA - The first action to check
370
- * @param actionB - The second action to check
371
- */
372
- static isInActionContain(actionA, actionB) {
373
- const actionAArray = String(actionA).split('.');
374
- const actionBArray = String(actionB).split('.');
375
- const a = actionAArray.length >= actionBArray.length ? actionAArray : actionBArray;
376
- const b = actionBArray.length <= actionAArray.length ? actionBArray : actionAArray;
377
- const c = a
378
- .reduce((acc, chunk, index) => {
379
- const iterationRes = chunk === b[index] || b[index] === '*' || chunk === '*';
380
- return acc.concat(iterationRes);
381
- }, []);
382
- return c.every(Boolean);
383
- }
384
- }
385
- exports.AbilityResolver = AbilityResolver;
386
- exports["default"] = AbilityResolver;
387
-
388
-
389
- /***/ }),
390
-
391
- /***/ "./src/AbilityRule.ts":
392
- /*!****************************!*\
393
- !*** ./src/AbilityRule.ts ***!
394
- \****************************/
395
- /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
396
-
397
-
398
- var __importDefault = (this && this.__importDefault) || function (mod) {
399
- return (mod && mod.__esModule) ? mod : { "default": mod };
400
- };
401
- Object.defineProperty(exports, "__esModule", ({ value: true }));
402
- exports.AbilityRule = void 0;
403
- const AbilityMatch_1 = __importDefault(__webpack_require__(/*! ./AbilityMatch */ "./src/AbilityMatch.ts"));
404
- const AbilityCondition_1 = __importDefault(__webpack_require__(/*! ./AbilityCondition */ "./src/AbilityCondition.ts"));
405
- class AbilityRule {
406
- /**
407
- * Subject key path like a 'user.name'
408
- */
409
- subject;
410
- /**
411
- * Resource key path like a 'user.name' or value
412
- */
413
- resource;
414
- condition;
415
- name;
416
- id;
417
- state = AbilityMatch_1.default.pending;
418
- constructor(params) {
419
- const { id, name, subject, resource, condition } = params;
420
- this.id = id;
421
- this.name = name;
422
- this.subject = subject;
423
- this.resource = resource;
424
- this.condition = new AbilityCondition_1.default(condition);
425
- }
426
- /**
427
- * Check if the rule is matched
428
- * @param resource - The resource to check
429
- */
430
- check(resource) {
431
- let is = false;
432
- const [valueS, valueO] = this.extractValues(resource);
433
- if (AbilityCondition_1.default.LESS_THAN.isEqual(this.condition)) {
434
- is = Number(valueS) < Number(valueO);
435
- }
436
- if (AbilityCondition_1.default.LESS_OR_EQUAL.isEqual(this.condition)) {
437
- is = Number(valueS) <= Number(valueO);
438
- }
439
- if (AbilityCondition_1.default.MORE_THAN.isEqual(this.condition)) {
440
- is = Number(valueS) > Number(valueO);
441
- }
442
- if (AbilityCondition_1.default.MORE_OR_EQUAL.isEqual(this.condition)) {
443
- is = Number(valueS) >= Number(valueO);
444
- }
445
- if (AbilityCondition_1.default.EQUAL.isEqual(this.condition)) {
446
- is = valueS === valueO;
447
- }
448
- if (AbilityCondition_1.default.NOT_EQUAL.isEqual(this.condition)) {
449
- is = valueS !== valueO;
450
- }
451
- if (AbilityCondition_1.default.IN.isEqual(this.condition)) {
452
- // [<some>] and [<some>]
453
- if (Array.isArray(valueS) && Array.isArray(valueO)) {
454
- is = valueS.some(v => valueO.find(v1 => v1 === v));
455
- }
456
- // <some> and [<some>]
457
- if ((typeof valueS === 'string' || typeof valueS === 'number') && Array.isArray(valueO)) {
458
- is = valueO.includes(valueS);
459
- }
460
- // [<some>] and <some>
461
- if ((typeof valueO === 'string' || typeof valueO === 'number') && Array.isArray(valueS)) {
462
- is = valueS.includes(valueO);
463
- }
464
- }
465
- if (AbilityCondition_1.default.NOT_IN.isEqual(this.condition)) {
466
- // [<some>] and [<some>]
467
- if (Array.isArray(valueS) && Array.isArray(valueO)) {
468
- is = !valueS.some(v => valueO.find(v1 => v1 === v));
469
- }
470
- // <some> and [<some>]
471
- if ((typeof valueS === 'string' || typeof valueS === 'number') && Array.isArray(valueO)) {
472
- is = !valueO.includes(valueS);
473
- }
474
- // [<some>] and <some>
475
- if ((typeof valueO === 'string' || typeof valueO === 'number') && Array.isArray(valueS)) {
476
- is = !valueS.includes(valueO);
477
- }
478
- }
479
- this.state = is ? AbilityMatch_1.default.match : AbilityMatch_1.default.mismatch;
480
- return this.state;
481
- }
482
- /**
483
- * Extract values from the resourceData
484
- * @param resourceData - The resourceData to extract values from
485
- */
486
- extractValues(resourceData) {
487
- let leftSideValue;
488
- let rightSideValue;
489
- if (resourceData === null || typeof resourceData === 'undefined') {
490
- return [NaN, NaN];
491
- }
492
- const isPath = (str) => {
493
- return typeof str === 'string' && str.match(/\./g) !== null;
494
- };
495
- if (isPath(this.subject)) {
496
- leftSideValue = this.getDotNotationValue(resourceData, this.subject);
497
- }
498
- else {
499
- leftSideValue = this.subject;
500
- }
501
- if (isPath(this.resource)) {
502
- rightSideValue = this.getDotNotationValue(resourceData, this.resource);
503
- }
504
- else {
505
- rightSideValue = this.resource;
506
- }
507
- return [leftSideValue, rightSideValue];
508
- }
509
- /**
510
- * Get the value of the object by dot notation
511
- * @param resource - The object to get the value from
512
- * @param desc - The dot notation string
513
- */
514
- getDotNotationValue(resource, desc) {
515
- const arr = desc.split('.');
516
- while (arr.length && resource) {
517
- const comp = arr.shift() || '';
518
- const match = new RegExp('(.+)\\[([0-9]*)\\]').exec(comp);
519
- if (match !== null && match.length == 3) {
520
- const arrayData = {
521
- arrName: match[1],
522
- arrIndex: match[2],
523
- };
524
- if (resource[arrayData.arrName] !== undefined) {
525
- resource = resource[arrayData.arrName][arrayData.arrIndex];
526
- }
527
- else {
528
- resource = undefined;
529
- }
530
- }
531
- else {
532
- resource = resource[comp];
533
- }
534
- }
535
- return resource;
536
- }
537
- static parse(config) {
538
- const { id, name, subject, resource, condition } = config;
539
- return new AbilityRule({
540
- id,
541
- name,
542
- subject,
543
- resource,
544
- condition,
545
- });
546
- }
547
- /**
548
- * Export the rule to config object
549
- */
550
- export() {
551
- return {
552
- id: this.id,
553
- name: this.name,
554
- subject: this.subject,
555
- resource: this.resource,
556
- condition: this.condition.code,
557
- };
558
- }
559
- }
560
- exports.AbilityRule = AbilityRule;
561
- exports["default"] = AbilityRule;
562
-
563
-
564
- /***/ }),
565
-
566
- /***/ "./src/AbilityRuleSet.ts":
567
- /*!*******************************!*\
568
- !*** ./src/AbilityRuleSet.ts ***!
569
- \*******************************/
570
- /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
571
-
572
-
573
- var __importDefault = (this && this.__importDefault) || function (mod) {
574
- return (mod && mod.__esModule) ? mod : { "default": mod };
575
- };
576
- Object.defineProperty(exports, "__esModule", ({ value: true }));
577
- exports.AbilityRuleSet = void 0;
578
- const AbilityRule_1 = __importDefault(__webpack_require__(/*! ./AbilityRule */ "./src/AbilityRule.ts"));
579
- const AbilityCompare_1 = __importDefault(__webpack_require__(/*! ./AbilityCompare */ "./src/AbilityCompare.ts"));
580
- const AbilityMatch_1 = __importDefault(__webpack_require__(/*! ./AbilityMatch */ "./src/AbilityMatch.ts"));
581
- class AbilityRuleSet {
582
- state = AbilityMatch_1.default.pending;
583
- /**
584
- * List of rules
585
- */
586
- rules = [];
587
- /**
588
- * Rules compare method.\
589
- * For the «and» method the rule will be permitted if all\
590
- * rules will be returns «permit» status and for the «or» - if\
591
- * one of the rules returns as «permit»
592
- */
593
- compareMethod = AbilityCompare_1.default.and;
594
- /**
595
- * Group name
596
- */
597
- name;
598
- /**
599
- * Group ID
600
- */
601
- id;
602
- constructor(params) {
603
- const { name, id, compareMethod } = params;
604
- this.name = name;
605
- this.id = id;
606
- this.compareMethod = AbilityCompare_1.default.fromLiteral(compareMethod);
607
- // this.compareMethod = new AbilityCompare(compareMethod);
608
- }
609
- addRule(rule, compareMethod) {
610
- this.rules.push(rule);
611
- this.compareMethod = compareMethod;
612
- return this;
613
- }
614
- addRules(rules, compareMethod) {
615
- rules.forEach(rule => this.addRule(rule, compareMethod));
616
- return this;
617
- }
618
- check(resources) {
619
- this.state = AbilityMatch_1.default.mismatch;
620
- if (!this.rules.length) {
621
- return this.state;
622
- }
623
- const ruleCheckStates = this.rules.reduce((collect, rule) => {
624
- return collect.concat(rule.check(resources));
625
- }, []);
626
- if (AbilityCompare_1.default.and.isEqual(this.compareMethod)) {
627
- if (ruleCheckStates.every(ruleState => AbilityMatch_1.default.match.isEqual(ruleState))) {
628
- this.state = AbilityMatch_1.default.match;
629
- }
630
- }
631
- if (AbilityCompare_1.default.or.isEqual(this.compareMethod)) {
632
- if (ruleCheckStates.some(ruleState => AbilityMatch_1.default.match.isEqual(ruleState))) {
633
- this.state = AbilityMatch_1.default.match;
634
- }
635
- }
636
- return this.state;
637
- }
638
- /**
639
- * Parse the config JSON format to Group class instance
640
- */
641
- static parse(config) {
642
- const { id, name, rules, compareMethod } = config;
643
- const ruleSet = new AbilityRuleSet({
644
- compareMethod,
645
- name,
646
- id,
647
- });
648
- // Adding rules if exists
649
- if (rules && rules.length > 0) {
650
- const abilityRules = rules.map(ruleConfig => AbilityRule_1.default.parse(ruleConfig));
651
- ruleSet.addRules(abilityRules, ruleSet.compareMethod);
652
- }
653
- return ruleSet;
654
- }
655
- export() {
656
- return {
657
- id: this.id.toString(),
658
- name: this.name.toString(),
659
- compareMethod: this.compareMethod.code.toString(),
660
- rules: this.rules.map(rule => rule.export()),
661
- };
662
- }
663
- }
664
- exports.AbilityRuleSet = AbilityRuleSet;
665
- exports["default"] = AbilityRuleSet;
666
-
667
-
668
- /***/ }),
669
-
670
- /***/ "./src/playground.ts":
671
- /*!***************************!*\
672
- !*** ./src/playground.ts ***!
673
- \***************************/
674
- /***/ (function(__unused_webpack_module, exports, __webpack_require__) {
675
-
676
-
677
- var __importDefault = (this && this.__importDefault) || function (mod) {
678
- return (mod && mod.__esModule) ? mod : { "default": mod };
679
- };
680
- Object.defineProperty(exports, "__esModule", ({ value: true }));
681
- const node_http_1 = __importDefault(__webpack_require__(/*! node:http */ "node:http"));
682
- const AbilityPolicy_1 = __importDefault(__webpack_require__(/*! ./AbilityPolicy */ "./src/AbilityPolicy.ts"));
683
- const AbilityResolver_1 = __importDefault(__webpack_require__(/*! ~/AbilityResolver */ "./src/AbilityResolver.ts"));
684
- const server = node_http_1.default.createServer();
685
- server.on('request', (_req, res) => {
686
- const config = [
687
- {
688
- id: 'bb758c1b-1015-4894-ba25-d23156e063cf',
689
- name: 'Status hui',
690
- action: 'order.status',
691
- effect: 'deny',
692
- compareMethod: 'and',
693
- ruleSet: [
694
- {
695
- id: '9cc009e5-0aa9-453a-a668-cb3f418ced92',
696
- name: 'Не администратор',
697
- compareMethod: 'and',
698
- rules: [
699
- {
700
- id: '4093cd50-e54f-4062-8053-2d3b5966fad3',
701
- name: 'Нет роли администраторв',
702
- subject: 'account.roles',
703
- resource: 'administrator',
704
- condition: '<>',
705
- },
706
- ],
707
- },
708
- ]
709
- },
710
- {
711
- id: 'bb758c1b-1015-4894-ba25-d23156e063cf',
712
- name: 'Status hui 1',
713
- action: 'order.status',
714
- effect: 'permit',
715
- compareMethod: 'and',
716
- ruleSet: [
717
- {
718
- id: '2f8f9d71-860b-4fa6-b395-9331f1f0848e',
719
- name: 'Rule set',
720
- compareMethod: 'and',
721
- rules: [
722
- {
723
- id: 'a3c7d66f-5c2d-4a24-83bc-03b0a2d9c32b',
724
- name: 'Rule 1',
725
- subject: 'order.status',
726
- resource: 'не обработан',
727
- condition: '=',
728
- },
729
- {
730
- id: 'a3c7d66f-5c2d-4a24-83bc-03b0a2d9c32b',
731
- name: 'Rule 2',
732
- subject: 'feature.status',
733
- resource: 'отменен',
734
- condition: '=',
735
- },
736
- ],
737
- },
738
- ],
739
- },
740
- ];
741
- const policies = config.map(cfg => AbilityPolicy_1.default.parse(cfg));
742
- const result = new AbilityResolver_1.default(policies).resolve('order.status', {
743
- account: {
744
- roles: ['user', 'couch'],
745
- },
746
- order: {
747
- status: 'не обработан',
748
- },
749
- feature: {
750
- status: 'отменен',
751
- // status: 'завершен'
752
- },
753
- });
754
- res.statusCode = 200;
755
- res.setHeader('content-type', 'application/json');
756
- res.write(JSON.stringify({
757
- status: result.isDeny() ? 'deny' : 'permit',
758
- }));
759
- res.end();
760
- });
761
- server.listen(8081, 'localhost', () => {
762
- console.debug('server started at http://localhost:8080');
763
- });
764
-
765
-
766
- /***/ }),
767
-
768
- /***/ "node:http":
769
- /*!****************************!*\
770
- !*** external "node:http" ***!
771
- \****************************/
772
- /***/ ((module) => {
773
-
774
- module.exports = require("node:http");
775
-
776
- /***/ })
777
-
778
- /******/ });
779
- /************************************************************************/
780
- /******/ // The module cache
781
- /******/ var __webpack_module_cache__ = {};
782
- /******/
783
- /******/ // The require function
784
- /******/ function __webpack_require__(moduleId) {
785
- /******/ // Check if module is in cache
786
- /******/ var cachedModule = __webpack_module_cache__[moduleId];
787
- /******/ if (cachedModule !== undefined) {
788
- /******/ return cachedModule.exports;
789
- /******/ }
790
- /******/ // Create a new module (and put it into the cache)
791
- /******/ var module = __webpack_module_cache__[moduleId] = {
792
- /******/ // no module.id needed
793
- /******/ // no module.loaded needed
794
- /******/ exports: {}
795
- /******/ };
796
- /******/
797
- /******/ // Execute the module function
798
- /******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
799
- /******/
800
- /******/ // Return the exports of the module
801
- /******/ return module.exports;
802
- /******/ }
803
- /******/
804
- /************************************************************************/
805
- /******/
806
- /******/ // startup
807
- /******/ // Load entry module and return exports
808
- /******/ // This entry module is referenced by other modules so it can't be inlined
809
- /******/ var __webpack_exports__ = __webpack_require__("./src/playground.ts");
810
- /******/ module.exports = __webpack_exports__;
811
- /******/
812
- /******/ })()
813
- ;
814
- //# sourceMappingURL=playground.js.map