forked from vadimdemedes/cancan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
303 lines (237 loc) · 5.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
'use strict';
/**
* Dependencies
*/
var isPlainObject = require('is-plain-obj');
var isFunction = require('is-function');
var isArray = require('isarray');
var equals = require('equals');
var format = require('util').format;
/**
* Expose main functions
*/
module.exports = {
configure: configure,
authorize: authorize,
cannot: cannot,
can: can
};
/**
* Configs for each entity
*/
let entityConfigs = [];
/**
* CanCan
*/
/**
* Add a new configuration for a class/entity
*
* @param {Function} entity - entity class/function
* @param {Function} config - function that defines rules
*/
function configure (entity, config) {
entityConfigs.push({
entity: entity,
config: config
});
}
/**
* Test if an entity instance can execute
* specific action on a sepcific target
*
* @param {Object} model - class/entity instance
* @param {String} action - action name
* @param {Object} target - target instance
* @return {Boolean}
*/
function can (model, action, target) {
var config;
// find a configuration for a model
entityConfigs.forEach(function (item) {
// check if model is an instance of
// the current entity
if (model.constructor === item.entity) {
config = item.config;
}
});
// no configuration found for
// the current model, quit
if (!config) {
return false;
}
// configure rules for
// this entity instance
var ability = new Ability();
config.call(ability, model);
// test for access
return ability.test(action, target);
}
/**
* Return negated result of #can()
* @return {Boolean}
*/
function cannot () {
return !can.apply(null, arguments);
}
/**
* Same as #can(), but throws an exception
* if access is not granted
*/
function authorize () {
var result = can.apply(null, arguments);
if (!result) {
var err = new Error('Not authorized');
err.status = 401;
throw err;
}
}
/**
* Ability definition
*/
function Ability () {
this.rules = [];
}
/**
* Ability#addRule alias
*/
Ability.prototype.can = function can () {
return this.addRule.apply(this, arguments);
};
/**
* Add a new rule
*
* @param {Array|String} actions - name or array of names
* @param {Array|Function} targets - function or array of functions (classes)
* @param {Function|Object} attrs - validator function or object of properties
*/
Ability.prototype.addRule = function addRule (actions, targets, attrs) {
// accept both arrays and single items
// in actions and targets
if (!isArray(actions)) {
actions = [actions];
}
if (!isArray(targets)) {
targets = [targets];
}
var ability = this;
// for each action and target
// add a new rule
actions.forEach(function (action) {
targets.forEach(function (target) {
ability.rules.push({
action: action,
target: target,
attrs: attrs
});
});
});
};
/**
* Test if access should be granted
*
* @param {String} action - action name
* @param {Object} target - target object
* @return {Boolean}
*/
Ability.prototype.test = function test (action, target) {
// filter out rules, that don't match
// the requested action and target
var rules = this.rules.filter(function (rule) {
// include rule in the result only if
// action, target and attributes match
return actionMatches(action, rule) &&
targetMatches(target, rule) &&
attrsMatch(target, rule);
});
// if there are matching rules,
// test is successful
return rules.length > 0;
};
/**
* Helpers
*/
/**
* Test if action requirements are satisfied
*
* @param {String} action - action name
* @param {Object} rule - rule object
* @return {Boolean}
*/
function actionMatches (action, rule) {
// action should be:
// 1. equal to rule's action
// 2. equal to "manage" to allow all actions
return action === rule.action || rule.action === 'manage';
}
/**
* Test if target requirements are satisfied
*
* @param {Object} target - target object
* @param {Object} rule - rule object
* @return {Boolean}
*/
function targetMatches (target, rule) {
// target should be:
// 1. an instance of rule's target entity
// 2. equal to "all" to allow all entities
return target.constructor === rule.target || rule.target === 'all';
}
/**
* Test if attributes match
*
* @param {Object} target - target object
* @param {Object} rule - rule object
* @return {Boolean}
*/
function attrsMatch (target, rule) {
// if validator function is set
// return its result directly
if (isFunction(rule.attrs)) {
return rule.attrs(target);
}
// test if rule's requirements
// are satisfied
if (isPlainObject(rule.attrs)) {
return matches(target, rule.attrs);
}
// unknown type of attributes
// or no required attributes at all
return true;
}
/**
* Get a property of an object
* and use .get() method, if there is one
* to support various ORM/ODMs
*
* @param {Object} model - target object
* @param {String} property - wanted property
* @return {Mixed}
*/
function get (model, property) {
// support for various ODM/ORMs
if (isFunction(model.get)) {
return model.get(property);
}
return model[property];
}
/**
* Determine whether `obj` has all `props` and
* their exact values
*
* @param {Object} obj - target object
* @param {Object} props - set of required properties
* @return {Boolean}
*/
function matches (obj, props) {
var match = true;
var keys = Object.keys(props);
keys.forEach(function (key) {
var expectedValue = props[key];
var actualValue = get(obj, key);
// test if values deep equal
if (!equals(actualValue, expectedValue)) {
match = false;
}
});
return match;
}