@strapi/permissions 0.0.0-next.e9bb5ccdc459f4c6b6717a2d5d86359b7a47d47d → 0.0.0-next.eb48c73c86cbc452c1ba8d727106f9ed9da0c834

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 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 in "ee/LICENSE".
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, 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, 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
22
- SOFTWARE.
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,3 @@
1
+ import * as permission from './permission';
2
+ export { permission };
3
+ //# sourceMappingURL=index.d.ts.map
@@ -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,2 @@
1
+ export * from './casl-ability';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -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, create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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":"index.d.ts","sourceRoot":"","sources":["../../src/engine/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAQhD,OAAO,KAAK,EAAE,qBAAqB,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE/D,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,KAAK,EAAE,cAAc,EAAE,MA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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,345 @@
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);
11
+ if (e) {
12
+ Object.keys(e).forEach(function (k) {
13
+ if (k !== 'default') {
14
+ var d = Object.getOwnPropertyDescriptor(e, k);
15
+ Object.defineProperty(n, k, d.get ? d : {
16
+ enumerable: true,
17
+ get: function () { return e[k]; }
18
+ });
19
+ }
20
+ });
21
+ }
22
+ n.default = e;
23
+ return Object.freeze(n);
24
+ }
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);
55
+ });
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({
61
+ __proto__: null,
62
+ addCondition: addCondition,
63
+ create: create,
64
+ getProperty: getProperty,
65
+ sanitizePermissionFields: sanitizePermissionFields
66
+ });
67
+
68
+ var index$2 = /*#__PURE__*/Object.freeze({
69
+ __proto__: null,
70
+ permission: index$3
71
+ });
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);
87
+ }
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
+
143
+ const allowedOperations = [
144
+ '$or',
145
+ '$and',
146
+ '$eq',
147
+ '$ne',
148
+ '$in',
149
+ '$nin',
150
+ '$lt',
151
+ '$lte',
152
+ '$gt',
153
+ '$gte',
154
+ '$exists',
155
+ '$elemMatch'
156
+ ];
157
+ const operations = _.pick(allowedOperations, sift__namespace);
158
+ const conditionsMatcher = (conditions)=>{
159
+ return sift__namespace.createQueryTester(conditions, {
160
+ operations
161
+ });
162
+ };
163
+ const buildParametrizedAction = ({ name, params })=>{
164
+ return `${name}?${qs.stringify(params)}`;
165
+ };
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
+ };
200
+ };
201
+
202
+ var index$1 = /*#__PURE__*/Object.freeze({
203
+ __proto__: null,
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
213
+ };
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);
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
+ /**
296
+ * Create a register function that wraps a `can` function
297
+ * used to register a permission in the ability builder
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
+ /**
309
+ * Register a new handler for a given hook
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
+ /**
320
+ * Generate an ability based on the instance's
321
+ * ability builder and the given permissions
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
+ };
335
+ };
336
+
337
+ var index = /*#__PURE__*/Object.freeze({
338
+ __proto__: null,
339
+ abilities: index$1,
340
+ new: newEngine
341
+ });
342
+
343
+ exports.domain = index$2;
344
+ exports.engine = index;
345
+ //# 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":["PERMISSION_FIELDS","sanitizePermissionFields","_","pick","getDefaultPermission","conditions","properties","subject","create","pipe","merge","addCondition","curry","condition","permission","newConditions","Array","isArray","uniq","concat","set","getProperty","property","get","createEngineHooks","hooks","createAsyncBailHook","createAsyncSeriesWaterfallHook","createAsyncSeriesHook","createValidateContext","cloneDeep","createBeforeEvaluateContext","Object","assign","domain","createWillRegisterContext","options","and","rawConditionObject","$and","push","or","orClause","find","has","$or","allowedOperations","operations","sift","conditionsMatcher","createQueryTester","buildParametrizedAction","name","params","qs","stringify","caslAbilityBuilder","can","build","rest","AbilityBuilder","Ability","action","fields","caslAction","isNil","isObject","undefined","ability","decorateCan","originalCan","args","apply","createEngineState","newEngine","providers","abilityBuilderFactory","abilities","state","runValidationHook","hook","context","call","evaluate","register","preFormatValidation","afterFormatValidation","actionName","actionParameters","keys","length","resolveConditions","map","removeInvalidConditions","filter","isFunction","handler","evaluateConditions","Promise","all","result","removeInvalidResults","isBoolean","evaluatedConditions","resolve","then","resultPropEq","propEq","pickResults","prop","every","isEmpty","some","results","createRegisterFunction","hookContext","on","validHooks","isValidHook","includes","Error","join","generateAbility","permissions"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,MAAMA,iBAAoB,GAAA;AAAC,I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