-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
228 lines (210 loc) · 5.37 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
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
/**!
* koa-roles - index.js
*
* Copyright(c) Alibaba Group Holding Limited.
* MIT Licensed
*
* Authors:
* fengmk2 <m@fengmk2.com> (http://fengmk2.com)
*/
'use strict';
/**
* Module dependencies.
*/
var is = require('is-type-of');
module.exports = KoaRoles;
/**
* Role Middleware for Koa
* @class KoaRoles
* @example
* ```js
* var Roles = require('koa-roles');
* var koa = require('koa');
* var app = koa();
*
* var user = new Roles({
* failureHandler: function *(action) {
* // optional function to customise code that runs when
* // user fails authorisation
* this.status = 403;
* var t = this.accepts('json', 'html');
* if (t === 'json') {
* this.body = {
* message: 'Access Denied - You don\'t have permission to: ' + action
* };
* } else if (t === 'html') {
* this.render('access-denied', {action: action});
* } else {
* this.body = 'Access Denied - You don\'t have permission to: ' + action;
* }
* }
* });
*
* app.use(user.middleware());
*
* // anonymous users can only access the home page
* // returning false stops any more rules from being
* // considered
* user.use(function *(action) {
* return action === 'access home page';
* });
*
* // moderator users can access private page, but
* // they might not be the only ones so we don't return
* // false if the user isn't a moderator
* user.use('access private page', function (action) {
* if (this.user.role === 'moderator') {
* return true;
* }
* });
* app.get('/', user.can('access home page'), function *(next) {
* this.render('private');
* });
* app.get('/private', user.can('access private page'), function *(next) {
* this.render('private');
* });
* app.get('/admin', user.can('access admin page'), function *(next) {
* this.render('admin');
* });
*
* app.listen(3000);
* ```
*/
function KoaRoles(options) {
options = options || {};
this.functionList = [];
this.failureHandler = options.failureHandler || defaultFailureHandler;
this.userProperty = options.userProperty || 'user';
this.actionMap = {};
}
/**
* @method KoaRoles#use
* @param {String} action - Name of role
* @param {Function} fn
*/
KoaRoles.prototype.use = function () {
if (arguments.length === 1) {
// role.use(fn);
this.use1(arguments[0]);
} else if (arguments.length === 2) {
// role.use(action, fn);
this.use2(arguments[0], arguments[1]);
} else {
throw new Error('use can have 1 or 2 arguments, not ' + arguments.length);
}
};
function assertFunction(fn) {
if (!is.function(fn)) {
throw new TypeError('Expected fn to be of type function or generator function');
}
}
KoaRoles.prototype.use1 = function (fn) {
assertFunction(fn);
this.functionList.push(fn);
};
KoaRoles.prototype.use2 = function (action, fn) {
if (typeof action !== 'string') {
throw new TypeError('Expected action to be of type string');
}
if (action[0] === '/') {
throw new TypeError('action can\'t start with `/`');
}
assertFunction(fn);
var old = this.actionMap[action];
// create or override
this.actionMap[action] = fn;
// action fn have already been used, skip
if (old) {
return;
}
var roles = this;
this.use1(function *(act) {
// get fn from actionMap
var fn = roles.actionMap[action];
if (act === action) {
if (is.generatorFunction(fn)) {
return yield fn.call(this);
} else {
return fn.call(this);
}
}
});
};
/**
* @method KoaRoles#can
* @param {String} action
*/
KoaRoles.prototype.can = function (action) {
var roles = this;
return function *(next) {
if (yield roles.test(this, action)) {
return yield next;
}
if (is.generatorFunction(roles.failureHandler)) {
yield roles.failureHandler.call(this, action);
} else {
roles.failureHandler.call(this, action);
}
};
};
/**
* @see KoaRoles#can
* @method KoaRoles#is
*/
KoaRoles.prototype.is = KoaRoles.prototype.can;
/**
* @method KoaRoles#test
* @param {Context} ctx
* @param {String} action
*/
KoaRoles.prototype.test = function *(ctx, action) {
for (var i = 0; i < this.functionList.length; i++){
var fn = this.functionList[i];
var vote = null;
if (is.generatorFunction(fn)) {
vote = yield fn.call(ctx, action);
} else {
vote = fn.call(ctx, action);
}
if (typeof vote === 'boolean') {
return vote;
}
}
return false;
};
/**
* @method KoaRoles#middleware
*/
KoaRoles.prototype.middleware = function (options) {
options = options || {};
var userProperty = options.userProperty || this.userProperty;
var roles = this;
return function *(next) {
var ctx = this;
var roleCheck = tester(roles, ctx);
if (ctx[userProperty]) {
ctx[userProperty].is = ctx[userProperty].can = roleCheck;
if (this.locals && !this.locals[userProperty]) {
this.locals[userProperty] = this[userProperty];
}
}
this.userIs = this.userCan = roleCheck;
yield next;
};
};
function tester(roles, ctx) {
return function* (action) {
return yield roles.test(ctx, action);
};
}
function defaultFailureHandler(action) {
this.status = 403;
var t = this.accepts('json', 'html');
if (t === 'json') {
this.body = {
message: 'Access Denied - You don\'t have permission to: ' + action
};
} else {
this.body = 'Access Denied - You don\'t have permission to: ' + action;
}
}