This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.coffee
65 lines (44 loc) · 1.82 KB
/
index.coffee
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
randomString = (length=32) ->
string = ""
string += Math.random().toString(36).substr(2) while string.length < length
string.substr 0, length
exports.check = (model, permission, req, callback) ->
if not model.clearance or model.clearance.length is 0
return callback null, false
if model.clearance is 'public'
if permission is 'r'
return callback null, true
else
return callback null, false
if model.clearance is 'publicrw'
return callback null, true
unless Array.isArray(model.clearance)
return callback new Error('malformed clearance'), false
key = req.query.key
clearance = model.clearance.filter (clearance) ->
clearance.key is key and
-1 isnt clearance.perm.indexOf permission
callback null, clearance[0] or false
exports.make = (model, permission, details) ->
details ?= {}
clearance = perm: permission
clearance[property] = value for own property, value of details
clearance.key = randomString()
return clearance
exports.add = (model, permission, details, callback) ->
[details, callback] = [{}, details] unless callback?
rule = exports.make model, permission, details
clearance = model.clearance or []
clearance = clearance.concat rule
model.updateAttributes clearance: clearance, (err) ->
callback err, rule.key
exports.revoke = (model, details, callback) ->
dontMatch = (clearance) ->
for own property, value of details
return true if clearance[property] isnt value
return false
clearance = model.clearance.filter(dontMatch)
model.updateAttributes clearance: clearance, callback
exports.replace = (model, newclearance, callback) ->
model.updateAttributes clearance: newclearance, callback
exports.controller = require './controller'