https://github.com/knodescommunity/nest-casl
A module for managing authorization in your nest application, using casl.
https://github.com/knodescommunity/nest-casl
Last synced: about 1 year ago
JSON representation
A module for managing authorization in your nest application, using casl.
- Host: GitHub
- URL: https://github.com/knodescommunity/nest-casl
- Owner: KnodesCommunity
- License: mit
- Created: 2021-01-11T13:28:33.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-04-07T18:29:19.000Z (about 1 year ago)
- Last Synced: 2025-04-07T19:36:59.555Z (about 1 year ago)
- Language: TypeScript
- Size: 1.44 MB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# @knodes/nest-casl
A simple decorator-based way to check CASL abilities on NestJS controllers.
## Description
Use decorators everywhere to protect your controller methods.
* [:book: Read the docs](https://knodescommunity.github.io/nest-casl/)
* [:rocket: Get started](https://knodescommunity.github.io/nest-casl/pages/Guides/getting-started.html)
## Installation
```bash
npm install --save @knodes/nest-casl
```
Additionally, please make sure you have correct peer dependencies installed:
* [@casl/ability](https://www.npmjs.com/package/@casl/ability): `^5.0.0`
* [@nestjs/common](https://www.npmjs.com/package/@nestjs/common): `^8.0.0`
* [@nestjs/core](https://www.npmjs.com/package/@nestjs/core): `^8.0.0`
* [lodash](https://www.npmjs.com/package/lodash): `^4.17.0`
* [reflect-metadata](https://www.npmjs.com/package/reflect-metadata): `^0.1.13`
* [rxjs](https://www.npmjs.com/package/rxjs): `^7.0.0`
```sh
npm 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
```
## In a nutshell
Declare a new service that converts the user of your request to a *CASL ability*:
```ts
import { Injectable } from '@nestjs/common';
import { AbilityBuilder, PureAbility } from '@casl/ability';
import { CaslAbilityFactory } from '@knodes/nest-casl';
@Injectable()
export class AbilityFactory implements CaslAbilityFactory {
// Here, `request` is the express or fastify request. You might get infos from it.
public createFromRequest( _request: unknown ): PureAbility {
const abilityBuilder = new AbilityBuilder( PureAbility );
abilityBuilder.can( 'feed', 'cat' );
abilityBuilder.can( 'hug', 'cat' );
abilityBuilder.can( 'pet', 'cat' );
abilityBuilder.cannot( 'rename', 'cat' );
return abilityBuilder.build();
}
}
```
Import the module:
```ts
import { Module } from '@nestjs/common';
import { CaslModule } from '@knodes/nest-casl';
@Module( {
imports: [
CaslModule.withConfig( ( { abilityFactory: AbilityFactory } ) ),
// ....
],
} )
export class AppModule {}
```
Use decorators in your controller:
```ts
import { AbilityBuilder, PureAbility } from '@casl/ability';
import { Controller, Get } from '@nestjs/common';
import { InjectAbility, PoliciesMask, Policy } from '@knodes/nest-casl';
@Controller( '/cat/care' )
@PoliciesMask({
'pet': { action: 'pet', subject: 'cat' }
})
export class CatCareController {
// Okay, you can feed.
@Get( 'feed' )
@Policy( { action: 'feed', subject: 'cat' } )
public feed(){
// ...
}
// Well, I guess he won't bite.
@Get( 'hug' )
@Policy( { action: 'hug', subject: 'cat' } )
public hug(){
// ...
}
@Get( 'pet' )
public pet( @InjectAbility() ability: PureAbility ){
// ...
}
}
```
For more details and usage with **guards**, please refer to the [guide](https://KnodesCommunity.github.io/nest-casl/pages/Guides/getting-started.html).
## License
@knodes/nest-casl is [MIT licensed](LICENSE).
