You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.7 KiB
JavaScript
50 lines
1.7 KiB
JavaScript
5 years ago
|
const
|
||
|
opcount = require('../model/opcount'),
|
||
|
{ PermissionError, PrivilegeError } = require('../error');
|
||
|
|
||
|
module.exports = {
|
||
|
requirePerm(perm) {
|
||
|
return async (ctx, next) => {
|
||
|
for (let i in arguments) {
|
||
|
if (arguments[i] instanceof Array) {
|
||
|
let p = false;
|
||
|
for (let j in arguments)
|
||
|
if (ctx.state.user.hasPerm(arguments[i][j])) {
|
||
|
p = true;
|
||
|
break;
|
||
|
}
|
||
|
if (!p) throw new PermissionError([arguments[i]]);
|
||
|
} else {
|
||
|
if (ctx.state.user.hasPerm(arguments[i])) continue;
|
||
|
else throw new PermissionError([arguments[i]]);
|
||
|
}
|
||
|
}
|
||
|
await next();
|
||
|
};
|
||
|
},
|
||
|
requirePriv(priv) {
|
||
|
return async (ctx, next) => {
|
||
|
for (let i in arguments) {
|
||
|
if (arguments[i] instanceof Array) {
|
||
|
let p = false;
|
||
|
for (let j in arguments)
|
||
|
if (ctx.state.user.hasPriv(arguments[i][j])) {
|
||
|
p = true;
|
||
|
break;
|
||
|
}
|
||
|
if (!p) throw new PrivilegeError([arguments[i]]);
|
||
|
} else {
|
||
|
if (ctx.state.user.hasPriv(arguments[i])) continue;
|
||
|
else throw new PrivilegeError([arguments[i]]);
|
||
|
}
|
||
|
}
|
||
|
await next();
|
||
|
};
|
||
|
},
|
||
|
limitRate(op, period_secs, max_operations) {
|
||
|
return async (ctx, next) => {
|
||
|
await opcount.inc(op, ctx.request.ip, period_secs, max_operations);
|
||
|
await next();
|
||
|
};
|
||
|
}
|
||
|
};
|