-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js.html
231 lines (189 loc) · 7.62 KB
/
index.js.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>index.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="VotersStack.html">VotersStack</a><ul class='methods'><li data-type='method'><a href="VotersStack.html#isAllowed">isAllowed</a></li><li data-type='method'><a href="VotersStack.html#registerVoter">registerVoter</a></li><li data-type='method'><a href="VotersStack.html#unregisterVoter">unregisterVoter</a></li></ul></li></ul><h3>Interfaces</h3><ul><li><a href="VoterInterface.html">VoterInterface</a><ul class='methods'><li data-type='method'><a href="VoterInterface.html#supportsPermission">supportsPermission</a></li><li data-type='method'><a href="VoterInterface.html#supportsResource">supportsResource</a></li><li data-type='method'><a href="VoterInterface.html#vote">vote</a></li></ul></li></ul>
</nav>
<div id="main">
<h1 class="page-title">index.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>var _ = require('lodash');
var async = require('async');
var Interface = require('./Interface');
var VoterInterface = require('./VoterInterface');
exports = module.exports = (function () {
/**
* Voting decision values.
*
* @member
* @readonly
* @enum {number}
*/
const DECISION = {
ACCESS_GRANTED: true,
ACCESS_ABSTAIN: undefined,
ACCESS_DENIED: false
};
/**
* Wraps voters into one control service.
*
* @class
* @name VotersStack
* @constructor
* @param {Object} config - Config object. If any key will be missing, default value will be copied from {@link VotersStack#defaultConfig}.
* @param {DECISION} config.defaultDecision - default decision, used when none of the voters can make decision.
* @param {Array} config.voters initial list of voters
*/
function VotersStack(config) {
this.config = _.defaults(config, VotersStack.defaultConfig);
this.voters = [];
for (var i = 0, len = this.config.voters.length; i < len; i++) {
this.registerVoter(this.config.voters[i]);
}
}
/**
* Default config for VotersStack.
*
* @static
* @name VotersStack#defaultConfig
* @property {DECISION} defaultDecision - default decision, used when none of the voters can make decision.
* @property {Array} voters initial list of voters
* @default {
defaultDecision: DECISION.ACCESS_DENIED,
voters: []
};
*/
VotersStack.defaultConfig = {
defaultDecision: DECISION.ACCESS_DENIED,
voters: []
};
/**
* Register new voter. Voter will be used in decision making.
*
* @method
* @name VotersStack#registerVoter
* @param {Object} voter - voter, should implement methods from VoterInterface
*/
VotersStack.prototype.registerVoter = function (voter) {
Interface.ensureImplements(voter, VoterInterface);
this.voters.push(voter);
};
/**
* Unregister voter. Voter will no longer be used in decision making.
*
* @method
* @name VotersStack#unregisterVoter
* @param {Object} voter - voter, should implement methods from VoterInterface
*/
VotersStack.prototype.unregisterVoter = function (voter) {
this.voters = _.without(this.voters, voter);
};
/**
* Check if user has permission to perform action on resource.
*
* @method
* @name VotersStack#isAllowed
*
* @param {*} user
* @param {*} resource
* @param {*} permissions
* @param {VotersStack~votingCallback} cb
*/
VotersStack.prototype.isAllowed = function (user, resource, permissions, cb) {
if (!_.isArray(permissions)) {
permissions = [permissions];
}
async.map(permissions, function (permission, callback) {
isUserAllowed.call(this, user, resource, permission, callback);
}.bind(this), function (err, result) {
//change all ABSTAIN do options.defaultValue
result = _.map(result, function (el) {
return (el == DECISION.ACCESS_ABSTAIN) ? this.config.defaultDecision : el;
}.bind(this));
//format response
if (permissions.length > 1) {
cb(null, _.zipObject(permissions, result)); //if multiple permissions, return { permission: decision } object
} else {
cb(null, result[0]); //otherwise return decision
}
}.bind(this));
function isUserAllowed(user, resource, permission, cb) {
var voters = this.voters;
async.waterfall([
function (callback) {
filterVoters.call(this, voters, resource, permission, function (err, filteredVoters) {
callback(err, filteredVoters, user, resource, permission);
})
},
iterateThroughVoters
], cb);
}
function filterVoters(voters, resource, permission, cb) {
async.filter(voters, function (voter, cb) {
cb(voter.supportsResource(resource) && voter.supportsPermission(permission));
}, function (results) {
cb(null, results);
});
}
function iterateThroughVoters(voters, user, resource, permission, cb) {
var decision = DECISION.ACCESS_ABSTAIN;
var i = 0;
async.whilst(
function () {
return (decision == DECISION.ACCESS_ABSTAIN) && (i < voters.length);
},
function (callback) {
var voter = voters[i++];
voter.vote(user, resource, permission, function (err, result) {
if (err) {
return callback(err)
}
decision = result;
callback(null);
});
},
function (err) {
cb(null, decision);
}
)
}
};
VotersStack.DECISION = DECISION;
/**
* Callback called after voting.
*
* @callback VotersStack~votingCallback
* @param {null|object} error
* @param {DECISION} decision - Voting decision.
*/
return VotersStack;
})();</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.3</a> on Thu Nov 19 2015 20:44:58 GMT+0100 (CET) using the Minami theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>