chore: update travis (#11)
Provide dynamic roles based authorisation. Use koa-roles.
$ npm i egg-userrole --save
// {app_root}/config/plugin.js exports.userrole = { package: 'egg-userrole', };
Recommend to use along with custom userservice plugin (which provide ctx.user). see egg-userservice for more info.
Recommend to use along with custom userservice plugin (which provide ctx.user).
userservice
ctx.user
see egg-userservice for more info.
egg-userservice
Roles build-in failureHandler:
Roles
failureHandler
function failureHandler(ctx, action) { const message = 'Forbidden, required role: ' + action; if (ctx.acceptJSON) { ctx.body = { message: message, stat: 'deny', }; } else { ctx.status = 403; ctx.body = message; } };
Build-in user role define:
user
app.role.use('user', ctx => !!ctx.user);
Define app.role.failureHandler(action) method in config/role.js
app.role.failureHandler(action)
config/role.js
app/extend/context.js
// {app_root}/config/role.js or {framework_root}/config/role.js module.exports = app => { app.role.failureHandler = function(ctx, action) { if (ctx.acceptJSON) { ctx.body = { target: loginURL, stat: 'deny' }; } else { ctx.realStatus = 200; ctx.redirect(loginURL); } }; }
// {app_root}/config/role.js or {framework_root}/config/role.js module.exports = function(app) { app.role.use('admin', ctx => { return ctx.user && ctx.user.isAdmin; }); app.role.use('can write', async ctx => { const post = await ctx.service.post.fetch(ctx.request.body.id); return ctx.user.name === post.author; }); };
Please open an issue here.
MIT
版权所有:中国计算机学会技术支持:开源发展技术委员会 京ICP备13000930号-9 京公网安备 11010802032778号
egg-userrole
Provide dynamic roles based authorisation. Use koa-roles.
Install
Usage
Build-in
Rolesbuild-infailureHandler:Build-in
userrole define:How to custom
failureHandlerDefine
app.role.failureHandler(action)method inconfig/role.jsapp/extend/context.jsHow to custom role
Questions & Suggestions
Please open an issue here.
License
MIT