-
Notifications
You must be signed in to change notification settings - Fork 13
/
roles.js
439 lines (371 loc) · 10.6 KB
/
roles.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* Init the variable
*/
Roles = {}
Roles.debug = false
/**
* Initialize variables
*/
Roles._roles = {}
Roles._actions = []
Roles._helpers = []
Roles._usersCollection = Meteor.users
Roles._specialRoles = ['__loggedIn__', '__notAdmin__', '__notLoggedIn__', '__all__']
/**
* Old collection
*/
Roles._oldCollection = new Mongo.Collection('roles')
/**
* Get the list of roles
*/
Roles.availableRoles = function () {
return _.difference(_.keys(this._roles), this._specialRoles)
}
/**
* Check if a user has a role
*/
Roles.userHasRole = function (userId, role) {
if (role == '__all__') return true
if (role == '__notLoggedIn__' && !userId) return true
if (role == '__default__' && userId) return true
if (
role == '__notAdmin__' &&
Roles._usersCollection.find({ _id: userId, roles: 'admin' }).count() === 0
) return true
return Roles._usersCollection.find({ _id: userId, roles: role }).count() > 0
}
/**
* Creates a new action
*/
Roles.registerAction = function (name, adminAllow, adminDeny) {
check(name, String)
check(adminAllow, Match.Optional(Match.Any))
check(adminDeny, Match.Optional(Match.Any))
if (!_.contains(this._actions, name)) {
this._actions.push(name)
}
if (adminAllow) {
Roles.adminRole.allow(name, adminAllow)
}
if (adminDeny) {
Roles.adminRole.deny(name, adminDeny)
}
}
/**
* Creates a new helper
*/
Roles.registerHelper = function (name, adminHelper) {
check(name, String)
check(adminHelper, Match.Any)
if (!_.contains(this._helpers, name)) {
this._helpers.push(name)
}
if (adminHelper) {
Roles.adminRole.helper(name, adminHelper)
}
}
/**
* Constructs a new role
*/
Roles.Role = function (name) {
check(name, String)
if (!(this instanceof Roles.Role))
throw new Error('use "new" to construct a role')
if (_.has(Roles._roles, name))
throw new Error('"' + name + '" role is already defined')
this.name = name
this.allowRules = {}
this.denyRules = {}
this.helpers = {}
Roles._roles[name] = this
}
/**
* Adds allow properties to a role
*/
Roles.Role.prototype.allow = function (action, allow) {
check(action, String)
check(allow, Match.Any)
if (!_.contains(Roles._actions, action)) {
Roles.registerAction(action)
}
if (!_.isFunction(allow)) {
var clone = _.clone(allow)
allow = function () {
return clone
}
}
this.allowRules[action] = this.allowRules[action] || []
this.allowRules[action].push(allow)
}
/**
* Adds deny properties to a role
*/
Roles.Role.prototype.deny = function (action, deny) {
check(action, String)
check(deny, Match.Any)
if (!_.contains(Roles._actions, action)) {
Roles.registerAction(action)
}
if (!_.isFunction(deny)) {
var clone = _.clone(deny)
deny = function () {
return clone
}
}
this.denyRules[action] = this.denyRules[action] || []
this.denyRules[action].push(deny)
}
/**
* Adds a helper to a role
*/
Roles.Role.prototype.helper = function (helper, func) {
check(helper, String)
check(func, Match.Any)
if (!_.contains(Roles._helpers, helper)) {
Roles.registerHelper(helper)
}
if (!_.isFunction(func)) {
var value = _.clone(func)
func = function () {
return value
}
}
if (!this.helpers[helper]) {
this.helpers[helper] = []
}
this.helpers[helper].push(func)
}
/**
* Get user roles
*/
Roles.getUserRoles = function (userId, includeSpecial) {
check(userId, Match.OneOf(String, null, undefined))
check(includeSpecial, Match.Optional(Boolean))
var object = Roles._usersCollection.findOne({ _id: userId }, { fields: { roles: 1 } })
var roles = (object && object.roles) || []
if (includeSpecial) {
roles.push('__all__')
if (!userId) {
roles.push('__notLoggedIn__')
} else {
roles.push('__loggedIn__')
if (!_.contains(roles, 'admin')) {
roles.push('__notAdmin__')
}
}
}
return roles
}
/**
* Calls a helper
*/
Roles.helper = function (userId, helper) {
check(userId, Match.OneOf(String, null, undefined))
check(helper, String)
if (!_.contains(this._helpers, helper)) throw 'Helper "' + helper + '" is not defined'
var args = _.toArray(arguments).slice(2)
var context = { userId: userId }
var responses = []
var roles = Roles.getUserRoles(userId, true)
_.each(roles, (role) => {
if (this._roles[role] && this._roles[role].helpers && this._roles[role].helpers[helper]) {
var helpers = this._roles[role].helpers[helper]
_.each(helpers, (helper) => {
responses.push(helper.apply(context, args))
})
}
})
return responses
}
/**
* Returns if the user passes the allow check
*/
Roles.allow = function (userId, action) {
check(userId, Match.OneOf(String, null, undefined))
check(action, String)
var args = _.toArray(arguments).slice(2)
var self = this
var context = { userId: userId }
var allowed = false
var roles = Roles.getUserRoles(userId, true)
_.each(roles, function (role) {
if (!allowed && self._roles[role] && self._roles[role].allowRules && self._roles[role].allowRules[action]) {
_.each(self._roles[role].allowRules[action], function (func) {
var allow = func.apply(context, args)
if (allow === true) {
allowed = true
}
})
}
})
return allowed
}
/**
* Returns if the user has permission using deny and deny
*/
Roles.deny = function (userId, action) {
check(userId, Match.OneOf(String, null, undefined))
check(action, String)
var args = _.toArray(arguments).slice(2)
var context = { userId: userId }
var denied = false
var roles = Roles.getUserRoles(userId, true)
_.each(roles, (role) => {
if (
!denied &&
this._roles[role] &&
this._roles[role].denyRules &&
this._roles[role].denyRules[action]
) {
_.each(this._roles[role].denyRules[action], (func) => {
var denies = func.apply(context, args)
if (denies === true) {
denied = true
if (Roles.debug) {
console.log(`[${action}] denied for ${userId} with role ${role}`)
}
}
})
}
})
return denied
}
/**
* To check if a user has permisisons to execute an action
*/
Roles.userHasPermission = function () {
var allows = this.allow.apply(this, arguments)
var denies = this.deny.apply(this, arguments)
return allows === true && denies === false
}
/**
* If the user doesn't has permission it will throw a error
*/
Roles.checkPermission = function () {
if (!this.userHasPermission.apply(this, arguments)) {
throw new Meteor.Error('unauthorized', 'The user has no permission to perform this action')
}
}
/**
* Adds helpers to users
*/
Roles.setUsersHelpers = function () {
Roles._usersCollection.helpers({
/**
* Returns the user roles
*/
getRoles: function (includeSpecial) {
return Roles.getUserRoles(this._id, includeSpecial)
},
/**
* To check if the user has a role
*/
hasRole: function (role) {
return Roles.userHasRole(this._id, role)
},
})
}
Roles.setUsersHelpers()
/**
* The admin role, who recives the default actions.
*/
Roles.adminRole = new Roles.Role('admin'); Roles._adminRole = Roles.adminRole // Backwards compatibility
/**
* All the logged in users users
*/
Roles.loggedInRole = new Roles.Role('__loggedIn__'); Roles.defaultRole = Roles.loggedInRole // Backwards compatibility
/**
* The users that are not admins
*/
Roles.notAdminRole = new Roles.Role('__notAdmin__')
/**
* The users that are not logged in
*/
Roles.notLoggedInRole = new Roles.Role('__notLoggedIn__')
/**
* Always, no exception
*/
Roles.allRole = new Roles.Role('__all__')
/**
* A Helper to attach actions to collections easily
*/
Mongo.Collection.prototype.attachRoles = function (name, dontAllow) {
Roles.registerAction(name + '.insert', !dontAllow)
Roles.registerAction(name + '.update', !dontAllow)
Roles.registerAction(name + '.remove', !dontAllow)
Roles.registerHelper(name + '.forbiddenFields', [])
this.allow({
insert: function (userId, doc) {
var allows = Roles.allow(userId, name + '.insert', userId, doc)
if (Roles.debug && !allows) {
console.log(`[${name}.insert] not allowed for ${userId}`)
}
return allows
},
update: function (userId, doc, fields, modifier) {
var allows = Roles.allow(userId, name + '.update', userId, doc, fields, modifier)
if (Roles.debug && !allows) {
console.log(`[${name}.update] not allowed for ${userId}`)
}
return allows
},
remove: function (userId, doc) {
var allows = Roles.allow(userId, name + '.remove', userId, doc)
if (Roles.debug && !allows) {
console.log(`[${name}.remove] not allowed for ${userId}`)
}
return allows
}
})
this.deny({
insert: function (userId, doc) {
return Roles.deny(userId, name + '.insert', userId, doc)
},
update: function (userId, doc, fields, modifier) {
return Roles.deny(userId, name + '.update', userId, doc, fields, modifier)
},
remove: function (userId, doc) {
return Roles.deny(userId, name + '.remove', userId, doc)
},
})
this.deny({
insert: function (userId, doc) {
var forbiddenFields = _.union.apply(this, Roles.helper(userId, name + '.forbiddenFields'))
for (var i in forbiddenFields) {
var field = forbiddenFields[i]
if (objectHasKey(doc, field)) {
if (Roles.debug) {
console.log(`[${name}.forbiddenField] Field ${field} is forbidden for ${userId}`)
}
return true
}
}
},
update: function (userId, doc, fields, modifier) {
var forbiddenFields = _.union.apply(this, Roles.helper(userId, name + '.forbiddenFields', doc._id))
var types = ['$inc', '$mul', '$rename', '$setOnInsert', '$set', '$unset', '$min', '$max', '$currentDate']
// By some reason following for will itterate even through empty array. This will prevent unwanted habbit.
if (forbiddenFields.length === 0) {
return false
}
for (var i in forbiddenFields) {
var field = forbiddenFields[i]
for (var j in types) {
var type = types[j]
if (objectHasKey(modifier[type], field)) {
if (Roles.debug) {
console.log(`[${name}.forbiddenField] Field ${field} is forbidden for ${userId}`)
}
return true
}
if (willChangeWithParent(modifier[type], field)) {
if (Roles.debug) {
console.log(`[${name}.forbiddenField] Field ${field} is forbidden for ${userId} is been changed by a parent object`)
}
return true
}
}
}
},
})
}