Skip to content
EggJS casbin plugin
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
app/middleware
config feat(add anonymous user): Jun 13, 2019
example
test
.autod.conf.js
.eslintignore
.eslintrc
.gitignore
.travis.yml
LICENSE feat(plugin init): Jun 4, 2019
README.md
README.zh_CN.md
app.js feat(add `zrole` to the middleware by default): Jun 5, 2019
appveyor.yml
package-lock.json
package.json
yarn.lock feat(add sequlize test): Jun 4, 2019

README.md

egg-zrole

NPM version Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-zrole --save

Usage

// {app_root}/config/plugin.js
exports.zrole = {
  enable: true,
  package: 'egg-zrole',
};

Configuration

// {app_root}/config/config.default.js
exports.zrole = {
  useAdapter: false,
  model: '/example/zrole_model.conf',
  policy: '/example/zrole_policy.csv',
  adapterConfig: () => {}
};

Tips:

  • If use some casbin adapter, you need make useAdapter to true, then config the adapterConfig.
  • in > 1.0.5 you don't need to add the zrole to middleware.

see config/config.default.js for more detail.

Example

Details Project Later

Now, You can see test/fixtures, there are two example

1.test/fixtures/zrole-sequelize-test.

  • Use Sequlize and MySQL to control permission, in controller file, you can see this.app.zrole.addPolicy('xdd', '/', 'GET'), it test the policy's dynamic addition; and you need to set useAdapter to true;
  • The casbin sequelize adapter, we use casbin-sequelize-adapter, about it, you can see https://github.com/node-casbin/sequelize-adapter
  • It will auto create the database that name is casbin, when you don't set the database, and don't set SequelizeAdapter.newAdapter second params to ture
  • If you want to use own database, you can set adapterConfig:
// example config.default.js
exports.zrole = {
  useAdapter: true,
  model: './example/zrole_model.conf',
  getUser(ctx) {
    if (ctx.headers.authorization) {
      return ctx.headers.authorization;
    }
    return null;
  },
  adapterConfig: async () => {
    const connect =  await SequelizeAdapter.newAdapter(`mysql://root:@localhost:3306/yourDatabase`, true)
    return connect
  }
};

2.test/fixtures/zrole-test.

model and policy use the fixed file If you set useAnonymous to true, the request that has no header(Authorization) will be the anonymous user. It will access the anonymous api, like,

p, anonymous, /anonymous, GET
// example
exports.zrole = {
  useAdapter: false,
  useAnonymous: true,
  model: './example/zrole_model.conf',
  policy: './example/zrole_policy.csv',
  getUser(ctx) {
    if (ctx.headers.authorization) {
      return ctx.headers.authorization;
    }
    return null;
  }
};

Questions & Suggestions

Please open an issue here.

License

MIT

You can’t perform that action at this time.