This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
forked from cozy/cozy-clearance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (85 loc) · 2.35 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
// Generated by CoffeeScript 1.9.3
var randomString,
hasProp = {}.hasOwnProperty;
randomString = function(length) {
var string;
if (length == null) {
length = 32;
}
string = "";
while (string.length < length) {
string += Math.random().toString(36).substr(2);
}
return string.substr(0, length);
};
exports.check = function(model, permission, req, callback) {
var clearance, key;
if (!model.clearance || model.clearance.length === 0) {
return callback(null, false);
}
if (model.clearance === 'public') {
return callback(null, true);
}
if (!Array.isArray(model.clearance)) {
return callback(new Error('malformed clearance'), false);
}
key = req.query.key;
clearance = model.clearance.filter(function(clearance) {
return clearance.key === key && -1 !== clearance.perm.indexOf(permission);
});
return callback(null, clearance[0] || false);
};
exports.make = function(model, permission, details) {
var clearance, property, value;
if (details == null) {
details = {};
}
clearance = {
perm: permission
};
for (property in details) {
if (!hasProp.call(details, property)) continue;
value = details[property];
clearance[property] = value;
}
clearance.key = randomString();
return clearance;
};
exports.add = function(model, permission, details, callback) {
var clearance, ref, rule;
if (callback == null) {
ref = [{}, details], details = ref[0], callback = ref[1];
}
rule = exports.make(model, permission, details);
clearance = model.clearance || [];
clearance = clearance.concat(rule);
return model.updateAttributes({
clearance: clearance
}, function(err) {
return callback(err, rule.key);
});
};
exports.revoke = function(model, details, callback) {
var clearance, dontMatch;
dontMatch = function(clearance) {
var property, value;
for (property in details) {
if (!hasProp.call(details, property)) continue;
value = details[property];
if (clearance[property] !== value) {
return true;
}
}
return false;
};
clearance = model.clearance.filter(dontMatch);
return model.updateAttributes({
clearance: clearance
}, callback);
};
exports.replace = function(model, newclearance, callback) {
return model.updateAttributes({
clearance: newclearance
}, callback);
};
exports.controller = require('./controller');