{"id":20844800,"url":"https://github.com/nest-cloud/rbac","last_synced_at":"2025-06-25T02:37:40.754Z","repository":{"id":43982285,"uuid":"245072103","full_name":"nest-cloud/rbac","owner":"nest-cloud","description":null,"archived":false,"fork":false,"pushed_at":"2022-12-10T20:31:24.000Z","size":581,"stargazers_count":1,"open_issues_count":14,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-25T01:42:40.719Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/nest-cloud.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}},"created_at":"2020-03-05T04:54:39.000Z","updated_at":"2020-09-15T10:29:31.000Z","dependencies_parsed_at":"2023-01-26T07:45:35.100Z","dependency_job_id":null,"html_url":"https://github.com/nest-cloud/rbac","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nest-cloud/rbac","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nest-cloud%2Frbac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nest-cloud%2Frbac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nest-cloud%2Frbac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nest-cloud%2Frbac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nest-cloud","download_url":"https://codeload.github.com/nest-cloud/rbac/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nest-cloud%2Frbac/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261792283,"owners_count":23210287,"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-11-18T02:11:00.093Z","updated_at":"2025-06-25T02:37:40.731Z","avatar_url":"https://github.com/nest-cloud.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n[travis-image]: https://api.travis-ci.org/nest-cloud/nestcloud.svg?branch=master\n[travis-url]: https://travis-ci.org/nest-cloud/nestcloud\n[linux-image]: https://img.shields.io/travis/nest-cloud/nestcloud/master.svg?label=linux\n[linux-url]: https://travis-ci.org/nest-cloud/nestcloud\n\n# NestCloud - Rbac\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://www.npmjs.com/~nestcloud\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/@nestcloud/rbac.svg\" alt=\"NPM Version\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://www.npmjs.com/~nestcloud\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/l/@nestcloud/rbac.svg\" alt=\"Package License\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://www.npmjs.com/~nestcloud\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/dm/@nestcloud/rbac.svg\" alt=\"NPM Downloads\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://travis-ci.org/nest-cloud/nestcloud\" target=\"_blank\"\u003e\u003cimg src=\"https://travis-ci.org/nest-cloud/nestcloud.svg?branch=master\" alt=\"Travis\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://travis-ci.org/nest-cloud/nestcloud\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/travis/nest-cloud/nestcloud/master.svg?label=linux\" alt=\"Linux\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://coveralls.io/github/nest-cloud/nestcloud?branch=master\" target=\"_blank\"\u003e\u003cimg src=\"https://coveralls.io/repos/github/nest-cloud/nestcloud/badge.svg?branch=master\" alt=\"Coverage\"/\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## Description\n\nProvides rbac for nestcloud.\n\n## Installation\n\n```bash\n$ npm i --save @nestcloud/rbac\n```\n\n## Usage\n\n```typescript\nimport { Injectable, Module } from '@nestjs/common';\nimport { NEST_BOOT, NEST_CONSUL } from '@nestcloud/common';\nimport { BootModule } from '@nestcloud/boot';\nimport { ConsulModule } from '@nestcloud/consul';\nimport { RbacModule, UseValidators, ConsulValidator } from '@nestcloud/rbac';\nimport { HeroController } from './hero.controller';\n\n@Injectable()\n@UseValidators(ConsulValidator)\nexport class ValidatorRegister {\n}\n\n@Module({\n    imports: [\n        BootModule.register(__dirname, `config.yaml`),\n        ConsulModule.register({ dependencies: [NEST_BOOT] }),\n        RbacModule.register({\n            dependencies: [NEST_CONSUL, NEST_BOOT],\n        })\n    ],\n    controllers: [HeroController],\n    providers: [ValidatorRegister],\n})\nexport class AppModule {\n}\n```\n\n### Boot Configuration\n\n```yaml\nconsul:\n  host: localhost\n  port: 8500\nrbac:\n  parameters:\n    name: service-rbac\n```\n\n### Rbac Configuration\n\nThe Rbac configuration has three kinds: Account, Role, RoleBinding and use '---' split these.\n\nPlease set the rbac configuration into consul kv named service-rbac.\n\n```yaml\nkind: Account\nname: test\n\n---\n\nkind: Role\nname: admin\nrules:\n  - resources: [\"user\"]\n    verbs: [\"get\", \"list\"]\n    \n---\n\nkind: RoleBinding\nrole: admin\naccounts:\n  - test\n```\n\n### Write A Guard\n\nPut a user object into request instance. The RbacGuard need it for permission validation.\n\n```typescript\nimport { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';\nimport { IRbacAccount } from '@nestcloud/rbac';\n\n@Injectable()\nexport class AccountGuard implements CanActivate {\n    canActivate(context: ExecutionContext): boolean {\n        const request = context.switchToHttp().getRequest();\n        request.user = { name: request.query.user } as IRbacAccount;\n        return true;\n    }\n}\n\n```\n\n### Define Resource And Verb in Controller\n\nYour custom AccountGuard must be set before RbacGuard.\n\n```typescript\nimport { Controller, Get, Param, UseGuards } from '@nestjs/common';\nimport { AccountGuard } from './account.guard';\nimport { RbacGuard, Resource, Verb, Verbs } from '@nestcloud/rbac';\n\n@Controller('/users')\n@Resource('user')\n@UseGuards(AccountGuard, RbacGuard)\nexport class HeroController {\n    @Get('/:userId')\n    @Verb(Verbs.GET)\n    async get(@Param('heroId') heroId: number): Promise\u003cany\u003e {\n        return { user: 'Shadow hunter' };\n    }\n\n    @Get('/')\n    @Verb(Verbs.LIST)\n    async list(): Promise\u003cany\u003e {\n        return { users: ['Shadow hunter', 'BladeMaster'] };\n    }\n}\n```\n\n## Use Custom Validator\n\nThe rbac component use ConsulValidator as default validator,\n if you don't want to use consul as storage backend, \n you can write a custom validator.\n \n```typescript\nimport { IRbacValidator } from \"./interfaces/rbac-validator.interface\";\nimport { IRbacAccount } from \"./interfaces/rbac-account.interface\";\nimport { IRbacRole } from \"./interfaces/rbac-role.interface\";\nimport { IRbacRoleBinding } from \"./interfaces/rbac-role-binding.interface\";\nimport { Store } from \"./store\";\nimport { IRbacOptions } from \"./interfaces/rbac-options.interface\";\n\nexport class CustomValidator implements IRbacValidator {\n    private readonly store: Store = new Store();\n\n    /**\n    * \n    * @param options\n    * @param client \n    * If set config.backend to Backend.CONSUl, the client will be consul instance;\n    * if set config.backend to Backend.LOADBALANCE, the client will be loadbalance instance;\n    * if set config.backend to Backend.CUSTOM or not set, the client will be null.\n    */\n    public async init(options: IRbacOptions, client?: any) {\n        const roles: IRbacRole[] = [];\n        const accounts: IRbacAccount[] = [];\n        const roleBindings: IRbacRoleBinding[] = [];\n        this.store.init(accounts, roles, roleBindings);\n    }\n    \n    public validate(resource: string, verb: string, account: IRbacAccount): boolean {\n        return this.store.validate(account.name, resource, verb);\n    }\n}\n\n```\n\n## Stay in touch\n\n- Author - [NestCloud](https://github.com/nest-cloud)\n\n## License\n\n  NestCloud is [MIT licensed](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnest-cloud%2Frbac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnest-cloud%2Frbac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnest-cloud%2Frbac/lists"}