{"name":"egg-userrole","version":"0.0.1","description":"egg plugin <egg-userrole>","keywords":["egg","eggjs","egg-plugin"],"homepage":"https://github.com/eggjs/egg-userrole","license":"MIT","author":{"name":"eggjs"},"contributors":[{"name":"TZ","email":"atian25@qq.com"},{"name":"popomore","email":"sakura9515@gmail.com"},{"name":"fengmk2","email":"m@fengmk2.com"},{"name":"dead-horse","email":"heyiyu.deadhorse@gmail.com"}],"repository":{"type":"git","url":"git+ssh://git@github.com/eggjs/egg-userrole.git"},"bugs":{"url":"https://github.com/eggjs/egg-userrole/issues"},"_id":"egg-userrole@0.0.1","scripts":{},"_shasum":"35f23bcdf0fb7120a2372e520c36ff3cd8184822","_from":".","_npmVersion":"2.15.1","_nodeVersion":"4.4.4","_npmUser":{"name":"atian25","email":"atian25@qq.com"},"dist":{"shasum":"35f23bcdf0fb7120a2372e520c36ff3cd8184822","tarball":"http://registry-npm.ywwl.com/egg-userrole/download/egg-userrole-0.0.1.tgz","integrity":"sha512-M7IQ8qUwvzyIXEatQGL827UzC4bsUYdzzBCfUKiugMPSxZ431JmFxW+i/aI10KkUdaRKQiDO5uJKdKs1mJ76ww==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCU6Sz3JtCdKgN9z17bnozdZC9LKpzW+W+6YJbfMHnpqgIhALBi08DEulezL0BO4loecaHEgB3XQaQqxswauK8Njjd7"}],"size":428,"noattachment":false,"key":"/egg-userrole/-/egg-userrole-0.0.1.tgz"},"maintainers":[{"name":"atian25","email":"atian25@qq.com"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/egg-userrole-0.0.1.tgz_1464253780040_0.35513931466266513"},"directories":{},"readme":"# egg-userrole\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][codecov-image]][codecov-url]\n[![David deps][david-image]][david-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/egg-userrole.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/egg-userrole\n[travis-image]: https://img.shields.io/travis/eggjs/egg-userrole.svg?style=flat-square\n[travis-url]: https://travis-ci.org/eggjs/egg-userrole\n[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-userrole.svg?style=flat-square\n[codecov-url]: https://codecov.io/github/eggjs/egg-userrole?branch=master\n[david-image]: https://img.shields.io/david/eggjs/egg-userrole.svg?style=flat-square\n[david-url]: https://david-dm.org/eggjs/egg-userrole\n[snyk-image]: https://snyk.io/test/npm/egg-userrole/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/egg-userrole\n[download-image]: https://img.shields.io/npm/dm/egg-userrole.svg?style=flat-square\n[download-url]: https://npmjs.org/package/egg-userrole\n\nProvide dynamic roles based authorisation. Use [koa-roles](https://github.com/koajs/koa-roles).\n\n## Install\n\n```bash\n$ npm i egg-userrole --save\n```\n\n## Usage\n```javascript\n// {app_root}/config/plugin.js\nexports.userrole = {\n  package: 'egg-userrole',\n};\n```\n\n> Recommend to use along with custom `userservice` plugin (which provide `ctx.user`).\n>\n> see [`egg-userservice`](https://github.com/eggjs/egg-userservice) for more info.\n\n### Build-in\n\n`Roles` build-in `failureHandler`:\n\n```javascript\nfunction failureHandler(ctx, action) {\n  const message = 'Forbidden, required role: ' + action;\n  if (ctx.acceptJSON) {\n    ctx.body = {\n      message: message,\n      stat: 'deny',\n    };\n  } else {\n    ctx.status = 403;\n    ctx.body = message;\n  }\n};\n```\n\nBuild-in `user` role define:\n\n```javascript\napp.role.use('user', ctx => !!ctx.user);\n```\n\n### How to custom `failureHandler`\n\nDefine `app.role.failureHandler(action)` method in `config/role.js`\n\n- `app/extend/context.js`\n\n```javascript\n// {app_root}/config/role.js or {framework_root}/config/role.js\nmodule.exports = app => {\n  app.role.failureHandler = function(ctx, action) {\n    if (ctx.acceptJSON) {\n      ctx.body = { target: loginURL, stat: 'deny' };\n    } else {\n      ctx.realStatus = 200;\n      ctx.redirect(loginURL);\n    }\n  };\n}\n```\n\n### How to custom role\n\n```javascript\n// {app_root}/config/role.js or {framework_root}/config/role.js\nmodule.exports = function(app) {\n  app.role.use('admin', ctx => {\n    return ctx.user && ctx.user.isAdmin;\n  });\n\n  app.role.use('can write', async ctx => {\n    const post = await ctx.service.post.fetch(ctx.request.body.id);\n    return ctx.user.name === post.author;\n  });\n};\n```\n\n## Questions & Suggestions\n\nPlease open an issue [here](https://github.com/eggjs/egg/issues).\n\n## License\n\n[MIT](https://github.com/eggjs/egg-userrole/blob/master/LICENSE)\n","publish_time":1464253780587,"_cnpm_publish_time":1464253780587,"dist-tags":{"latest":"2.1.0"}}