{"name":"egg-userrole","version":"0.0.2","description":"user role plugin for egg","eggPlugin":{"name":"userrole"},"keywords":["egg","plugin","userrole"],"dependencies":{"koa-roles":"^1.0.2"},"devDependencies":{"autod":"2","egg":"^0.0.4","egg-bin":"1","egg-ci":"1","egg-mock":"^0.0.1","eslint":"3","eslint-config-egg":"3","should":"8","supertest":"1"},"engines":{"node":">=4.0.0"},"scripts":{"test":"npm run lint && npm run test-local","test-local":"egg-bin test","cov":"egg-bin cov","lint":"eslint --ext js . --fix","ci":"npm run lint && npm run cov","autod":"autod"},"ci":{"version":"4, 6"},"repository":{"type":"git","url":"git+https://github.com/eggjs/egg-userrole.git"},"bugs":{"url":"https://github.com/eggjs/egg/issues"},"homepage":"https://github.com/eggjs/egg-userrole#readme","author":{"name":"TZ","email":"atian25@qq.com"},"license":"MIT","gitHead":"70be1564e17d1d7ae32cfa6d50828b3d5a414541","_id":"egg-userrole@0.0.2","_shasum":"244bd0fc54d662fd3ca317f2aadb0dbb5dd81d0f","_from":".","_npmVersion":"2.15.1","_nodeVersion":"4.4.4","_npmUser":{"name":"atian25","email":"atian25@qq.com"},"dist":{"shasum":"244bd0fc54d662fd3ca317f2aadb0dbb5dd81d0f","tarball":"http://registry-npm.ywwl.com/egg-userrole/download/egg-userrole-0.0.2.tgz","integrity":"sha512-n2U5vquCiaxzAZxgm/iI5izr4I6M2KprbO7iTU24GAuSgNXnleUzhRM6TPCb5dieh4SK7VlspBnq1djYILiN+A==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDAn8zTIqZBM3wLKplp1Gl/TqXe4aZHrw3lTRCM3brMHAIhAK7qQ8Pzbgkmhbb11XPfzQhw6+V5Thb/h5vxwl3D9TfB"}],"size":12453,"noattachment":false,"key":"/egg-userrole/-/egg-userrole-0.0.2.tgz"},"maintainers":[{"name":"atian25","email":"atian25@qq.com"},{"name":"dead_horse","email":"dead_horse@qq.com"},{"name":"eggjs","email":"fengmk2+eggjs@gmail.com"},{"name":"fengmk2","email":"fengmk2@gmail.com"},{"name":"popomore","email":"sakura9515@gmail.com"}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/egg-userrole-0.0.2.tgz_1468767477755_0.6974917994812131"},"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":1468767478002,"_cnpm_publish_time":1468767478002,"dist-tags":{"latest":"2.1.0"}}