-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
193 lines (176 loc) · 4.77 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
'use strict';
const 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);
const old = this.actionMap[action];
// create or override
this.actionMap[action] = fn;
// action fn have already been used, skip
if (old) {
return;
}
const roles = this;
this.use1((ctx, act) => {
// get fn from actionMap
const fn = roles.actionMap[action];
if (act === action) {
return fn(ctx, act);
}
});
};
/**
* @method KoaRoles#can
* @param {String} action
*/
KoaRoles.prototype.can = function(action) {
const roles = this;
return async (ctx, next) => {
if (await roles.test(ctx, action)) {
return next();
}
const r = roles.failureHandler(ctx, action);
if (is.promise(r)) await r;
};
};
/**
* @see KoaRoles#can
* @method KoaRoles#is
*/
KoaRoles.prototype.is = KoaRoles.prototype.can;
/**
* @method KoaRoles#test
* @param {Context} ctx
* @param {String} action
*/
KoaRoles.prototype.test = async function(ctx, action) {
for (let i = 0; i < this.functionList.length; i++) {
const fn = this.functionList[i];
let vote = fn(ctx, action);
if (is.promise(vote)) vote = await vote;
if (typeof vote === 'boolean') return vote;
}
return false;
};
/**
* @method KoaRoles#middleware
*/
KoaRoles.prototype.middleware = function(options) {
options = options || {};
const userProperty = options.userProperty || this.userProperty;
const roles = this;
return (ctx, next) => {
const roleCheck = tester(roles, ctx);
if (ctx[userProperty]) {
ctx[userProperty].is = ctx[userProperty].can = roleCheck;
if (ctx.locals && !ctx.locals[userProperty]) {
ctx.locals[userProperty] = ctx[userProperty];
}
}
ctx.userIs = ctx.userCan = roleCheck;
return next();
};
};
function tester(roles, ctx) {
return action => roles.test(ctx, action);
}
function defaultFailureHandler(ctx, action) {
const message = `Access Denied - You don't have permission to: ${action}`;
ctx.body = ctx.accepts('json', 'html') === 'json' ? { message } : message;
ctx.status = 403;
}