eggjs/egg-userrole

Name: egg-userrole

Owner: egg

Description: user role plugin for egg

Created: 2016-07-17 08:45:13.0

Updated: 2018-05-18 08:39:17.0

Pushed: 2017-11-23 10:09:35.0

Homepage: null

Size: 20

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

egg-userrole

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Provide dynamic roles based authorisation. Use koa-roles.

Install
m i egg-userrole --save
Usage
app_root}/config/plugin.js
rts.userrole = {
ckage: 'egg-userrole',

Recommend to use along with custom userservice plugin (which provide ctx.user).

see egg-userservice for more info.

Build-in

Roles build-in failureHandler:

tion failureHandler(ctx, action) {
nst message = 'Forbidden, required role: ' + action;
 (ctx.acceptJSON) {
ctx.body = {
  message: message,
  stat: 'deny',
};
else {
ctx.status = 403;
ctx.body = message;


Build-in user role define:

role.use('user', ctx => !!ctx.user);
How to custom failureHandler

Define app.role.failureHandler(action) method in config/role.js

app_root}/config/role.js or {framework_root}/config/role.js
le.exports = app => {
p.role.failureHandler = function(ctx, action) {
if (ctx.acceptJSON) {
  ctx.body = { target: loginURL, stat: 'deny' };
} else {
  ctx.realStatus = 200;
  ctx.redirect(loginURL);
}


How to custom role
app_root}/config/role.js or {framework_root}/config/role.js
le.exports = function(app) {
p.role.use('admin', ctx => {
return ctx.user && ctx.user.isAdmin;
;

p.role.use('can write', async ctx => {
const post = await ctx.service.post.fetch(ctx.request.body.id);
return ctx.user.name === post.author;
;

Questions & Suggestions

Please open an issue here.

License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.