Code Monkey home page Code Monkey logo

egg-userrole's Introduction

egg-userrole

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

Provide dynamic roles based authorisation. Use koa-roles.

Install

$ npm i egg-userrole --save

Usage

// {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.

Build-in

Roles build-in 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:

app.role.use('user', ctx => !!ctx.user);

How to custom failureHandler

Define app.role.failureHandler(action) method in 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);
    }
  };
}

How to custom role

// {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;
  });
};

Questions & Suggestions

Please open an issue here.

License

MIT

egg-userrole's People

Contributors

atian25 avatar dead-horse avatar popomore avatar fengmk2 avatar whxaxes avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.