{"id":13779562,"url":"https://github.com/koajs/koa-roles","last_synced_at":"2025-04-13T05:30:24.690Z","repository":{"id":15442155,"uuid":"18174940","full_name":"koajs/koa-roles","owner":"koajs","description":"koa version of Connect-Roles","archived":false,"fork":false,"pushed_at":"2019-07-03T18:59:13.000Z","size":54,"stargazers_count":118,"open_issues_count":2,"forks_count":10,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-29T15:32:20.365Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/koajs.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-27T12:24:09.000Z","updated_at":"2024-07-20T00:07:44.000Z","dependencies_parsed_at":"2022-08-26T05:20:49.579Z","dependency_job_id":null,"html_url":"https://github.com/koajs/koa-roles","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fkoa-roles","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fkoa-roles/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fkoa-roles/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/koajs%2Fkoa-roles/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/koajs","download_url":"https://codeload.github.com/koajs/koa-roles/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601446,"owners_count":20964866,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T18:01:06.477Z","updated_at":"2025-04-13T05:30:24.669Z","avatar_url":"https://github.com/koajs.png","language":"JavaScript","funding_links":[],"categories":["Middleware","仓库"],"sub_categories":["中间件"],"readme":"koa-roles\n=======\n\n[![NPM version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![Test coverage][coveralls-image]][coveralls-url]\n[![Gittip][gittip-image]][gittip-url]\n[![David deps][david-image]][david-url]\n[![npm download][download-image]][download-url]\n\n[npm-image]: https://img.shields.io/npm/v/koa-roles.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/koa-roles\n[travis-image]: https://img.shields.io/travis/koajs/koa-roles.svg?style=flat-square\n[travis-url]: https://travis-ci.org/koajs/koa-roles\n[coveralls-image]: https://img.shields.io/coveralls/koajs/koa-roles.svg?style=flat-square\n[coveralls-url]: https://coveralls.io/r/koajs/koa-roles?branch=master\n[gittip-image]: https://img.shields.io/gittip/fengmk2.svg?style=flat-square\n[gittip-url]: https://www.gittip.com/fengmk2/\n[david-image]: https://img.shields.io/david/koajs/koa-roles.svg?style=flat-square\n[david-url]: https://david-dm.org/koajs/koa-roles\n[download-image]: https://img.shields.io/npm/dm/koa-roles.svg?style=flat-square\n[download-url]: https://npmjs.org/package/koa-roles\n\nkoa version of [connect-roles](https://github.com/ForbesLindesay/connect-roles)\n\n## Install\n\n```bash\n$ npm install koa-roles\n```\n\n## Usage\n\n```js\nconst Roles = require('koa-roles');\nconst Koa = require('koa');\nconst Router = require('koa-router');\n\nconst app = new Koa();\nconst router = new Router();\nconst user = new Roles({\n  async failureHandler(ctx, action) {\n    // optional function to customise code that runs when\n    // user fails authorisation\n    ctx.status = 403;\n    var t = ctx.accepts('json', 'html');\n    if (t === 'json') {\n      ctx.body = {\n        message: 'Access Denied - You don\\'t have permission to: ' + action\n      };\n    } else if (t === 'html') {\n      ctx.render('access-denied', {action: action});\n    } else {\n      ctx.body = 'Access Denied - You don\\'t have permission to: ' + action;\n    }\n  }\n});\n\napp.use(user.middleware());\napp.use(router.routes())\n  .use(router.allowedMethods());\n  \n// anonymous users can only access the home page\n// returning false stops any more rules from being\n// considered\nuser.use(async (ctx, action) =\u003e {\n  return ctx.user || action === 'access home page';\n});\n\n// moderator users can access private page, but\n// they might not be the only ones so we don't return\n// false if the user isn't a moderator\nuser.use('access private page', ctx =\u003e {\n  if (ctx.user.role === 'moderator') {\n    return true;\n  }\n})\n\n//admin users can access all pages\nuser.use((ctx, action) =\u003e {\n  if (ctx.user.role === 'admin') {\n    return true;\n  }\n});\n\nrouter.get('/', user.can('access home page'), async ctx =\u003e {\n  await ctx.render('private');\n});\nrouter.get('/private', user.can('access private page'), async ctx =\u003e {\n  await ctx.render('private');\n});\nrouter.get('/admin', user.can('access admin page'), async ctx =\u003e {\n  await ctx.render('admin');\n});\n\napp.listen(3000);\n```\n\n## License\n\n[MIT](LICENSE.txt)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fkoa-roles","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkoajs%2Fkoa-roles","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkoajs%2Fkoa-roles/lists"}