{"name":"egg-userrole","version":"2.0.0","description":"user role plugin for egg","eggPlugin":{"name":"userrole"},"keywords":["egg","plugin","userrole"],"files":["config","app.js"],"dependencies":{"koa-roles":"^2.0.0"},"devDependencies":{"autod":"^3.0.1","egg":"^2.0.0","egg-bin":"^4.3.5","egg-ci":"^1.8.0","egg-mock":"^3.13.1","eslint":"^4.11.0","eslint-config-egg":"^5.1.1"},"engines":{"node":">=8.0.0"},"scripts":{"test":"npm run lint -- --fix && npm run test-local","test-local":"egg-bin test","cov":"egg-bin cov","lint":"eslint .","ci":"npm run lint && npm run cov","autod":"autod"},"ci":{"version":"8, 9"},"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":"af185efade5bf626c3f170b1d647296878e16b48","_id":"egg-userrole@2.0.0","_npmVersion":"5.5.1","_nodeVersion":"8.4.0","_npmUser":{"name":"dead_horse","email":"dead_horse@qq.com"},"dist":{"integrity":"sha512-eWaCT/5eL7BlvfzTf/keIhzVHSAGaP/S7h6cm9BDV7k/28eWIqMR4trUXKPlT2Hdq0JOlpGA8tXhKQnimUEFFQ==","shasum":"5347b3fc0a3f11c50fec219c135c6f7072bccf80","tarball":"http://registry-npm.ywwl.com/egg-userrole/download/egg-userrole-2.0.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIFIjExCxmv6Y+TmddBglO/B+WCOSXb0cm3XDwwCUHyE4AiBJcsUnt/wg4uqns2YRyRoNL2fSyggxWPZCuIphy986eQ=="}],"size":3187,"noattachment":false,"key":"/egg-userrole/-/egg-userrole-2.0.0.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"},{"name":"shaoshuai0102","email":"shaoshuai0102@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/egg-userrole-2.0.0.tgz_1511431781433_0.7576571542304009"},"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":1511431781540,"_cnpm_publish_time":1511431781540,"dist-tags":{"latest":"2.1.0"}}