npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

nestjs-casl-authorization

v1.0.3

Published

NestJS casl authorization mannager

Readme

This package contains CASL integration for NestJS microservices

Instructions

Installation

yarn add nestjs-casl-authorization

Importing Module

import { Module } from '@nestjs/common';
import {
  AuthorizationModule,
} from 'nestjs-casl-authorization';

@Module({
  imports: [
    AuthorizationModule
  ],
})
export class AppModule {}

Use guard

Global guard

You can use the Authorization guard for all your resolvers, you just have to include it in the providers section of the app.module

import { Module } from '@nestjs/common';
import {
  AuthorizationModule,
  AuthorizationGuard,
} from 'nestjs-casl-authorization';

@Module({
  imports: [
    AuthorizationModule
  ],
  providers: [
    {
      provide: APP_GUARD,
      useClass: AuthorizationGuard,
    },
  ],
})
export class AppModule {}

Scoped guard

Import Authorize decorator and implement it on the desired resolver

import { Resolver } from '@nestjs/graphql';
import { Authorize } from 'nestjs-casl-authorization';

@Authorize()
@Resolver(() => MyResolverDTO)
export class MyResolver {
  constructor() {}
}

In your resolver you can implement the GrantPermission interface and the CheckPermissions to grant permissions to a user with an action and a scope.

import { Resolver, Query } from '@nestjs/graphql';
import
import {
  Authorize,
  CheckPermissions,
  GrantPermission
} from 'nestjs-casl-authorization';

// This Authorize decorator can be avoided if the guard is globally scoped.
@Authorize()
@Resolver(() => MyResolverDTO)
export class MyResolver {
  constructor() {}

  /* If the user coming from the context has this permission
  { action: 'read', scope: 'my-resolver.hello' }, permission will be granthed */
  @CheckPermissions(new GrantPermission('read', 'my-resolver.hello'))
  @Query(() => String)
  public async hello(): Promise<DeleteOneResponseDTO> {
    return 'Hello'
  }
}

Create permissions

You can implement your permissions by implementing the IPermissionHandler interface providing an action and a subject

import { IPermissionHandler } from 'nestjs-casl-authorization';

export class MyPermission implements IPermissionHandler {
  constructor(readonly action: string, readonly subject: string) {}

  handle(ability: any) {
    return ability.can(this.action, this.subject);
  }
}

In your resolver you can implement your custom permission with the CheckPermissions decorator by providing the action and a scope.

import { Resolver, Query } from '@nestjs/graphql';
import { MyPermission } from './my-permission.ts';
import
import {
  Authorize,
  CheckPermissions,
} from 'nestjs-casl-authorization';

// This Authorize decorator can be avoided if the guard is globally scoped.
@Authorize()
@Resolver(() => MyResolverDTO)
export class MyResolver {
  constructor() {}

  /* If the user coming from the context has this permission
  { action: 'read', scope: 'my-resolver.hello' }, permission will be granthed */
  @CheckPermissions(new MyPermission('read', 'my-resolver.hello'))
  @Query(() => String)
  public async hello(): Promise<DeleteOneResponseDTO> {
    return 'Hello'
  }
}

Implementation with Graphql Federation

There may be some cases where you need to import the user from the context, in this case we get an user object from the headers to implement authorization through services. To do so yo can import the "getUserFromContext" custom helper. It will add an IUserPaylod inteface to the request context that we will be able to use in the resolvers.

Importing Sync

import { Module } from '@nestjs/common';
import {
  AuthorizationModule,
  getUserFromContext,
} from 'nestjs-casl-authorization';

@Module({
  imports: [
    AuthorizationModule,
    GraphQLFederationModule.forRoot({
      context: async ({ req, request }): Promise<GqlContext> => {
        return await getUserFromContext({ req, request });
      },
    }),
  ],
})
export class AppModule {}

If you need info from the user in the context you can use the CurrentUser decorator.

import { Resolver, Query } from '@nestjs/graphql';
import { MyPermission } from './my-permission.ts';
import
import {
  Authorize,
  CheckPermissions,
  CurrentUser,
  IUserPayload
} from 'nestjs-casl-authorization';

// This Authorize decorator can be avoided if the guard is globally scoped.
@Authorize()
@Resolver(() => MyResolverDTO)
export class MyResolver {
  constructor() {}

  /* If the user coming from the context has this permission
  { action: 'read', scope: 'my-resolver.hello' }, permission will be granthed */
  @CheckPermissions(new MyPermission('read', 'my-resolver.hello'))
  @Query(() => String)
  public async hello(
      @CurrentUser() user: IUserPayload
  ): Promise<DeleteOneResponseDTO> {
    return `Hello user: ${JSON.stringify(user)}`
  }
}

Change Log

See Changelog for more information.

Author

Luis Carlos Arias

License

Licensed under the MIT License - see the LICENSE file for details.