@strapi/permissions 0.0.0-next.d999ad0b00ff427885462051cdc96626b7946472 → 0.0.0-next.da19c0501ff87d14fb664b55b8e0630d3c548485
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.js +8 -0
- package/dist/domain/index.js.map +1 -0
- package/dist/domain/index.mjs +3 -0
- package/dist/domain/index.mjs.map +1 -0
- package/dist/domain/permission/index.d.ts +1 -1
- package/dist/domain/permission/index.d.ts.map +1 -1
- package/dist/domain/permission/index.js +41 -0
- package/dist/domain/permission/index.js.map +1 -0
- package/dist/domain/permission/index.mjs +36 -0
- package/dist/domain/permission/index.mjs.map +1 -0
- package/dist/engine/abilities/casl-ability.d.ts +2 -13
- package/dist/engine/abilities/casl-ability.d.ts.map +1 -1
- package/dist/engine/abilities/casl-ability.js +87 -0
- package/dist/engine/abilities/casl-ability.js.map +1 -0
- package/dist/engine/abilities/casl-ability.mjs +66 -0
- package/dist/engine/abilities/casl-ability.mjs.map +1 -0
- package/dist/engine/abilities/index.js +8 -0
- package/dist/engine/abilities/index.js.map +1 -0
- package/dist/engine/abilities/index.mjs +2 -0
- package/dist/engine/abilities/index.mjs.map +1 -0
- package/dist/engine/hooks.d.ts +1 -1
- package/dist/engine/hooks.d.ts.map +1 -1
- package/dist/engine/hooks.js +81 -0
- package/dist/engine/hooks.js.map +1 -0
- package/dist/engine/hooks.mjs +76 -0
- package/dist/engine/hooks.mjs.map +1 -0
- package/dist/engine/index.d.ts +2 -1
- package/dist/engine/index.d.ts.map +1 -1
- package/dist/engine/index.js +141 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/engine/index.mjs +139 -0
- package/dist/engine/index.mjs.map +1 -0
- package/dist/index.js +9 -296
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -274
- package/dist/index.mjs.map +1 -1
- package/dist/types.d.ts +19 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +11 -12
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash/fp');
|
|
4
|
+
var qs = require('qs');
|
|
5
|
+
var hooks = require('./hooks.js');
|
|
6
|
+
var index = require('./abilities/index.js');
|
|
7
|
+
var caslAbility = require('./abilities/casl-ability.js');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Create a default state object for the engine
|
|
11
|
+
*/ const createEngineState = ()=>{
|
|
12
|
+
const hooks$1 = hooks.createEngineHooks();
|
|
13
|
+
return {
|
|
14
|
+
hooks: hooks$1
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
const newEngine = (params)=>{
|
|
18
|
+
const { providers, abilityBuilderFactory = caslAbility.caslAbilityBuilder } = params;
|
|
19
|
+
const state = createEngineState();
|
|
20
|
+
const runValidationHook = async (hook, context)=>state.hooks[hook].call(context);
|
|
21
|
+
/**
|
|
22
|
+
* Evaluate a permission using local and registered behaviors (using hooks).
|
|
23
|
+
* Validate, format (add condition, etc...), evaluate (evaluate conditions) and register a permission
|
|
24
|
+
*/ const evaluate = async (params)=>{
|
|
25
|
+
const { options, register } = params;
|
|
26
|
+
const preFormatValidation = await runValidationHook('before-format::validate.permission', hooks.createBeforeEvaluateContext(params.permission));
|
|
27
|
+
if (preFormatValidation === false) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const permission = await state.hooks['format.permission'].call(params.permission);
|
|
31
|
+
const afterFormatValidation = await runValidationHook('after-format::validate.permission', hooks.createValidateContext(permission));
|
|
32
|
+
if (afterFormatValidation === false) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
await state.hooks['before-evaluate.permission'].call(hooks.createBeforeEvaluateContext(permission));
|
|
36
|
+
const { action: actionName, subject, properties, conditions = [], actionParameters = {} } = permission;
|
|
37
|
+
let action = actionName;
|
|
38
|
+
if (actionParameters && Object.keys(actionParameters).length > 0) {
|
|
39
|
+
action = `${actionName}?${qs.stringify(actionParameters)}`;
|
|
40
|
+
}
|
|
41
|
+
if (conditions.length === 0) {
|
|
42
|
+
return register({
|
|
43
|
+
action,
|
|
44
|
+
subject,
|
|
45
|
+
properties
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
const resolveConditions = _.map(providers.condition.get);
|
|
49
|
+
const removeInvalidConditions = _.filter((condition)=>_.isFunction(condition.handler));
|
|
50
|
+
const evaluateConditions = (conditions)=>{
|
|
51
|
+
return Promise.all(conditions.map(async (condition)=>({
|
|
52
|
+
condition,
|
|
53
|
+
result: await condition.handler(_.merge(options, {
|
|
54
|
+
permission: _.cloneDeep(permission)
|
|
55
|
+
}))
|
|
56
|
+
})));
|
|
57
|
+
};
|
|
58
|
+
const removeInvalidResults = _.filter(({ result })=>_.isBoolean(result) || _.isObject(result));
|
|
59
|
+
const evaluatedConditions = await Promise.resolve(conditions).then(resolveConditions).then(removeInvalidConditions).then(evaluateConditions).then(removeInvalidResults);
|
|
60
|
+
const resultPropEq = _.propEq('result');
|
|
61
|
+
const pickResults = _.map(_.prop('result'));
|
|
62
|
+
if (evaluatedConditions.every(resultPropEq(false))) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (_.isEmpty(evaluatedConditions) || evaluatedConditions.some(resultPropEq(true))) {
|
|
66
|
+
return register({
|
|
67
|
+
action,
|
|
68
|
+
subject,
|
|
69
|
+
properties
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const results = pickResults(evaluatedConditions).filter(_.isObject);
|
|
73
|
+
if (_.isEmpty(results)) {
|
|
74
|
+
return register({
|
|
75
|
+
action,
|
|
76
|
+
subject,
|
|
77
|
+
properties
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return register({
|
|
81
|
+
action,
|
|
82
|
+
subject,
|
|
83
|
+
properties,
|
|
84
|
+
condition: {
|
|
85
|
+
$and: [
|
|
86
|
+
{
|
|
87
|
+
$or: results
|
|
88
|
+
}
|
|
89
|
+
]
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
return {
|
|
94
|
+
get hooks () {
|
|
95
|
+
return state.hooks;
|
|
96
|
+
},
|
|
97
|
+
/**
|
|
98
|
+
* Create a register function that wraps a `can` function
|
|
99
|
+
* used to register a permission in the ability builder
|
|
100
|
+
*/ createRegisterFunction (can, options) {
|
|
101
|
+
return async (permission)=>{
|
|
102
|
+
const hookContext = hooks.createWillRegisterContext({
|
|
103
|
+
options,
|
|
104
|
+
permission
|
|
105
|
+
});
|
|
106
|
+
await state.hooks['before-register.permission'].call(hookContext);
|
|
107
|
+
return can(permission);
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
/**
|
|
111
|
+
* Register a new handler for a given hook
|
|
112
|
+
*/ on (hook, handler) {
|
|
113
|
+
const validHooks = Object.keys(state.hooks);
|
|
114
|
+
const isValidHook = validHooks.includes(hook);
|
|
115
|
+
if (!isValidHook) {
|
|
116
|
+
throw new Error(`Invalid hook supplied when trying to register an handler to the permission engine. Got "${hook}" but expected one of ${validHooks.join(', ')}`);
|
|
117
|
+
}
|
|
118
|
+
state.hooks[hook].register(handler);
|
|
119
|
+
return this;
|
|
120
|
+
},
|
|
121
|
+
/**
|
|
122
|
+
* Generate an ability based on the instance's
|
|
123
|
+
* ability builder and the given permissions
|
|
124
|
+
*/ async generateAbility (permissions, options = {}) {
|
|
125
|
+
const { can, build } = abilityBuilderFactory();
|
|
126
|
+
for (const permission of permissions){
|
|
127
|
+
const register = this.createRegisterFunction(can, options);
|
|
128
|
+
await evaluate({
|
|
129
|
+
permission,
|
|
130
|
+
options,
|
|
131
|
+
register
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return build();
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
exports.abilities = index;
|
|
140
|
+
exports.new = newEngine;
|
|
141
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/engine/index.ts"],"sourcesContent":["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":["createEngineState","hooks","createEngineHooks","newEngine","params","providers","abilityBuilderFactory","abilities","state","runValidationHook","hook","context","call","evaluate","options","register","preFormatValidation","createBeforeEvaluateContext","permission","afterFormatValidation","createValidateContext","action","actionName","subject","properties","conditions","actionParameters","Object","keys","length","qs","stringify","resolveConditions","_","map","condition","get","removeInvalidConditions","filter","isFunction","handler","evaluateConditions","Promise","all","result","merge","cloneDeep","removeInvalidResults","isBoolean","isObject","evaluatedConditions","resolve","then","resultPropEq","propEq","pickResults","prop","every","isEmpty","some","results","$and","$or","createRegisterFunction","can","hookContext","createWillRegisterContext","on","validHooks","isValidHook","includes","Error","join","generateAbility","permissions","build"],"mappings":";;;;;;;;AAoDA;;AAEC,IACD,MAAMA,iBAAoB,GAAA,IAAA;AACxB,IAAA,MAAMC,OAAQC,GAAAA,uBAAAA,EAAAA;IAEd,OAAO;AAAED,eAAAA;AAAM,KAAA;AACjB,CAAA;AAEA,MAAME,YAAY,CAACC,MAAAA,GAAAA;AACjB,IAAA,MAAM,EAAEC,SAAS,EAAEC,wBAAwBC,8BAA4B,EAAE,GAAGH,MAAAA;AAE5E,IAAA,MAAMI,KAAQR,GAA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'lodash/fp';
|
|
2
|
+
import qs from 'qs';
|
|
3
|
+
import { createWillRegisterContext, createEngineHooks, createBeforeEvaluateContext, createValidateContext } from './hooks.mjs';
|
|
4
|
+
import * as index from './abilities/index.mjs';
|
|
5
|
+
export { index as abilities };
|
|
6
|
+
import { caslAbilityBuilder } from './abilities/casl-ability.mjs';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Create a default state object for the engine
|
|
10
|
+
*/ const createEngineState = ()=>{
|
|
11
|
+
const hooks = createEngineHooks();
|
|
12
|
+
return {
|
|
13
|
+
hooks
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
const newEngine = (params)=>{
|
|
17
|
+
const { providers, abilityBuilderFactory = caslAbilityBuilder } = params;
|
|
18
|
+
const state = createEngineState();
|
|
19
|
+
const runValidationHook = async (hook, context)=>state.hooks[hook].call(context);
|
|
20
|
+
/**
|
|
21
|
+
* Evaluate a permission using local and registered behaviors (using hooks).
|
|
22
|
+
* Validate, format (add condition, etc...), evaluate (evaluate conditions) and register a permission
|
|
23
|
+
*/ const evaluate = async (params)=>{
|
|
24
|
+
const { options, register } = params;
|
|
25
|
+
const preFormatValidation = await runValidationHook('before-format::validate.permission', createBeforeEvaluateContext(params.permission));
|
|
26
|
+
if (preFormatValidation === false) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const permission = await state.hooks['format.permission'].call(params.permission);
|
|
30
|
+
const afterFormatValidation = await runValidationHook('after-format::validate.permission', createValidateContext(permission));
|
|
31
|
+
if (afterFormatValidation === false) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
await state.hooks['before-evaluate.permission'].call(createBeforeEvaluateContext(permission));
|
|
35
|
+
const { action: actionName, subject, properties, conditions = [], actionParameters = {} } = permission;
|
|
36
|
+
let action = actionName;
|
|
37
|
+
if (actionParameters && Object.keys(actionParameters).length > 0) {
|
|
38
|
+
action = `${actionName}?${qs.stringify(actionParameters)}`;
|
|
39
|
+
}
|
|
40
|
+
if (conditions.length === 0) {
|
|
41
|
+
return register({
|
|
42
|
+
action,
|
|
43
|
+
subject,
|
|
44
|
+
properties
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const resolveConditions = _.map(providers.condition.get);
|
|
48
|
+
const removeInvalidConditions = _.filter((condition)=>_.isFunction(condition.handler));
|
|
49
|
+
const evaluateConditions = (conditions)=>{
|
|
50
|
+
return Promise.all(conditions.map(async (condition)=>({
|
|
51
|
+
condition,
|
|
52
|
+
result: await condition.handler(_.merge(options, {
|
|
53
|
+
permission: _.cloneDeep(permission)
|
|
54
|
+
}))
|
|
55
|
+
})));
|
|
56
|
+
};
|
|
57
|
+
const removeInvalidResults = _.filter(({ result })=>_.isBoolean(result) || _.isObject(result));
|
|
58
|
+
const evaluatedConditions = await Promise.resolve(conditions).then(resolveConditions).then(removeInvalidConditions).then(evaluateConditions).then(removeInvalidResults);
|
|
59
|
+
const resultPropEq = _.propEq('result');
|
|
60
|
+
const pickResults = _.map(_.prop('result'));
|
|
61
|
+
if (evaluatedConditions.every(resultPropEq(false))) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
if (_.isEmpty(evaluatedConditions) || evaluatedConditions.some(resultPropEq(true))) {
|
|
65
|
+
return register({
|
|
66
|
+
action,
|
|
67
|
+
subject,
|
|
68
|
+
properties
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const results = pickResults(evaluatedConditions).filter(_.isObject);
|
|
72
|
+
if (_.isEmpty(results)) {
|
|
73
|
+
return register({
|
|
74
|
+
action,
|
|
75
|
+
subject,
|
|
76
|
+
properties
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return register({
|
|
80
|
+
action,
|
|
81
|
+
subject,
|
|
82
|
+
properties,
|
|
83
|
+
condition: {
|
|
84
|
+
$and: [
|
|
85
|
+
{
|
|
86
|
+
$or: results
|
|
87
|
+
}
|
|
88
|
+
]
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
return {
|
|
93
|
+
get hooks () {
|
|
94
|
+
return state.hooks;
|
|
95
|
+
},
|
|
96
|
+
/**
|
|
97
|
+
* Create a register function that wraps a `can` function
|
|
98
|
+
* used to register a permission in the ability builder
|
|
99
|
+
*/ createRegisterFunction (can, options) {
|
|
100
|
+
return async (permission)=>{
|
|
101
|
+
const hookContext = createWillRegisterContext({
|
|
102
|
+
options,
|
|
103
|
+
permission
|
|
104
|
+
});
|
|
105
|
+
await state.hooks['before-register.permission'].call(hookContext);
|
|
106
|
+
return can(permission);
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
/**
|
|
110
|
+
* Register a new handler for a given hook
|
|
111
|
+
*/ on (hook, handler) {
|
|
112
|
+
const validHooks = Object.keys(state.hooks);
|
|
113
|
+
const isValidHook = validHooks.includes(hook);
|
|
114
|
+
if (!isValidHook) {
|
|
115
|
+
throw new Error(`Invalid hook supplied when trying to register an handler to the permission engine. Got "${hook}" but expected one of ${validHooks.join(', ')}`);
|
|
116
|
+
}
|
|
117
|
+
state.hooks[hook].register(handler);
|
|
118
|
+
return this;
|
|
119
|
+
},
|
|
120
|
+
/**
|
|
121
|
+
* Generate an ability based on the instance's
|
|
122
|
+
* ability builder and the given permissions
|
|
123
|
+
*/ async generateAbility (permissions, options = {}) {
|
|
124
|
+
const { can, build } = abilityBuilderFactory();
|
|
125
|
+
for (const permission of permissions){
|
|
126
|
+
const register = this.createRegisterFunction(can, options);
|
|
127
|
+
await evaluate({
|
|
128
|
+
permission,
|
|
129
|
+
options,
|
|
130
|
+
register
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
return build();
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export { newEngine as new };
|
|
139
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../src/engine/index.ts"],"sourcesContent":["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":["createEngineState","hooks","createEngineHooks","newEngine","params","providers","abilityBuilderFactory","abilities","state","runValidationHook","hook","context","call","evaluate","options","register","preFormatValidation","createBeforeEvaluateContext","permission","afterFormatValidation","createValidateContext","action","actionName","subject","properties","conditions","actionParameters","Object","keys","length","qs","stringify","resolveConditions","_","map","condition","get","removeInvalidConditions","filter","isFunction","handler","evaluateConditions","Promise","all","result","merge","cloneDeep","removeInvalidResults","isBoolean","isObject","evaluatedConditions","resolve","then","resultPropEq","propEq","pickResults","prop","every","isEmpty","some","results","$and","$or","createRegisterFunction","can","hookContext","createWillRegisterContext","on","validHooks","isValidHook","includes","Error","join","generateAbility","permissions","build"],"mappings":";;;;;;;AAoDA;;AAEC,IACD,MAAMA,iBAAoB,GAAA,IAAA;AACxB,IAAA,MAAMC,KAAQC,GAAAA,iBAAAA,EAAAA;IAEd,OAAO;AAAED,QAAAA;AAAM,KAAA;AACjB,CAAA;AAEA,MAAME,YAAY,CAACC,MAAAA,GAAAA;AACjB,IAAA,MAAM,EAAEC,SAAS,EAAEC,wBAAwBC,kBAA4B,EAAE,GAAGH,MAAAA;AAE5E,IAAA,MAAMI,KAAQR,GAAAA,iBAAAA,EAAAA;IAEd,MAAMS,iBAAAA,GAAoB,OAAOC,IAAAA,E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6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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;
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var index = require('./domain/index.js');
|
|
4
|
+
var index$1 = require('./engine/index.js');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
exports.domain = index;
|
|
9
|
+
exports.engine = index$1;
|
|
297
10
|
//# sourceMappingURL=index.js.map
|