@strapi/permissions 0.0.0-next.fc231041206e6f3999b094160cfa05db2892ad54 → 0.0.0-next.fc9d26d995624dc886b29f563e1de655d47e6609
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/index.js +309 -261
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +297 -249
- package/dist/index.mjs.map +1 -1
- package/package.json +11 -11
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.
|
package/dist/index.js
CHANGED
|
@@ -1,297 +1,345 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
return e;
|
|
12
|
-
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _ = require('lodash/fp');
|
|
4
|
+
var qs = require('qs');
|
|
5
|
+
var utils = require('@strapi/utils');
|
|
6
|
+
var sift = require('sift');
|
|
7
|
+
var ability = require('@casl/ability');
|
|
8
|
+
|
|
9
|
+
function _interopNamespaceDefault(e) {
|
|
10
|
+
var n = Object.create(null);
|
|
13
11
|
if (e) {
|
|
14
|
-
|
|
15
|
-
if (k !==
|
|
16
|
-
|
|
12
|
+
Object.keys(e).forEach(function (k) {
|
|
13
|
+
if (k !== 'default') {
|
|
14
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
17
15
|
Object.defineProperty(n, k, d.get ? d : {
|
|
18
16
|
enumerable: true,
|
|
19
|
-
get: ()
|
|
17
|
+
get: function () { return e[k]; }
|
|
20
18
|
});
|
|
21
19
|
}
|
|
22
|
-
}
|
|
20
|
+
});
|
|
23
21
|
}
|
|
24
22
|
n.default = e;
|
|
25
23
|
return Object.freeze(n);
|
|
26
24
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const PERMISSION_FIELDS = [
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
25
|
+
|
|
26
|
+
var sift__namespace = /*#__PURE__*/_interopNamespaceDefault(sift);
|
|
27
|
+
|
|
28
|
+
const PERMISSION_FIELDS = [
|
|
29
|
+
'action',
|
|
30
|
+
'subject',
|
|
31
|
+
'properties',
|
|
32
|
+
'conditions'
|
|
33
|
+
];
|
|
34
|
+
const sanitizePermissionFields = _.pick(PERMISSION_FIELDS);
|
|
35
|
+
/**
|
|
36
|
+
* Creates a permission with default values for optional properties
|
|
37
|
+
*/ const getDefaultPermission = ()=>({
|
|
38
|
+
conditions: [],
|
|
39
|
+
properties: {},
|
|
40
|
+
subject: null
|
|
41
|
+
});
|
|
42
|
+
/**
|
|
43
|
+
* Create a new permission based on given attributes
|
|
44
|
+
*
|
|
45
|
+
* @param {object} attributes
|
|
46
|
+
*/ const create = _.pipe(_.pick(PERMISSION_FIELDS), _.merge(getDefaultPermission()));
|
|
47
|
+
/**
|
|
48
|
+
* Add a condition to a permission
|
|
49
|
+
*/ const addCondition = _.curry((condition, permission)=>{
|
|
50
|
+
const { conditions } = permission;
|
|
51
|
+
const newConditions = Array.isArray(conditions) ? _.uniq(conditions.concat(condition)) : [
|
|
52
|
+
condition
|
|
53
|
+
];
|
|
54
|
+
return _.set('conditions', newConditions, permission);
|
|
42
55
|
});
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
);
|
|
46
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Gets a property or a part of a property from a permission.
|
|
58
|
+
*/ const getProperty = _.curry((property, permission)=>_.get(`properties.${property}`, permission));
|
|
59
|
+
|
|
60
|
+
var index$3 = /*#__PURE__*/Object.freeze({
|
|
47
61
|
__proto__: null,
|
|
48
|
-
addCondition,
|
|
49
|
-
create,
|
|
50
|
-
getProperty,
|
|
51
|
-
sanitizePermissionFields
|
|
52
|
-
}
|
|
53
|
-
|
|
62
|
+
addCondition: addCondition,
|
|
63
|
+
create: create,
|
|
64
|
+
getProperty: getProperty,
|
|
65
|
+
sanitizePermissionFields: sanitizePermissionFields
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
var index$2 = /*#__PURE__*/Object.freeze({
|
|
54
69
|
__proto__: null,
|
|
55
70
|
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
71
|
});
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
permission
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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] });
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Create a hook map used by the permission Engine
|
|
75
|
+
*/ const createEngineHooks = ()=>({
|
|
76
|
+
'before-format::validate.permission': utils.hooks.createAsyncBailHook(),
|
|
77
|
+
'format.permission': utils.hooks.createAsyncSeriesWaterfallHook(),
|
|
78
|
+
'after-format::validate.permission': utils.hooks.createAsyncBailHook(),
|
|
79
|
+
'before-evaluate.permission': utils.hooks.createAsyncSeriesHook(),
|
|
80
|
+
'before-register.permission': utils.hooks.createAsyncSeriesHook()
|
|
81
|
+
});
|
|
82
|
+
/**
|
|
83
|
+
* Create a context from a domain {@link Permission} used by the validate hooks
|
|
84
|
+
*/ const createValidateContext = (permission)=>({
|
|
85
|
+
get permission () {
|
|
86
|
+
return _.cloneDeep(permission);
|
|
103
87
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
88
|
+
});
|
|
89
|
+
/**
|
|
90
|
+
* Create a context from a domain {@link Permission} used by the before valuate hook
|
|
91
|
+
*/ const createBeforeEvaluateContext = (permission)=>({
|
|
92
|
+
get permission () {
|
|
93
|
+
return _.cloneDeep(permission);
|
|
94
|
+
},
|
|
95
|
+
addCondition (condition) {
|
|
96
|
+
Object.assign(permission, addCondition(condition, permission));
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
/**
|
|
101
|
+
* Create a context from a casl Permission & some options
|
|
102
|
+
* @param caslPermission
|
|
103
|
+
*/ const createWillRegisterContext = ({ permission, options })=>({
|
|
104
|
+
...options,
|
|
105
|
+
get permission () {
|
|
106
|
+
return _.cloneDeep(permission);
|
|
107
|
+
},
|
|
108
|
+
condition: {
|
|
109
|
+
and (rawConditionObject) {
|
|
110
|
+
if (!permission.condition) {
|
|
111
|
+
permission.condition = {
|
|
112
|
+
$and: []
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
if (_.isArray(permission.condition.$and)) {
|
|
116
|
+
permission.condition.$and.push(rawConditionObject);
|
|
117
|
+
}
|
|
118
|
+
return this;
|
|
119
|
+
},
|
|
120
|
+
or (rawConditionObject) {
|
|
121
|
+
if (!permission.condition) {
|
|
122
|
+
permission.condition = {
|
|
123
|
+
$and: []
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
if (_.isArray(permission.condition.$and)) {
|
|
127
|
+
const orClause = permission.condition.$and.find(_.has('$or'));
|
|
128
|
+
if (orClause) {
|
|
129
|
+
orClause.$or.push(rawConditionObject);
|
|
130
|
+
} else {
|
|
131
|
+
permission.condition.$and.push({
|
|
132
|
+
$or: [
|
|
133
|
+
rawConditionObject
|
|
134
|
+
]
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return this;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
109
143
|
const allowedOperations = [
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
144
|
+
'$or',
|
|
145
|
+
'$and',
|
|
146
|
+
'$eq',
|
|
147
|
+
'$ne',
|
|
148
|
+
'$in',
|
|
149
|
+
'$nin',
|
|
150
|
+
'$lt',
|
|
151
|
+
'$lte',
|
|
152
|
+
'$gt',
|
|
153
|
+
'$gte',
|
|
154
|
+
'$exists',
|
|
155
|
+
'$elemMatch'
|
|
122
156
|
];
|
|
123
157
|
const operations = _.pick(allowedOperations, sift__namespace);
|
|
124
|
-
const conditionsMatcher = (conditions)
|
|
125
|
-
|
|
158
|
+
const conditionsMatcher = (conditions)=>{
|
|
159
|
+
return sift__namespace.createQueryTester(conditions, {
|
|
160
|
+
operations
|
|
161
|
+
});
|
|
126
162
|
};
|
|
127
|
-
const buildParametrizedAction = ({ name, params })
|
|
128
|
-
|
|
163
|
+
const buildParametrizedAction = ({ name, params })=>{
|
|
164
|
+
return `${name}?${qs.stringify(params)}`;
|
|
129
165
|
};
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
can(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Casl Ability Builder.
|
|
168
|
+
*/ const caslAbilityBuilder = ()=>{
|
|
169
|
+
const { can, build, ...rest } = new ability.AbilityBuilder(ability.Ability);
|
|
170
|
+
return {
|
|
171
|
+
can (permission) {
|
|
172
|
+
const { action, subject, properties = {}, condition } = permission;
|
|
173
|
+
const { fields } = properties;
|
|
174
|
+
const caslAction = typeof action === 'string' ? action : buildParametrizedAction(action);
|
|
175
|
+
return can(caslAction, _.isNil(subject) ? 'all' : subject, fields, _.isObject(condition) ? condition : undefined);
|
|
176
|
+
},
|
|
177
|
+
buildParametrizedAction ({ name, params }) {
|
|
178
|
+
return `${name}?${qs.stringify(params)}`;
|
|
179
|
+
},
|
|
180
|
+
build () {
|
|
181
|
+
const ability = build({
|
|
182
|
+
conditionsMatcher
|
|
183
|
+
});
|
|
184
|
+
function decorateCan(originalCan) {
|
|
185
|
+
return function(...args) {
|
|
186
|
+
const [action, ...rest] = args;
|
|
187
|
+
const caslAction = typeof action === 'string' ? action : buildParametrizedAction(action);
|
|
188
|
+
// Call the original `can` method
|
|
189
|
+
return originalCan.apply(ability, [
|
|
190
|
+
caslAction,
|
|
191
|
+
...rest
|
|
192
|
+
]);
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
ability.can = decorateCan(ability.can);
|
|
196
|
+
return ability;
|
|
197
|
+
},
|
|
198
|
+
...rest
|
|
199
|
+
};
|
|
161
200
|
};
|
|
162
|
-
|
|
201
|
+
|
|
202
|
+
var index$1 = /*#__PURE__*/Object.freeze({
|
|
163
203
|
__proto__: null,
|
|
164
|
-
caslAbilityBuilder
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const
|
|
171
|
-
|
|
172
|
-
|
|
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
|
-
);
|
|
204
|
+
caslAbilityBuilder: caslAbilityBuilder
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Create a default state object for the engine
|
|
209
|
+
*/ const createEngineState = ()=>{
|
|
210
|
+
const hooks = createEngineHooks();
|
|
211
|
+
return {
|
|
212
|
+
hooks
|
|
221
213
|
};
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
const
|
|
226
|
-
const
|
|
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
|
-
},
|
|
214
|
+
};
|
|
215
|
+
const newEngine = (params)=>{
|
|
216
|
+
const { providers, abilityBuilderFactory = caslAbilityBuilder } = params;
|
|
217
|
+
const state = createEngineState();
|
|
218
|
+
const runValidationHook = async (hook, context)=>state.hooks[hook].call(context);
|
|
249
219
|
/**
|
|
220
|
+
* Evaluate a permission using local and registered behaviors (using hooks).
|
|
221
|
+
* Validate, format (add condition, etc...), evaluate (evaluate conditions) and register a permission
|
|
222
|
+
*/ const evaluate = async (params)=>{
|
|
223
|
+
const { options, register } = params;
|
|
224
|
+
const preFormatValidation = await runValidationHook('before-format::validate.permission', createBeforeEvaluateContext(params.permission));
|
|
225
|
+
if (preFormatValidation === false) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
const permission = await state.hooks['format.permission'].call(params.permission);
|
|
229
|
+
const afterFormatValidation = await runValidationHook('after-format::validate.permission', createValidateContext(permission));
|
|
230
|
+
if (afterFormatValidation === false) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
await state.hooks['before-evaluate.permission'].call(createBeforeEvaluateContext(permission));
|
|
234
|
+
const { action: actionName, subject, properties, conditions = [], actionParameters = {} } = permission;
|
|
235
|
+
let action = actionName;
|
|
236
|
+
if (actionParameters && Object.keys(actionParameters).length > 0) {
|
|
237
|
+
action = `${actionName}?${qs.stringify(actionParameters)}`;
|
|
238
|
+
}
|
|
239
|
+
if (conditions.length === 0) {
|
|
240
|
+
return register({
|
|
241
|
+
action,
|
|
242
|
+
subject,
|
|
243
|
+
properties
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
const resolveConditions = _.map(providers.condition.get);
|
|
247
|
+
const removeInvalidConditions = _.filter((condition)=>_.isFunction(condition.handler));
|
|
248
|
+
const evaluateConditions = (conditions)=>{
|
|
249
|
+
return Promise.all(conditions.map(async (condition)=>({
|
|
250
|
+
condition,
|
|
251
|
+
result: await condition.handler(_.merge(options, {
|
|
252
|
+
permission: _.cloneDeep(permission)
|
|
253
|
+
}))
|
|
254
|
+
})));
|
|
255
|
+
};
|
|
256
|
+
const removeInvalidResults = _.filter(({ result })=>_.isBoolean(result) || _.isObject(result));
|
|
257
|
+
const evaluatedConditions = await Promise.resolve(conditions).then(resolveConditions).then(removeInvalidConditions).then(evaluateConditions).then(removeInvalidResults);
|
|
258
|
+
const resultPropEq = _.propEq('result');
|
|
259
|
+
const pickResults = _.map(_.prop('result'));
|
|
260
|
+
if (evaluatedConditions.every(resultPropEq(false))) {
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
if (_.isEmpty(evaluatedConditions) || evaluatedConditions.some(resultPropEq(true))) {
|
|
264
|
+
return register({
|
|
265
|
+
action,
|
|
266
|
+
subject,
|
|
267
|
+
properties
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
const results = pickResults(evaluatedConditions).filter(_.isObject);
|
|
271
|
+
if (_.isEmpty(results)) {
|
|
272
|
+
return register({
|
|
273
|
+
action,
|
|
274
|
+
subject,
|
|
275
|
+
properties
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
return register({
|
|
279
|
+
action,
|
|
280
|
+
subject,
|
|
281
|
+
properties,
|
|
282
|
+
condition: {
|
|
283
|
+
$and: [
|
|
284
|
+
{
|
|
285
|
+
$or: results
|
|
286
|
+
}
|
|
287
|
+
]
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
};
|
|
291
|
+
return {
|
|
292
|
+
get hooks () {
|
|
293
|
+
return state.hooks;
|
|
294
|
+
},
|
|
295
|
+
/**
|
|
250
296
|
* Create a register function that wraps a `can` function
|
|
251
297
|
* used to register a permission in the ability builder
|
|
252
|
-
*/
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
298
|
+
*/ createRegisterFunction (can, options) {
|
|
299
|
+
return async (permission)=>{
|
|
300
|
+
const hookContext = createWillRegisterContext({
|
|
301
|
+
options,
|
|
302
|
+
permission
|
|
303
|
+
});
|
|
304
|
+
await state.hooks['before-register.permission'].call(hookContext);
|
|
305
|
+
return can(permission);
|
|
306
|
+
};
|
|
307
|
+
},
|
|
308
|
+
/**
|
|
261
309
|
* Register a new handler for a given hook
|
|
262
|
-
*/
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
}
|
|
273
|
-
state.hooks[hook].register(handler);
|
|
274
|
-
return this;
|
|
275
|
-
},
|
|
276
|
-
/**
|
|
310
|
+
*/ on (hook, handler) {
|
|
311
|
+
const validHooks = Object.keys(state.hooks);
|
|
312
|
+
const isValidHook = validHooks.includes(hook);
|
|
313
|
+
if (!isValidHook) {
|
|
314
|
+
throw new Error(`Invalid hook supplied when trying to register an handler to the permission engine. Got "${hook}" but expected one of ${validHooks.join(', ')}`);
|
|
315
|
+
}
|
|
316
|
+
state.hooks[hook].register(handler);
|
|
317
|
+
return this;
|
|
318
|
+
},
|
|
319
|
+
/**
|
|
277
320
|
* Generate an ability based on the instance's
|
|
278
321
|
* ability builder and the given permissions
|
|
279
|
-
*/
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
322
|
+
*/ async generateAbility (permissions, options = {}) {
|
|
323
|
+
const { can, build } = abilityBuilderFactory();
|
|
324
|
+
for (const permission of permissions){
|
|
325
|
+
const register = this.createRegisterFunction(can, options);
|
|
326
|
+
await evaluate({
|
|
327
|
+
permission,
|
|
328
|
+
options,
|
|
329
|
+
register
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
return build();
|
|
333
|
+
}
|
|
334
|
+
};
|
|
289
335
|
};
|
|
290
|
-
|
|
336
|
+
|
|
337
|
+
var index = /*#__PURE__*/Object.freeze({
|
|
291
338
|
__proto__: null,
|
|
292
339
|
abilities: index$1,
|
|
293
340
|
new: newEngine
|
|
294
|
-
}
|
|
341
|
+
});
|
|
342
|
+
|
|
295
343
|
exports.domain = index$2;
|
|
296
344
|
exports.engine = index;
|
|
297
345
|
//# sourceMappingURL=index.js.map
|