{"id":23416132,"url":"https://github.com/knodescommunity/nest-casl","last_synced_at":"2025-04-12T08:52:12.449Z","repository":{"id":37012195,"uuid":"328672863","full_name":"KnodesCommunity/nest-casl","owner":"KnodesCommunity","description":"A module for managing authorization in your nest application, using casl.","archived":false,"fork":false,"pushed_at":"2025-04-07T18:29:19.000Z","size":1505,"stargazers_count":4,"open_issues_count":14,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-07T19:36:59.555Z","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/KnodesCommunity.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2021-01-11T13:28:33.000Z","updated_at":"2024-05-02T10:20:23.000Z","dependencies_parsed_at":"2023-10-16T08:20:09.862Z","dependency_job_id":"1fe03cfc-dde5-4722-848d-ffe1750e5430","html_url":"https://github.com/KnodesCommunity/nest-casl","commit_stats":{"total_commits":105,"total_committers":4,"mean_commits":26.25,"dds":0.3142857142857143,"last_synced_commit":"0459693321b932a18c20c058aa8ffe0f6879e21b"},"previous_names":["scitizen/nest-casl"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KnodesCommunity%2Fnest-casl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KnodesCommunity%2Fnest-casl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KnodesCommunity%2Fnest-casl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/KnodesCommunity%2Fnest-casl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/KnodesCommunity","download_url":"https://codeload.github.com/KnodesCommunity/nest-casl/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248543881,"owners_count":21121838,"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-12-22T22:13:42.714Z","updated_at":"2025-04-12T08:52:12.409Z","avatar_url":"https://github.com/KnodesCommunity.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @knodes/nest-casl\n\n\u003cp align=\"center\"\u003e\n\t\u003ca href=\"http://nestjs.com/\" target=\"_blank\"\u003e\u003cimg src=\"https://nestjs.com/img/logo-small.svg\" height=\"150\" alt=\"Nest Logo\" /\u003e\u003c/a\u003e\n\t\u003cspan style=\"font-size: 150px\"\u003e\u0026nbsp;+\u0026nbsp;\u003c/span\u003e\n\t\u003ca href=\"https://casl.js.org/v5/en\" target=\"_blank\"\u003e\u003cimg src=\"https://casl.js.org/v5/51b9cc103e05f66c.png\" height=\"150\" alt=\"CASL Logo\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003eA simple decorator-based way to check \u003ca href=\"https://casl.js.org/v5/en\" target=\"_blank\"\u003eCASL abilities\u003c/a\u003e on \u003ca href=\"http://nestjs.com/\" target=\"_blank\"\u003eNestJS\u003c/a\u003e controllers.\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\t\u003ca href=\"https://github.com/KnodesCommunity/nest-casl/issues\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/github/issues/KnodesCommunity/nest-casl\" alt=\"GitHub issues\" /\u003e\u003c/a\u003e\n\t\u003ca href=\"https://www.npmjs.com/package/@knodes/nest-casl\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/@knodes/nest-casl.svg\" alt=\"NPM version\" /\u003e\u003c/a\u003e\n\t\u003ca href=\"https://www.npmjs.com/package/@knodes/nest-casl\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/dm/@knodes/nest-casl.svg\" alt=\"NPM downloads\" /\u003e\u003c/a\u003e\n\t\u003ca href=\"https://www.npmjs.com/package/@knodes/nest-casl\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/npm/l/@knodes/nest-casl.svg\" alt=\"License\" /\u003e\u003c/a\u003e\n\t\u003ca href=\"https://circleci.com/gh/KnodesCommunity/nest-casl/tree/main\" target=\"_blank\"\u003e\u003cimg src=\"https://img.shields.io/circleci/build/github/KnodesCommunity/nest-casl/main\" alt=\"CircleCI\" /\u003e\u003c/a\u003e\n\t\u003ca href=\"https://codeclimate.com/github/KnodesCommunity/nest-casl/maintainability\"\u003e\u003cimg src=\"https://api.codeclimate.com/v1/badges/21cc8f69c9eac8d36aa9/maintainability\" alt=\"CodeClimate maintainability\" /\u003e\u003c/a\u003e\n\t\u003ca href=\"https://codeclimate.com/github/KnodesCommunity/nest-casl/test_coverage\"\u003e\u003cimg src=\"https://api.codeclimate.com/v1/badges/21cc8f69c9eac8d36aa9/test_coverage\" alt=\"CodeClimate test coverage\" /\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## Description\n\nUse decorators everywhere to protect your controller methods.\n\n* [:book: Read the docs](https://knodescommunity.github.io/nest-casl/)\n* [:rocket: Get started](https://knodescommunity.github.io/nest-casl/pages/Guides/getting-started.html)\n\n## Installation\n\n```bash\nnpm install --save @knodes/nest-casl\n```\n\nAdditionally, please make sure you have correct peer dependencies installed:\n\n\u003c!-- PEER DEPS --\u003e\n* [@casl/ability](https://www.npmjs.com/package/@casl/ability): `^5.0.0`\n* [@nestjs/common](https://www.npmjs.com/package/@nestjs/common): `^8.0.0`\n* [@nestjs/core](https://www.npmjs.com/package/@nestjs/core): `^8.0.0`\n* [lodash](https://www.npmjs.com/package/lodash): `^4.17.0`\n* [reflect-metadata](https://www.npmjs.com/package/reflect-metadata): `^0.1.13`\n* [rxjs](https://www.npmjs.com/package/rxjs): `^7.0.0`\n\n```sh\nnpm install @casl/ability@^5.0.0 @nestjs/common@^8.0.0 @nestjs/core@^8.0.0 lodash@^4.17.0 reflect-metadata@^0.1.13 rxjs@^7.0.0\n```\n\u003c!-- END PEER DEPS --\u003e\n\n## In a nutshell\n\nDeclare a new service that converts the user of your request to a *CASL ability*:\n\n```ts\nimport { Injectable } from '@nestjs/common';\nimport { AbilityBuilder, PureAbility } from '@casl/ability';\nimport { CaslAbilityFactory } from '@knodes/nest-casl';\n\n@Injectable()\nexport class AbilityFactory implements CaslAbilityFactory {\n\t// Here, `request` is the express or fastify request. You might get infos from it.\n\tpublic createFromRequest( _request: unknown ): PureAbility {\n\t\tconst abilityBuilder = new AbilityBuilder( PureAbility );\n\t\tabilityBuilder.can( 'feed', 'cat' );\n\t\tabilityBuilder.can( 'hug', 'cat' );\n\t\tabilityBuilder.can( 'pet', 'cat' );\n\t\tabilityBuilder.cannot( 'rename', 'cat' );\n\t\treturn abilityBuilder.build();\n\t}\n}\n```\n\nImport the module:\n\n```ts\nimport { Module } from '@nestjs/common';\nimport { CaslModule } from '@knodes/nest-casl';\n\n@Module( {\n\timports: [\n\t\tCaslModule.withConfig( ( { abilityFactory: AbilityFactory } ) ),\n\t\t// ....\n\t],\n} )\nexport class AppModule {}\n```\n\nUse decorators in your controller:\n\n```ts\nimport { AbilityBuilder, PureAbility } from '@casl/ability';\nimport { Controller, Get } from '@nestjs/common';\nimport { InjectAbility, PoliciesMask, Policy } from '@knodes/nest-casl';\n\n@Controller( '/cat/care' )\n@PoliciesMask({\n\t'pet': { action: 'pet', subject: 'cat' }\n})\nexport class CatCareController {\n\t// Okay, you can feed.\n\t@Get( 'feed' )\n\t@Policy( { action: 'feed', subject: 'cat' } )\n\tpublic feed(){\n\t\t// ...\n\t}\n\n\t// Well, I guess he won't bite.\n\t@Get( 'hug' )\n\t@Policy( { action: 'hug', subject: 'cat' } )\n\tpublic hug(){\n\t\t// ...\n\t}\n\n\t@Get( 'pet' )\n\tpublic pet( @InjectAbility() ability: PureAbility ){\n\t\t// ...\n\t}\n}\n```\n\nFor more details and usage with **guards**, please refer to the [guide](https://KnodesCommunity.github.io/nest-casl/pages/Guides/getting-started.html).\n\n## License\n\n@knodes/nest-casl is [MIT licensed](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknodescommunity%2Fnest-casl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fknodescommunity%2Fnest-casl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fknodescommunity%2Fnest-casl/lists"}