@strapi/permissions 0.0.0-next.e9bb5ccdc459f4c6b6717a2d5d86359b7a47d47d → 0.0.0-next.ec9b1b708d4d319f2b8b39d9397bd752d250d541
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.
- package/LICENSE +18 -3
- package/dist/domain/index.d.ts +3 -0
- package/dist/domain/index.d.ts.map +1 -0
- package/dist/domain/permission/index.d.ts +26 -0
- package/dist/domain/permission/index.d.ts.map +1 -0
- package/dist/engine/abilities/casl-ability.d.ts +12 -0
- package/dist/engine/abilities/casl-ability.d.ts.map +1 -0
- package/dist/engine/abilities/index.d.ts +2 -0
- package/dist/engine/abilities/index.d.ts.map +1 -0
- package/dist/engine/hooks.d.ts +55 -0
- package/dist/engine/hooks.d.ts.map +1 -0
- package/dist/engine/index.d.ts +28 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +297 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +275 -0
- package/dist/index.mjs.map +1 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +23 -7
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -4
- package/index.d.ts +0 -54
- package/lib/domain/index.js +0 -7
- package/lib/domain/permission/index.js +0 -68
- package/lib/engine/abilities/casl-ability.js +0 -57
- package/lib/engine/abilities/index.js +0 -7
- package/lib/engine/hooks.js +0 -97
- package/lib/engine/index.js +0 -209
- package/lib/index.js +0 -9
package/LICENSE
CHANGED
|
@@ -2,7 +2,21 @@ Copyright (c) 2015-present Strapi Solutions SAS
|
|
|
2
2
|
|
|
3
3
|
Portions of the Strapi software are licensed as follows:
|
|
4
4
|
|
|
5
|
-
* All software that resides under an "ee/" directory (the “EE Software”), if that directory exists, is licensed under the license defined
|
|
5
|
+
* All software that resides under an "ee/" directory (the “EE Software”), if that directory exists, is licensed under the license defined below.
|
|
6
|
+
|
|
7
|
+
Enterprise License
|
|
8
|
+
|
|
9
|
+
If you or the company you represent has entered into a written agreement referencing the Enterprise Edition of the Strapi source code available at
|
|
10
|
+
https://github.com/strapi/strapi, then such agreement applies to your use of the Enterprise Edition of the Strapi Software. If you or the company you
|
|
11
|
+
represent is using the Enterprise Edition of the Strapi Software in connection with a subscription to our cloud offering, then the agreement you have
|
|
12
|
+
agreed to with respect to our cloud offering and the licenses included in such agreement apply to your use of the Enterprise Edition of the Strapi Software.
|
|
13
|
+
Otherwise, the Strapi Enterprise Software License Agreement (found here https://strapi.io/enterprise-terms) applies to your use of the Enterprise Edition of the Strapi Software.
|
|
14
|
+
|
|
15
|
+
BY ACCESSING OR USING THE ENTERPRISE EDITION OF THE STRAPI SOFTWARE, YOU ARE AGREEING TO BE BOUND BY THE RELEVANT REFERENCED AGREEMENT.
|
|
16
|
+
IF YOU ARE NOT AUTHORIZED TO ACCEPT THESE TERMS ON BEHALF OF THE COMPANY YOU REPRESENT OR IF YOU DO NOT AGREE TO ALL OF THE RELEVANT TERMS AND CONDITIONS REFERENCED AND YOU
|
|
17
|
+
HAVE NOT OTHERWISE EXECUTED A WRITTEN AGREEMENT WITH STRAPI, YOU ARE NOT AUTHORIZED TO ACCESS OR USE OR ALLOW ANY USER TO ACCESS OR USE ANY PART OF
|
|
18
|
+
THE ENTERPRISE EDITION OF THE STRAPI SOFTWARE. YOUR ACCESS RIGHTS ARE CONDITIONAL ON YOUR CONSENT TO THE RELEVANT REFERENCED TERMS TO THE EXCLUSION OF ALL OTHER TERMS;
|
|
19
|
+
IF THE RELEVANT REFERENCED TERMS ARE CONSIDERED AN OFFER BY YOU, ACCEPTANCE IS EXPRESSLY LIMITED TO THE RELEVANT REFERENCED TERMS.
|
|
6
20
|
|
|
7
21
|
* All software outside of the above-mentioned directories or restrictions above is available under the "MIT Expat" license as set forth below.
|
|
8
22
|
|
|
@@ -18,5 +32,6 @@ furnished to do so, subject to the following conditions:
|
|
|
18
32
|
The above copyright notice and this permission notice shall be included in all
|
|
19
33
|
copies or substantial portions of the Software.
|
|
20
34
|
|
|
21
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
|
|
35
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
36
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
37
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/domain/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAE3C,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference types="lodash" />
|
|
2
|
+
import _ from 'lodash/fp';
|
|
3
|
+
declare const sanitizePermissionFields: _.LodashPick2x1;
|
|
4
|
+
export interface Permission {
|
|
5
|
+
action: string;
|
|
6
|
+
actionParameters?: Record<string, unknown>;
|
|
7
|
+
subject?: string | object | null;
|
|
8
|
+
properties?: Record<string, any>;
|
|
9
|
+
conditions?: string[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Create a new permission based on given attributes
|
|
13
|
+
*
|
|
14
|
+
* @param {object} attributes
|
|
15
|
+
*/
|
|
16
|
+
declare const create: <T>(object: T | null | undefined) => Pick<Permission, "subject" | "properties" | "conditions"> & Partial<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Add a condition to a permission
|
|
19
|
+
*/
|
|
20
|
+
declare const addCondition: import("lodash").CurriedFunction2<string, Permission, Permission>;
|
|
21
|
+
/**
|
|
22
|
+
* Gets a property or a part of a property from a permission.
|
|
23
|
+
*/
|
|
24
|
+
declare const getProperty: (...args: any[]) => any;
|
|
25
|
+
export { create, sanitizePermissionFields, addCondition, getProperty };
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/domain/permission/index.ts"],"names":[],"mappings":";AAAA,OAAO,CAAC,MAAM,WAAW,CAAC;AAI1B,QAAA,MAAM,wBAAwB,iBAA4B,CAAC;AAE3D,MAAM,WAAW,UAAU;IACzB,MAAM,EAAE,MAAM,CAAC;IACf,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC3C,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IACjC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACjC,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;CACvB;AAWD;;;;GAIG;AACH,QAAA,MAAM,MAAM,6GAAqE,CAAC;AAElF;;GAEG;AACH,QAAA,MAAM,YAAY,mEAQhB,CAAC;AAEH;;GAEG;AACH,QAAA,MAAM,WAAW,yBAKhB,CAAC;AAEF,OAAO,EAAE,MAAM,EAAE,wBAAwB,EAAE,YAAY,EAAE,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbilityBuilder, Ability } from '@casl/ability';
|
|
2
|
+
import type { ParametrizedAction, PermissionRule } from '../../types';
|
|
3
|
+
export interface CustomAbilityBuilder {
|
|
4
|
+
can(permission: PermissionRule): ReturnType<AbilityBuilder<Ability>['can']>;
|
|
5
|
+
buildParametrizedAction: (parametrizedAction: ParametrizedAction) => string;
|
|
6
|
+
build(): Ability;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Casl Ability Builder.
|
|
10
|
+
*/
|
|
11
|
+
export declare const caslAbilityBuilder: () => CustomAbilityBuilder;
|
|
12
|
+
//# sourceMappingURL=casl-ability.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"casl-ability.d.ts","sourceRoot":"","sources":["../../../src/engine/abilities/casl-ability.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAExD,OAAO,KAAK,EAAE,kBAAkB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAEtE,MAAM,WAAW,oBAAoB;IACnC,GAAG,CAAC,UAAU,EAAE,cAAc,GAAG,UAAU,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;IAC5E,uBAAuB,EAAE,CAAC,kBAAkB,EAAE,kBAAkB,KAAK,MAAM,CAAC;IAC5E,KAAK,IAAI,OAAO,CAAC;CAClB;AA2BD;;GAEG;AACH,eAAO,MAAM,kBAAkB,QAAO,oBAyCrC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/engine/abilities/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { hooks } from '@strapi/utils';
|
|
2
|
+
import * as domain from '../domain';
|
|
3
|
+
import type { Permission } from '../domain/permission';
|
|
4
|
+
import type { PermissionRule } from '../types';
|
|
5
|
+
export interface PermissionEngineHooks {
|
|
6
|
+
'before-format::validate.permission': ReturnType<typeof hooks.createAsyncBailHook>;
|
|
7
|
+
'format.permission': ReturnType<typeof hooks.createAsyncSeriesWaterfallHook>;
|
|
8
|
+
'after-format::validate.permission': ReturnType<typeof hooks.createAsyncBailHook>;
|
|
9
|
+
'before-evaluate.permission': ReturnType<typeof hooks.createAsyncSeriesHook>;
|
|
10
|
+
'before-register.permission': ReturnType<typeof hooks.createAsyncSeriesHook>;
|
|
11
|
+
}
|
|
12
|
+
export type HookName = keyof PermissionEngineHooks;
|
|
13
|
+
/**
|
|
14
|
+
* Create a hook map used by the permission Engine
|
|
15
|
+
*/
|
|
16
|
+
declare const createEngineHooks: () => PermissionEngineHooks;
|
|
17
|
+
/**
|
|
18
|
+
* Create a context from a domain {@link Permission} used by the validate hooks
|
|
19
|
+
*/
|
|
20
|
+
declare const createValidateContext: (permission: Permission) => {
|
|
21
|
+
readonly permission: Readonly<domain.permission.Permission>;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Create a context from a domain {@link Permission} used by the before valuate hook
|
|
25
|
+
*/
|
|
26
|
+
declare const createBeforeEvaluateContext: (permission: Permission) => {
|
|
27
|
+
readonly permission: Readonly<domain.permission.Permission>;
|
|
28
|
+
addCondition(condition: string): {
|
|
29
|
+
readonly permission: Readonly<domain.permission.Permission>;
|
|
30
|
+
addCondition(condition: string): any;
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
interface WillRegisterContextParams {
|
|
34
|
+
permission: PermissionRule;
|
|
35
|
+
options: Record<string, unknown>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Create a context from a casl Permission & some options
|
|
39
|
+
* @param caslPermission
|
|
40
|
+
*/
|
|
41
|
+
declare const createWillRegisterContext: ({ permission, options }: WillRegisterContextParams) => {
|
|
42
|
+
permission: PermissionRule;
|
|
43
|
+
condition: {
|
|
44
|
+
and(rawConditionObject: unknown): {
|
|
45
|
+
and(rawConditionObject: unknown): any;
|
|
46
|
+
or(rawConditionObject: unknown): any;
|
|
47
|
+
};
|
|
48
|
+
or(rawConditionObject: unknown): {
|
|
49
|
+
and(rawConditionObject: unknown): any;
|
|
50
|
+
or(rawConditionObject: unknown): any;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
export { createEngineHooks, createValidateContext, createBeforeEvaluateContext, createWillRegisterContext, };
|
|
55
|
+
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/engine/hooks.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC,OAAO,KAAK,MAAM,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C,MAAM,WAAW,qBAAqB;IACpC,oCAAoC,EAAE,UAAU,CAAC,OAAO,KAAK,CAAC,mBAAmB,CAAC,CAAC;IACnF,mBAAmB,EAAE,UAAU,CAAC,OAAO,KAAK,CAAC,8BAA8B,CAAC,CAAC;IAC7E,mCAAmC,EAAE,UAAU,CAAC,OAAO,KAAK,CAAC,mBAAmB,CAAC,CAAC;IAClF,4BAA4B,EAAE,UAAU,CAAC,OAAO,KAAK,CAAC,qBAAqB,CAAC,CAAC;IAC7E,4BAA4B,EAAE,UAAU,CAAC,OAAO,KAAK,CAAC,qBAAqB,CAAC,CAAC;CAC9E;AAED,MAAM,MAAM,QAAQ,GAAG,MAAM,qBAAqB,CAAC;AAEnD;;GAEG;AACH,QAAA,MAAM,iBAAiB,QAAO,qBAM5B,CAAC;AAEH;;GAEG;AACH,QAAA,MAAM,qBAAqB,eAAgB,UAAU;;CAInD,CAAC;AAEH;;GAEG;AACH,QAAA,MAAM,2BAA2B,eAAgB,UAAU;;4BAKjC,MAAM;;gCAAN,MAAM;;CAK9B,CAAC;AAEH,UAAU,yBAAyB;IACjC,UAAU,EAAE,cAAc,CAAC;IAC3B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AAED;;;GAGG;AACH,QAAA,MAAM,yBAAyB,4BAA6B,yBAAyB;;;gCAQzD,OAAO;oCAAP,OAAO;mCAYR,OAAO;;+BAAP,OAAO;oCAZN,OAAO;mCAYR,OAAO;;;CAkBhC,CAAC;AAEH,OAAO,EACL,iBAAiB,EACjB,qBAAqB,EACrB,2BAA2B,EAC3B,yBAAyB,GAC1B,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Ability } from '@casl/ability';
|
|
2
|
+
import { providerFactory } from '@strapi/utils';
|
|
3
|
+
import type { PermissionEngineHooks, HookName } from './hooks';
|
|
4
|
+
import * as abilities from './abilities';
|
|
5
|
+
import { Permission } from '../domain/permission';
|
|
6
|
+
import type { PermissionRule } from '../types';
|
|
7
|
+
export { abilities };
|
|
8
|
+
type Provider = Omit<ReturnType<typeof providerFactory>, 'register'> & {
|
|
9
|
+
register(...args: unknown[]): Promise<Provider> | Provider;
|
|
10
|
+
};
|
|
11
|
+
type ActionProvider = Provider;
|
|
12
|
+
type ConditionProvider = Provider;
|
|
13
|
+
export interface Engine {
|
|
14
|
+
hooks: PermissionEngineHooks;
|
|
15
|
+
on(hook: HookName, handler: (...args: any[]) => any): Engine;
|
|
16
|
+
generateAbility(permissions: Permission[], options?: object): Promise<Ability>;
|
|
17
|
+
createRegisterFunction(can: (permission: PermissionRule) => unknown, options: Record<string, unknown>): (permission: PermissionRule) => Promise<unknown>;
|
|
18
|
+
}
|
|
19
|
+
export interface EngineParams {
|
|
20
|
+
providers: {
|
|
21
|
+
action: ActionProvider;
|
|
22
|
+
condition: ConditionProvider;
|
|
23
|
+
};
|
|
24
|
+
abilityBuilderFactory?(): abilities.CustomAbilityBuilder;
|
|
25
|
+
}
|
|
26
|
+
declare const newEngine: (params: EngineParams) => Engine;
|
|
27
|
+
export { newEngine as new };
|
|
28
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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AE,cAAc,CAAC;QAAC,SAAS,EAAE,iBAAiB,CAAA;KAAE,CAAC;IACpE,qBAAqB,CAAC,IAAI,SAAS,CAAC,oBAAoB,CAAC;CAC1D;AAsBD,QAAA,MAAM,SAAS,WAAY,YAAY,KAAG,MAoKzC,CAAC;AAEF,OAAO,EAAE,SAAS,IAAI,GAAG,EAAE,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AAEnC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const _ = require("lodash/fp");
|
|
4
|
+
const qs = require("qs");
|
|
5
|
+
const utils = require("@strapi/utils");
|
|
6
|
+
const sift = require("sift");
|
|
7
|
+
const ability = require("@casl/ability");
|
|
8
|
+
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
|
|
9
|
+
function _interopNamespace(e) {
|
|
10
|
+
if (e && e.__esModule)
|
|
11
|
+
return e;
|
|
12
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
13
|
+
if (e) {
|
|
14
|
+
for (const k in e) {
|
|
15
|
+
if (k !== "default") {
|
|
16
|
+
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
17
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
get: () => e[k]
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
n.default = e;
|
|
25
|
+
return Object.freeze(n);
|
|
26
|
+
}
|
|
27
|
+
const ___default = /* @__PURE__ */ _interopDefault(_);
|
|
28
|
+
const qs__default = /* @__PURE__ */ _interopDefault(qs);
|
|
29
|
+
const sift__namespace = /* @__PURE__ */ _interopNamespace(sift);
|
|
30
|
+
const PERMISSION_FIELDS = ["action", "subject", "properties", "conditions"];
|
|
31
|
+
const sanitizePermissionFields = ___default.default.pick(PERMISSION_FIELDS);
|
|
32
|
+
const getDefaultPermission = () => ({
|
|
33
|
+
conditions: [],
|
|
34
|
+
properties: {},
|
|
35
|
+
subject: null
|
|
36
|
+
});
|
|
37
|
+
const create = ___default.default.pipe(___default.default.pick(PERMISSION_FIELDS), ___default.default.merge(getDefaultPermission()));
|
|
38
|
+
const addCondition = ___default.default.curry((condition, permission) => {
|
|
39
|
+
const { conditions } = permission;
|
|
40
|
+
const newConditions = Array.isArray(conditions) ? ___default.default.uniq(conditions.concat(condition)) : [condition];
|
|
41
|
+
return ___default.default.set("conditions", newConditions, permission);
|
|
42
|
+
});
|
|
43
|
+
const getProperty = ___default.default.curry(
|
|
44
|
+
(property, permission) => ___default.default.get(`properties.${property}`, permission)
|
|
45
|
+
);
|
|
46
|
+
const index$3 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
47
|
+
__proto__: null,
|
|
48
|
+
addCondition,
|
|
49
|
+
create,
|
|
50
|
+
getProperty,
|
|
51
|
+
sanitizePermissionFields
|
|
52
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
53
|
+
const index$2 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
54
|
+
__proto__: null,
|
|
55
|
+
permission: index$3
|
|
56
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
57
|
+
const createEngineHooks = () => ({
|
|
58
|
+
"before-format::validate.permission": utils.hooks.createAsyncBailHook(),
|
|
59
|
+
"format.permission": utils.hooks.createAsyncSeriesWaterfallHook(),
|
|
60
|
+
"after-format::validate.permission": utils.hooks.createAsyncBailHook(),
|
|
61
|
+
"before-evaluate.permission": utils.hooks.createAsyncSeriesHook(),
|
|
62
|
+
"before-register.permission": utils.hooks.createAsyncSeriesHook()
|
|
63
|
+
});
|
|
64
|
+
const createValidateContext = (permission) => ({
|
|
65
|
+
get permission() {
|
|
66
|
+
return _.cloneDeep(permission);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
const createBeforeEvaluateContext = (permission) => ({
|
|
70
|
+
get permission() {
|
|
71
|
+
return _.cloneDeep(permission);
|
|
72
|
+
},
|
|
73
|
+
addCondition(condition) {
|
|
74
|
+
Object.assign(permission, addCondition(condition, permission));
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
const createWillRegisterContext = ({ permission, options }) => ({
|
|
79
|
+
...options,
|
|
80
|
+
get permission() {
|
|
81
|
+
return _.cloneDeep(permission);
|
|
82
|
+
},
|
|
83
|
+
condition: {
|
|
84
|
+
and(rawConditionObject) {
|
|
85
|
+
if (!permission.condition) {
|
|
86
|
+
permission.condition = { $and: [] };
|
|
87
|
+
}
|
|
88
|
+
if (_.isArray(permission.condition.$and)) {
|
|
89
|
+
permission.condition.$and.push(rawConditionObject);
|
|
90
|
+
}
|
|
91
|
+
return this;
|
|
92
|
+
},
|
|
93
|
+
or(rawConditionObject) {
|
|
94
|
+
if (!permission.condition) {
|
|
95
|
+
permission.condition = { $and: [] };
|
|
96
|
+
}
|
|
97
|
+
if (_.isArray(permission.condition.$and)) {
|
|
98
|
+
const orClause = permission.condition.$and.find(_.has("$or"));
|
|
99
|
+
if (orClause) {
|
|
100
|
+
orClause.$or.push(rawConditionObject);
|
|
101
|
+
} else {
|
|
102
|
+
permission.condition.$and.push({ $or: [rawConditionObject] });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
const allowedOperations = [
|
|
110
|
+
"$or",
|
|
111
|
+
"$and",
|
|
112
|
+
"$eq",
|
|
113
|
+
"$ne",
|
|
114
|
+
"$in",
|
|
115
|
+
"$nin",
|
|
116
|
+
"$lt",
|
|
117
|
+
"$lte",
|
|
118
|
+
"$gt",
|
|
119
|
+
"$gte",
|
|
120
|
+
"$exists",
|
|
121
|
+
"$elemMatch"
|
|
122
|
+
];
|
|
123
|
+
const operations = _.pick(allowedOperations, sift__namespace);
|
|
124
|
+
const conditionsMatcher = (conditions) => {
|
|
125
|
+
return sift__namespace.createQueryTester(conditions, { operations });
|
|
126
|
+
};
|
|
127
|
+
const buildParametrizedAction = ({ name, params }) => {
|
|
128
|
+
return `${name}?${qs__default.default.stringify(params)}`;
|
|
129
|
+
};
|
|
130
|
+
const caslAbilityBuilder = () => {
|
|
131
|
+
const { can, build, ...rest } = new ability.AbilityBuilder(ability.Ability);
|
|
132
|
+
return {
|
|
133
|
+
can(permission) {
|
|
134
|
+
const { action, subject, properties = {}, condition } = permission;
|
|
135
|
+
const { fields } = properties;
|
|
136
|
+
const caslAction = typeof action === "string" ? action : buildParametrizedAction(action);
|
|
137
|
+
return can(
|
|
138
|
+
caslAction,
|
|
139
|
+
_.isNil(subject) ? "all" : subject,
|
|
140
|
+
fields,
|
|
141
|
+
_.isObject(condition) ? condition : void 0
|
|
142
|
+
);
|
|
143
|
+
},
|
|
144
|
+
buildParametrizedAction({ name, params }) {
|
|
145
|
+
return `${name}?${qs__default.default.stringify(params)}`;
|
|
146
|
+
},
|
|
147
|
+
build() {
|
|
148
|
+
const ability2 = build({ conditionsMatcher });
|
|
149
|
+
function decorateCan(originalCan) {
|
|
150
|
+
return function(...args) {
|
|
151
|
+
const [action, ...rest2] = args;
|
|
152
|
+
const caslAction = typeof action === "string" ? action : buildParametrizedAction(action);
|
|
153
|
+
return originalCan.apply(ability2, [caslAction, ...rest2]);
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
ability2.can = decorateCan(ability2.can);
|
|
157
|
+
return ability2;
|
|
158
|
+
},
|
|
159
|
+
...rest
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
163
|
+
__proto__: null,
|
|
164
|
+
caslAbilityBuilder
|
|
165
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
166
|
+
const createEngineState = () => {
|
|
167
|
+
const hooks = createEngineHooks();
|
|
168
|
+
return { hooks };
|
|
169
|
+
};
|
|
170
|
+
const newEngine = (params) => {
|
|
171
|
+
const { providers, abilityBuilderFactory = caslAbilityBuilder } = params;
|
|
172
|
+
const state = createEngineState();
|
|
173
|
+
const runValidationHook = async (hook, context) => state.hooks[hook].call(context);
|
|
174
|
+
const evaluate = async (params2) => {
|
|
175
|
+
const { options, register } = params2;
|
|
176
|
+
const preFormatValidation = await runValidationHook(
|
|
177
|
+
"before-format::validate.permission",
|
|
178
|
+
createBeforeEvaluateContext(params2.permission)
|
|
179
|
+
);
|
|
180
|
+
if (preFormatValidation === false) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const permission = await state.hooks["format.permission"].call(
|
|
184
|
+
params2.permission
|
|
185
|
+
);
|
|
186
|
+
const afterFormatValidation = await runValidationHook(
|
|
187
|
+
"after-format::validate.permission",
|
|
188
|
+
createValidateContext(permission)
|
|
189
|
+
);
|
|
190
|
+
if (afterFormatValidation === false) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
await state.hooks["before-evaluate.permission"].call(createBeforeEvaluateContext(permission));
|
|
194
|
+
const {
|
|
195
|
+
action: actionName,
|
|
196
|
+
subject,
|
|
197
|
+
properties,
|
|
198
|
+
conditions = [],
|
|
199
|
+
actionParameters = {}
|
|
200
|
+
} = permission;
|
|
201
|
+
let action = actionName;
|
|
202
|
+
if (actionParameters && Object.keys(actionParameters).length > 0) {
|
|
203
|
+
action = `${actionName}?${qs__default.default.stringify(actionParameters)}`;
|
|
204
|
+
}
|
|
205
|
+
if (conditions.length === 0) {
|
|
206
|
+
return register({ action, subject, properties });
|
|
207
|
+
}
|
|
208
|
+
const resolveConditions = ___default.default.map(providers.condition.get);
|
|
209
|
+
const removeInvalidConditions = ___default.default.filter(
|
|
210
|
+
(condition) => ___default.default.isFunction(condition.handler)
|
|
211
|
+
);
|
|
212
|
+
const evaluateConditions = (conditions2) => {
|
|
213
|
+
return Promise.all(
|
|
214
|
+
conditions2.map(async (condition) => ({
|
|
215
|
+
condition,
|
|
216
|
+
result: await condition.handler(
|
|
217
|
+
___default.default.merge(options, { permission: ___default.default.cloneDeep(permission) })
|
|
218
|
+
)
|
|
219
|
+
}))
|
|
220
|
+
);
|
|
221
|
+
};
|
|
222
|
+
const removeInvalidResults = ___default.default.filter(
|
|
223
|
+
({ result }) => ___default.default.isBoolean(result) || ___default.default.isObject(result)
|
|
224
|
+
);
|
|
225
|
+
const evaluatedConditions = await Promise.resolve(conditions).then(resolveConditions).then(removeInvalidConditions).then(evaluateConditions).then(removeInvalidResults);
|
|
226
|
+
const resultPropEq = ___default.default.propEq("result");
|
|
227
|
+
const pickResults = ___default.default.map(___default.default.prop("result"));
|
|
228
|
+
if (evaluatedConditions.every(resultPropEq(false))) {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
if (___default.default.isEmpty(evaluatedConditions) || evaluatedConditions.some(resultPropEq(true))) {
|
|
232
|
+
return register({ action, subject, properties });
|
|
233
|
+
}
|
|
234
|
+
const results = pickResults(evaluatedConditions).filter(___default.default.isObject);
|
|
235
|
+
if (___default.default.isEmpty(results)) {
|
|
236
|
+
return register({ action, subject, properties });
|
|
237
|
+
}
|
|
238
|
+
return register({
|
|
239
|
+
action,
|
|
240
|
+
subject,
|
|
241
|
+
properties,
|
|
242
|
+
condition: { $and: [{ $or: results }] }
|
|
243
|
+
});
|
|
244
|
+
};
|
|
245
|
+
return {
|
|
246
|
+
get hooks() {
|
|
247
|
+
return state.hooks;
|
|
248
|
+
},
|
|
249
|
+
/**
|
|
250
|
+
* Create a register function that wraps a `can` function
|
|
251
|
+
* used to register a permission in the ability builder
|
|
252
|
+
*/
|
|
253
|
+
createRegisterFunction(can, options) {
|
|
254
|
+
return async (permission) => {
|
|
255
|
+
const hookContext = createWillRegisterContext({ options, permission });
|
|
256
|
+
await state.hooks["before-register.permission"].call(hookContext);
|
|
257
|
+
return can(permission);
|
|
258
|
+
};
|
|
259
|
+
},
|
|
260
|
+
/**
|
|
261
|
+
* Register a new handler for a given hook
|
|
262
|
+
*/
|
|
263
|
+
on(hook, handler) {
|
|
264
|
+
const validHooks = Object.keys(state.hooks);
|
|
265
|
+
const isValidHook = validHooks.includes(hook);
|
|
266
|
+
if (!isValidHook) {
|
|
267
|
+
throw new Error(
|
|
268
|
+
`Invalid hook supplied when trying to register an handler to the permission engine. Got "${hook}" but expected one of ${validHooks.join(
|
|
269
|
+
", "
|
|
270
|
+
)}`
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
state.hooks[hook].register(handler);
|
|
274
|
+
return this;
|
|
275
|
+
},
|
|
276
|
+
/**
|
|
277
|
+
* Generate an ability based on the instance's
|
|
278
|
+
* ability builder and the given permissions
|
|
279
|
+
*/
|
|
280
|
+
async generateAbility(permissions, options = {}) {
|
|
281
|
+
const { can, build } = abilityBuilderFactory();
|
|
282
|
+
for (const permission of permissions) {
|
|
283
|
+
const register = this.createRegisterFunction(can, options);
|
|
284
|
+
await evaluate({ permission, options, register });
|
|
285
|
+
}
|
|
286
|
+
return build();
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
};
|
|
290
|
+
const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
291
|
+
__proto__: null,
|
|
292
|
+
abilities: index$1,
|
|
293
|
+
new: newEngine
|
|
294
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
295
|
+
exports.domain = index$2;
|
|
296
|
+
exports.engine = index;
|
|
297
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/domain/permission/index.ts","../src/engine/hooks.ts","../src/engine/abilities/casl-ability.ts","../src/engine/index.ts"],"sourcesContent":["import _ from 'lodash/fp';\n\nconst PERMISSION_FIELDS = ['action', 'subject', 'properties', 'conditions'] as const;\n\nconst sanitizePermissionFields = _.pick(PERMISSION_FIELDS);\n\nexport interface Permission {\n action: string;\n actionParameters?: Record<string, unknown>;\n subject?: string | object | null;\n properties?: Record<string, any>;\n conditions?: string[];\n}\n\n/**\n * Creates a permission with default values for optional properties\n */\nconst getDefaultPermission = (): Pick<Permission, 'conditions' | 'properties' | 'subject'> => ({\n conditions: [],\n properties: {},\n subject: null,\n});\n\n/**\n * Create a new permission based on given attributes\n *\n * @param {object} attributes\n */\nconst create = _.pipe(_.pick(PERMISSION_FIELDS), _.merge(getDefaultPermission()));\n\n/**\n * Add a condition to a permission\n */\nconst addCondition = _.curry((condition: string, permission: Permission): Permission => {\n const { conditions } = permission;\n\n const newConditions = Array.isArray(conditions)\n ? _.uniq(conditions.concat(condition))\n : [condition];\n\n return _.set('conditions', newConditions, permission);\n});\n\n/**\n * Gets a property or a part of a property from a permission.\n */\nconst getProperty = _.curry(\n <T extends keyof Permission['properties']>(\n property: T,\n permission: Permission\n ): Permission['properties'][T] => _.get(`properties.${property}`, permission)\n);\n\nexport { create, sanitizePermissionFields, addCondition, getProperty };\n","import { cloneDeep, has, isArray } from 'lodash/fp';\nimport { hooks } from '@strapi/utils';\n\nimport * as domain from '../domain';\nimport type { Permission } from '../domain/permission';\nimport type { PermissionRule } from '../types';\n\nexport interface PermissionEngineHooks {\n 'before-format::validate.permission': ReturnType<typeof hooks.createAsyncBailHook>;\n 'format.permission': ReturnType<typeof hooks.createAsyncSeriesWaterfallHook>;\n 'after-format::validate.permission': ReturnType<typeof hooks.createAsyncBailHook>;\n 'before-evaluate.permission': ReturnType<typeof hooks.createAsyncSeriesHook>;\n 'before-register.permission': ReturnType<typeof hooks.createAsyncSeriesHook>;\n}\n\nexport type HookName = keyof PermissionEngineHooks;\n\n/**\n * Create a hook map used by the permission Engine\n */\nconst createEngineHooks = (): PermissionEngineHooks => ({\n 'before-format::validate.permission': hooks.createAsyncBailHook(),\n 'format.permission': hooks.createAsyncSeriesWaterfallHook(),\n 'after-format::validate.permission': hooks.createAsyncBailHook(),\n 'before-evaluate.permission': hooks.createAsyncSeriesHook(),\n 'before-register.permission': hooks.createAsyncSeriesHook(),\n});\n\n/**\n * Create a context from a domain {@link Permission} used by the validate hooks\n */\nconst createValidateContext = (permission: Permission) => ({\n get permission(): Readonly<Permission> {\n return cloneDeep(permission);\n },\n});\n\n/**\n * Create a context from a domain {@link Permission} used by the before valuate hook\n */\nconst createBeforeEvaluateContext = (permission: Permission) => ({\n get permission(): Readonly<Permission> {\n return cloneDeep(permission);\n },\n\n addCondition(condition: string) {\n Object.assign(permission, domain.permission.addCondition(condition, permission));\n\n return this;\n },\n});\n\ninterface WillRegisterContextParams {\n permission: PermissionRule;\n options: Record<string, unknown>;\n}\n\n/**\n * Create a context from a casl Permission & some options\n * @param caslPermission\n */\nconst createWillRegisterContext = ({ permission, options }: WillRegisterContextParams) => ({\n ...options,\n\n get permission() {\n return cloneDeep(permission);\n },\n\n condition: {\n and(rawConditionObject: unknown) {\n if (!permission.condition) {\n permission.condition = { $and: [] };\n }\n\n if (isArray(permission.condition.$and)) {\n permission.condition.$and.push(rawConditionObject);\n }\n\n return this;\n },\n\n or(rawConditionObject: unknown) {\n if (!permission.condition) {\n permission.condition = { $and: [] };\n }\n\n if (isArray(permission.condition.$and)) {\n const orClause = permission.condition.$and.find(has('$or'));\n\n if (orClause) {\n orClause.$or.push(rawConditionObject);\n } else {\n permission.condition.$and.push({ $or: [rawConditionObject] });\n }\n }\n\n return this;\n },\n },\n});\n\nexport {\n createEngineHooks,\n createValidateContext,\n createBeforeEvaluateContext,\n createWillRegisterContext,\n};\n","import * as sift from 'sift';\nimport qs from 'qs';\nimport { AbilityBuilder, Ability } from '@casl/ability';\nimport { pick, isNil, isObject } from 'lodash/fp';\nimport type { ParametrizedAction, PermissionRule } from '../../types';\n\nexport interface CustomAbilityBuilder {\n can(permission: PermissionRule): ReturnType<AbilityBuilder<Ability>['can']>;\n buildParametrizedAction: (parametrizedAction: ParametrizedAction) => string;\n build(): Ability;\n}\n\nconst allowedOperations = [\n '$or',\n '$and',\n '$eq',\n '$ne',\n '$in',\n '$nin',\n '$lt',\n '$lte',\n '$gt',\n '$gte',\n '$exists',\n '$elemMatch',\n] as const;\n\nconst operations = pick(allowedOperations, sift);\n\nconst conditionsMatcher = (conditions: unknown) => {\n return sift.createQueryTester(conditions, { operations });\n};\n\nconst buildParametrizedAction = ({ name, params }: ParametrizedAction) => {\n return `${name}?${qs.stringify(params)}`;\n};\n\n/**\n * Casl Ability Builder.\n */\nexport const caslAbilityBuilder = (): CustomAbilityBuilder => {\n const { can, build, ...rest } = new AbilityBuilder(Ability);\n\n return {\n can(permission: PermissionRule) {\n const { action, subject, properties = {}, condition } = permission;\n const { fields } = properties;\n\n const caslAction = typeof action === 'string' ? action : buildParametrizedAction(action);\n\n return can(\n caslAction,\n isNil(subject) ? 'all' : subject,\n fields,\n isObject(condition) ? condition : undefined\n );\n },\n\n buildParametrizedAction({ name, params }: ParametrizedAction) {\n return `${name}?${qs.stringify(params)}`;\n },\n\n build() {\n const ability = build({ conditionsMatcher });\n\n function decorateCan(originalCan: Ability['can']) {\n return function (...args: Parameters<Ability['can']>) {\n const [action, ...rest] = args;\n const caslAction = typeof action === 'string' ? action : buildParametrizedAction(action);\n\n // Call the original `can` method\n return originalCan.apply(ability, [caslAction, ...rest]);\n };\n }\n\n ability.can = decorateCan(ability.can);\n return ability;\n },\n\n ...rest,\n };\n};\n","import _ from 'lodash/fp';\nimport qs from 'qs';\nimport { Ability } from '@casl/ability';\nimport { providerFactory } from '@strapi/utils';\n\nimport {\n createEngineHooks,\n createWillRegisterContext,\n createBeforeEvaluateContext,\n createValidateContext,\n} from './hooks';\nimport type { PermissionEngineHooks, HookName } from './hooks';\n\nimport * as abilities from './abilities';\nimport { Permission } from '../domain/permission';\nimport type { PermissionRule } from '../types';\n\nexport { abilities };\n\ntype Provider = Omit<ReturnType<typeof providerFactory>, 'register'> & {\n register(...args: unknown[]): Promise<Provider> | Provider;\n};\n\ntype ActionProvider = Provider;\ntype ConditionProvider = Provider;\n\nexport interface Engine {\n hooks: PermissionEngineHooks;\n on(hook: HookName, handler: (...args: any[]) => any): Engine;\n generateAbility(permissions: Permission[], options?: object): Promise<Ability>;\n createRegisterFunction(\n can: (permission: PermissionRule) => unknown,\n options: Record<string, unknown>\n ): (permission: PermissionRule) => Promise<unknown>;\n}\n\nexport interface EngineParams {\n providers: { action: ActionProvider; condition: ConditionProvider };\n abilityBuilderFactory?(): abilities.CustomAbilityBuilder;\n}\n\ninterface EvaluateParams {\n options: Record<string, unknown>;\n register: (permission: PermissionRule) => Promise<unknown>;\n permission: Permission;\n}\n\ninterface Condition {\n name: string;\n handler(...params: unknown[]): boolean | object;\n}\n\n/**\n * Create a default state object for the engine\n */\nconst createEngineState = () => {\n const hooks = createEngineHooks();\n\n return { hooks };\n};\n\nconst newEngine = (params: EngineParams): Engine => {\n const { providers, abilityBuilderFactory = abilities.caslAbilityBuilder } = params;\n\n const state = createEngineState();\n\n const runValidationHook = async (hook: HookName, context: unknown) =>\n state.hooks[hook].call(context);\n\n /**\n * Evaluate a permission using local and registered behaviors (using hooks).\n * Validate, format (add condition, etc...), evaluate (evaluate conditions) and register a permission\n */\n const evaluate = async (params: EvaluateParams) => {\n const { options, register } = params;\n\n const preFormatValidation = await runValidationHook(\n 'before-format::validate.permission',\n createBeforeEvaluateContext(params.permission)\n );\n\n if (preFormatValidation === false) {\n return;\n }\n\n const permission = (await state.hooks['format.permission'].call(\n params.permission\n )) as Permission;\n\n const afterFormatValidation = await runValidationHook(\n 'after-format::validate.permission',\n createValidateContext(permission)\n );\n\n if (afterFormatValidation === false) {\n return;\n }\n\n await state.hooks['before-evaluate.permission'].call(createBeforeEvaluateContext(permission));\n\n const {\n action: actionName,\n subject,\n properties,\n conditions = [],\n actionParameters = {},\n } = permission;\n\n let action = actionName;\n\n if (actionParameters && Object.keys(actionParameters).length > 0) {\n action = `${actionName}?${qs.stringify(actionParameters)}`;\n }\n\n if (conditions.length === 0) {\n return register({ action, subject, properties });\n }\n\n const resolveConditions = _.map(providers.condition.get);\n\n const removeInvalidConditions = _.filter((condition: Condition) =>\n _.isFunction(condition.handler)\n );\n\n const evaluateConditions = (conditions: Condition[]) => {\n return Promise.all(\n conditions.map(async (condition) => ({\n condition,\n result: await condition.handler(\n _.merge(options, { permission: _.cloneDeep(permission) })\n ),\n }))\n );\n };\n\n const removeInvalidResults = _.filter(\n ({ result }) => _.isBoolean(result) || _.isObject(result)\n );\n\n const evaluatedConditions = await Promise.resolve(conditions)\n .then(resolveConditions)\n .then(removeInvalidConditions)\n .then(evaluateConditions)\n .then(removeInvalidResults);\n\n const resultPropEq = _.propEq('result');\n const pickResults = _.map(_.prop('result'));\n\n if (evaluatedConditions.every(resultPropEq(false))) {\n return;\n }\n\n if (_.isEmpty(evaluatedConditions) || evaluatedConditions.some(resultPropEq(true))) {\n return register({ action, subject, properties });\n }\n\n const results = pickResults(evaluatedConditions).filter(_.isObject);\n\n if (_.isEmpty(results)) {\n return register({ action, subject, properties });\n }\n\n return register({\n action,\n subject,\n properties,\n condition: { $and: [{ $or: results }] },\n });\n };\n\n return {\n get hooks() {\n return state.hooks;\n },\n\n /**\n * Create a register function that wraps a `can` function\n * used to register a permission in the ability builder\n */\n createRegisterFunction(can, options: Record<string, unknown>) {\n return async (permission: PermissionRule) => {\n const hookContext = createWillRegisterContext({ options, permission });\n\n await state.hooks['before-register.permission'].call(hookContext);\n\n return can(permission);\n };\n },\n\n /**\n * Register a new handler for a given hook\n */\n on(hook, handler) {\n const validHooks = Object.keys(state.hooks);\n const isValidHook = validHooks.includes(hook);\n\n if (!isValidHook) {\n throw new Error(\n `Invalid hook supplied when trying to register an handler to the permission engine. Got \"${hook}\" but expected one of ${validHooks.join(\n ', '\n )}`\n );\n }\n\n state.hooks[hook].register(handler);\n\n return this;\n },\n\n /**\n * Generate an ability based on the instance's\n * ability builder and the given permissions\n */\n async generateAbility(permissions, options: Record<string, unknown> = {}) {\n const { can, build } = abilityBuilderFactory();\n\n for (const permission of permissions) {\n const register = this.createRegisterFunction(can, options);\n\n await evaluate({ permission, options, register });\n }\n\n return build();\n },\n };\n};\n\nexport { newEngine as new };\n"],"names":["_","hooks","cloneDeep","domain.permission.addCondition","isArray","has","pick","sift","qs","AbilityBuilder","Ability","isNil","isObject","ability","rest","abilities.caslAbilityBuilder","params","conditions"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,MAAM,oBAAoB,CAAC,UAAU,WAAW,cAAc,YAAY;AAE1E,MAAM,2BAA2BA,WAAE,QAAA,KAAK,iBAAiB;AAazD,MAAM,uBAAuB,OAAkE;AAAA,EAC7F,YAAY,CAAC;AAAA,EACb,YAAY,CAAC;AAAA,EACb,SAAS;AACX;AAOA,MAAM,SAASA,WAAE,QAAA,KAAKA,WAAAA,QAAE,KAAK,iBAAiB,GAAGA,mBAAE,MAAM,qBAAqB,CAAC,CAAC;AAKhF,MAAM,eAAeA,WAAAA,QAAE,MAAM,CAAC,WAAmB,eAAuC;AAChF,QAAA,EAAE,WAAe,IAAA;AAEvB,QAAM,gBAAgB,MAAM,QAAQ,UAAU,IAC1CA,WAAA,QAAE,KAAK,WAAW,OAAO,SAAS,CAAC,IACnC,CAAC,SAAS;AAEd,SAAOA,WAAE,QAAA,IAAI,cAAc,eAAe,UAAU;AACtD,CAAC;AAKD,MAAM,cAAcA,WAAE,QAAA;AAAA,EACpB,CACE,UACA,eACgCA,mBAAE,IAAI,cAAc,QAAQ,IAAI,UAAU;AAC9E;;;;;;;;;;;;AC/BA,MAAM,oBAAoB,OAA8B;AAAA,EACtD,sCAAsCC,YAAM,oBAAoB;AAAA,EAChE,qBAAqBA,YAAM,+BAA+B;AAAA,EAC1D,qCAAqCA,YAAM,oBAAoB;AAAA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