-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
59 lines (49 loc) · 1.3 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
var ability = new (require('./lib/ability'))();
// , helpers = require('./lib/helpers');
exports = module.exports = createAbilities;
function createAbilities(abilities) {
ability.abilities = abilities;
return exports;
}
exports.add = function (schema) {
createAbilities(schema);
}
exports.configure = function(options) {
for (var i in options) {
ability[i] = options[i];
// console.log(this);
}
}
exports.addHelpers = function(app) {
app.dynamicHelpers({
able: function (req, res) {
if (req.user) {
ability.role = req.user[ability.role_name];
}
return ability;
}
});
}
authorize = function(action, target, role) {
req = arguments.callee.caller.arguments[0];
res = arguments.callee.caller.arguments[1];
if (req.user) {
ability.role = req.user[ability.role_name];
}
if (role) {
ability.role = role;
}
// extrapolating everything from the req.route
if (target == null && action == null) {
value = ability.can_role(req);
} else {
// everything is explicitly defined or the user is not using everyauth
value = ability.can_ability(action, target);
}
if (ability.redirect == true && value == false) {
res.render = function(view, options, fn) {
req.flash("alert", ability.redirect_message);
res.redirect(ability.redirect_to);
}
}
}