-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (53 loc) · 1.49 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
/**
* @author: EgorKluch ([email protected])
* @date: 29.01.14
*/
'use strict';
var AccessManager = function () {
this.handlers = {
'default': function (action, args, next) {
next(null, true);
}
};
this.prepareHandlers = {
'default': function (action, args, next) {
if (null === args) args = {};
next(null, args);
}
};
};
AccessManager.prototype.prepareHandle = function (action, prepareHandler) {
this._regHandler(action, prepareHandler, this.prepareHandlers);
};
AccessManager.prototype.handle = function (action, handler) {
this._regHandler(action, handler, this.handlers);
};
AccessManager.prototype._regHandler = function (action, handler, container) {
var self = this;
if (action instanceof Array) {
return action.forEach(function (act) {
self._regHandler(act, handler, container);
});
}
if (action instanceof Function) {
handler = action;
action = 'default';
}
if (!action) action = 'default';
container[action] = handler;
};
AccessManager.prototype.hasAccess = function (action, args, next) {
var self = this;
var prepareAction = action;
if (!this.prepareHandlers.hasOwnProperty(prepareAction)) {
prepareAction = 'default';
}
this.prepareHandlers[prepareAction](action, args, function (err, args) {
if (err) return next(err);
if (!self.handlers.hasOwnProperty(action)) {
action = 'default';
}
self.handlers[action](action, args, next);
});
};
module.exports = AccessManager;