-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
index.js
81 lines (65 loc) · 2.03 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
'use strict';
const isObject = require('is-plain-obj');
const autoBind = require('auto-bind');
const arrify = require('arrify');
const get = (obj, key) => typeof obj.get === 'function' ? obj.get(key) : obj[key];
const isPartiallyEqual = (target, obj) => {
return Object.keys(obj).every(key => get(target, key) === obj[key]);
};
const getConditionFn = condition => {
return (performer, target) => isPartiallyEqual(target, condition);
};
const defaultInstanceOf = (instance, model) => instance instanceof model;
const defaultCreateError = () => new Error('Authorization error');
class CanCan {
constructor(options) {
autoBind(this);
options = options || {};
this.abilities = [];
this.instanceOf = options.instanceOf || defaultInstanceOf;
this.createError = options.createError || defaultCreateError;
}
allow(model, actions, targets, condition) {
if (typeof condition !== 'undefined' && typeof condition !== 'function' && !isObject(condition)) {
throw new TypeError(`Expected condition to be object or function, got ${typeof condition}`);
}
if (isObject(condition)) {
condition = getConditionFn(condition);
}
arrify(actions).forEach(action => {
arrify(targets).forEach(target => {
this.abilities.push({model, action, target, condition});
});
});
}
can(performer, action, target, options) {
return this.abilities
.filter(ability => this.instanceOf(performer, ability.model))
.filter(ability => {
return ability.target === 'all' ||
target === ability.target ||
this.instanceOf(target, ability.target);
})
.filter(ability => {
return ability.action === 'manage' ||
action === ability.action;
})
.filter(ability => {
if (ability.condition) {
return ability.condition(performer, target, options || {});
}
return true;
})
.length > 0;
}
cannot() {
return !this.can.apply(this, arguments);
}
authorize() {
if (this.cannot.apply(this, arguments)) {
const err = this.createError.apply(null, arguments);
throw err;
}
}
}
module.exports = CanCan;