-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
208 lines (199 loc) · 5.26 KB
/
Copy pathindex.js
File metadata and controls
208 lines (199 loc) · 5.26 KB
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
var _ = require('lodash');
/**
* Serverbone ACL
*
* Serverbone ACL provides a simple inline RBAC that can be easily extented to be used with normal user/pass
* authentication or token based authentication.
*
* Roles can be granted several ways,
*
* - Directly to a model (like User model instance) via .grant(roles)
* - Based on schema relations, grant 'owner, user' to user model that has its id in this.get('user_id') as seen below.
* - via overriding getRoles(model) in ACLModel subclass, like seen also below.
*
* var MyModel = BaseModel.extend({
* schema: {
* permissions: {
* "*": ["read","create"],
* owner: ["update","delete"],
* admin: ["*"]
* },
* properties: {
* hidden_id: {
* type: "integer",
* permissions: {
* "*": [], owner:[]
* }
* },
* hiddent_text: {
* type: "string",
* permissions: {
* "*": [],
* "hidden": ["read","update"]
* }
* },
* user_id: {"type":"integer"},
* user: {
* "type": "relation",
* "roles": ["owner"],
* "references": {
* id: 'user_id'
* }
* }
* }
* },
* getRoles: function(model) {
* // returns function that matches model type against 'user'
* var isUser = acl.type('user');
* // returns function that matches this.get('hidden_id') against model.get('hidden_id')
* var isIdMatch = acl.property('hidden_id', 'hidden_id');
* var roles = [];
* if(isUser(model) && isIdMatch(this, model)) {
* // grant model 'hidden' role against this model to allow access to hidden_text
* roles.push('hidden');
* }
* return roles;
* }
* });
*
*/
// Default role permission sets that are implicit
var Permissions = {
owner: ['read', 'update'],
admin: ['*'],
'*': ['create', 'read']
};
/**
* Create new ACL with passed in roles and attached permissions.
*
* permissions are in the form of {"role":["action1","action2"]}
*
* @param {Object} permissions
*/
exports.ACL = function (permissions) {
// Permissions map roles to array of actions: {"user":["read"],"admin":["write"]}
this.permissions = _.extend({}, Permissions, permissions);
};
_.extend(exports.ACL.prototype, {
/**
* allows additional permissions for this acl
*
* @param {Object} permissions
*
*/
grant: function (permissions) {
_.merge(this.permissions, permissions, function (a, b) {
return _.isArray(a) ? a.concat(b) : undefined;
});
return this;
},
/**
* Revoke a role
*
* @param {String|Array} roles a role or roles to revoke
* @api public
*
* @return {ACL} this for chaining
*/
revoke: function (roles) {
if (!Array.isArray(roles)) {
roles = [roles];
}
this.permissions = _.reduce(this.permissions, function (result, actions, key) {
if (roles.indexOf(key) === -1) {
result[key] = actions;
}
return result;
}, {});
return this;
},
/**
* Assert access to this acl's 'action' with role(s)
*
* Can I write here with these roles?
*
*
* @param {String|Array} roles
* @param {String} action
*
* @example
*
* var ownerReading = acl.assert(['*','owner'], 'read')
* var someoneWriting = acl.assert(['*']‚ 'write')
*
* @return {Boolean}
*/
assert: function (roles, action) {
var self = this;
if (!action) {
action = roles;
roles = ['*'];
}
if (!Array.isArray(roles)) {
roles = [roles];
}
roles.push('*');
var matches = _.filter(roles, function (role) {
var actions = self.permissions[role] || self.permissions['*'];
return actions.indexOf(action) !== -1 || actions.indexOf('*') !== -1;
});
return matches.length > 0;
},
/**
* Returns a list of actions given role(s) have access to
* @param {String|Array} roles
* @return {Array} list of actions
*/
allowedActions: function(roles) {
roles = roles || ['*'];
if (!Array.isArray(roles)) {
roles = [roles];
}
if (roles.indexOf('*') === -1) roles.push('*');
var actions = [];
_.each(roles, function(role) {
if (_.has(this.permissions, role)) {
actions = actions.concat(this.permissions[role]);
}
}, this);
return _.uniq(actions);
}
});
/**
* Asserts actorModel.type == type || actorModel.get('type') == type
*
* @param {[type]} type
* @return {[type]}
*/
exports.type = function (type) {
return function (actor) {
if (!actor) return false;
return actor.has('type') ? actor.get('type') === type : actor.type === type;
};
};
/**
* Asserts actorModel.has(key) && actorModel.get(key) === value
*
* @param {String} attr
* @param {Object} value
* @api public
* @return {Boolean}
*/
exports.attribute = function (attr, value) {
return function (model, actor) {
return actor.has(attr) && actor.get(attr) === value;
};
};
/**
* Asserts model.has(key) && actorModel.has(key) && actorModel.get(key) === model.get(key)
*
* @param {String} key
* @param {Object} actorKey
* @api public
* @return {Boolean}
*/
exports.property = function (key, actorKey) {
return function (model, actor) {
return actor.has(actorKey) && model.has(key) && model.get(key) === actor.get(actorKey);
};
};