{"id":24879831,"url":"https://github.com/klren0312/egg-zrole","last_synced_at":"2025-10-15T22:31:19.488Z","repository":{"id":34918152,"uuid":"190141224","full_name":"klren0312/egg-zrole","owner":"klren0312","description":"EggJS casbin plugin","archived":false,"fork":false,"pushed_at":"2024-11-07T02:38:23.000Z","size":1404,"stargazers_count":18,"open_issues_count":0,"forks_count":10,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-05T21:46:04.992Z","etag":null,"topics":["casbin","egg-plugin","eggjs","rbac"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/klren0312.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-04T06:19:46.000Z","updated_at":"2024-11-07T02:17:53.000Z","dependencies_parsed_at":"2024-06-21T15:21:27.264Z","dependency_job_id":"5b669bc2-7eee-4a8b-b0a9-aecb7715635d","html_url":"https://github.com/klren0312/egg-zrole","commit_stats":{"total_commits":59,"total_committers":2,"mean_commits":29.5,"dds":"0.23728813559322037","last_synced_commit":"86d50f0338568c71fbab2321f5d7d1339e1c0f51"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klren0312%2Fegg-zrole","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klren0312%2Fegg-zrole/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klren0312%2Fegg-zrole/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klren0312%2Fegg-zrole/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/klren0312","download_url":"https://codeload.github.com/klren0312/egg-zrole/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":236646159,"owners_count":19182611,"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":["casbin","egg-plugin","eggjs","rbac"],"created_at":"2025-02-01T10:17:52.057Z","updated_at":"2025-10-15T22:31:14.154Z","avatar_url":"https://github.com/klren0312.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# egg-zrole\n\n[![NPM version][npm-image]][npm-url]\n[![Test coverage][codecov-image]][codecov-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-zrole.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/egg-zrole\n[codecov-image]: https://img.shields.io/codecov/c/github/klren0312/egg-zrole.svg?style=flat-square\n[codecov-url]: https://codecov.io/github/klren0312/egg-zrole?branch=master\n[snyk-image]: https://snyk.io/test/npm/egg-zrole/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/egg-zrole\n[download-image]: https://img.shields.io/npm/dm/egg-zrole.svg?style=flat-square\n[download-url]: https://npmjs.org/package/egg-zrole\n\n\u003c!--\nDescription here.\n--\u003e\n\n## Install\n\n```bash\n$ npm i egg-zrole --save\n```\n\n## Usage\n\n```js\n// {app_root}/config/plugin.js\nexports.zrole = {\n  enable: true,\n  package: 'egg-zrole',\n};\n```\n\n## Configuration\n\n```js\n// {app_root}/config/config.default.js\nexports.zrole = {\n  useAdapter: false,\n  useAnonymous: false,\n  usePolicyInit: false,\n  useCustomResponse: false,\n  model: '/example/zrole_model.conf',\n  policy: '/example/zrole_policy.csv',\n  adapterConfig: () =\u003e {},\n  getUser: (ctx) =\u003e {},\n  initPolicy: () =\u003e {},\n  customResponse: (ctx) =\u003e {},\n  useAutoMiddleware: true,\n  useSuperManage: 'admin'\n};\n```\n\n**Tips:**\n\n - After `v1.0.5` you don't need to add the `zrole` to middleware.\n - You must set the `model` path; When you don't use the adapter, you also need to set `policy` path.\n - If your userinfo not in the `Authorization`, you should use `getUser` method to set how to get userinfo that can check the user role.**If don't set the getUser method, it will jump.**\n - If use some casbin adapter, you need make `useAdapter` to `true`, then config the adapter, use `adapterConfig` method.\n - If you need to init the policy, you can set `usePolicyInit` to `true`, and use `initPolicy` method to set role.\n - If you need to custom your response, when 403; You can set `useCustomResponse` to `true`, and use `customResponse` method to custom the response.\n - If you need to use default `anonymous` role, you can set `useAnonymous` to `true`.\n - In `v1.3.0`, you can set `useAutoMiddleware` to false (default is true), then the zrole middleware will not add to your middleware array, you need to write middleware yourself.\n - In `v1.5.0`, you can set super manage name to jump role check.\n - After `v2.0.2`, support the `keyMatch5` matcher.\n - In `v3.0.0`, only support Nodejs v16.0.0+\n\nsee [config/config.default.js](config/config.default.js) for more detail.\n\n## Example\n\u003e Details Project Later\n\nNow, You can see [test/fixtures](test/fixtures), there are two example\n\n### 1.[test/fixtures/zrole-sequelize-test](test/fixtures/zrole-sequelize-test).\n\u003e this test project, show the following features: \n\u003e1.sequelize adapter; 2.init policy\n\n - 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`;\n - The casbin sequelize adapter, we use `casbin-sequelize-adapter`, about it, you can see https://github.com/node-casbin/sequelize-adapter\n - 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`\n - If you want to use own database, you can set `adapterConfig`:\n\n```javascript\n// example config.default.js\nexports.zrole = {\n  useAdapter: true,\n  usePolicyInit: true,\n  model: './example/zrole_model.conf',\n  policy: './example/zrole_policy.csv',\n  getUser: ctx =\u003e {\n    if (ctx.headers.authorization) {\n      return ctx.headers.authorization;\n    }\n    return null;\n  },\n  adapterConfig: async () =\u003e {\n    const connect = await SequelizeAdapter.newAdapter(\n      {\n        host: 'localhost',\n        port: 3306,\n        database: 'test',\n        username: 'root',\n        password: 'root',\n        dialect: 'mysql',\n      },\n      true\n    );\n    return connect;\n  },\n  initPolicy: zrole =\u003e {\n    zrole.addPolicy('xdd', '/', 'GET');\n    zrole.addPolicy('xdd', '/remove', 'GET');\n  },\n};\n```\n\n### 2.[test/fixtures/zrole-test](test/fixtures/zrole-test).\n\u003e this test project, show the following features: \n\u003e1.anonymous; 2.custom response; 3.multi roles check;4.super manage\n\nmodel and policy use the fixed file\nIf you set `useAnonymous` to `true`, the request that has no header(Authorization) will be the `anonymous` user. It will access the `anonymous` api, like,\n```\np, anonymous, /anonymous, GET\n```\n\n```javascript\n// example\nexports.zrole = {\n  useAnonymous: true,\n  useCustomResponse: true,\n  model: './example/zrole_model.conf',\n  policy: './example/zrole_policy.csv',\n  getUser: ctx =\u003e {\n    if (ctx.headers.authorization) {\n      return ctx.headers.authorization;\n    }\n    return null;\n  },\n  customResponse: ctx =\u003e {\n    ctx.status = 403;\n    ctx.body = 'Your do not has permission to access';\n  },\n  useSuperManage: 'admin'\n};\n```\n\n### 3.[test/fixtures/zrole-no-auto-add-middleware-test](test/fixtures/zrole-no-auto-add-middleware-test).\n\u003e this test project, show the following features: \n\u003e1.use custom middleware\n\n\n```javascript\n// example\nexports.zrole = {\n  useAutoMiddleware: false,\n  model: './example/zrole_model.conf',\n  policy: './example/zrole_policy.csv',\n};\n```\n\n## Questions \u0026 Suggestions\n\nPlease open an issue [here](https://github.com/klren0312/egg-zrole).\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklren0312%2Fegg-zrole","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fklren0312%2Fegg-zrole","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklren0312%2Fegg-zrole/lists"}