Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow global usage of AccessGuard with exempt routes #979

Open
jreidgreer opened this issue Sep 1, 2024 · 0 comments
Open

Allow global usage of AccessGuard with exempt routes #979

jreidgreer opened this issue Sep 1, 2024 · 0 comments

Comments

@jreidgreer
Copy link

Problem

I prefer to automatically deny all requests unless they're explicitly allowed by permissions & access controls instead of using guards on a per-route basis like this:

@Module({
  imports: [
    CaslModule.forRoot({/* ... */}),
  ],
  controllers: [/* ... */],
  providers: [
    /* ... */
    {
      provide: APP_GUARD,
      useClass: AccessGuard,
    },
  ],
})
export class AppModule implements NestModule {}

I use a @Public() decorator to denote routes that that are explicitly intended for unauthenticated use, but the AccessGuard automatically denies them because of the user check: https://github.com/getjerry/nest-casl/blob/master/src/access.service.ts#L30-L32

Proposed Solution

There are probably a lot of ways to implement this. Here are a few suggestions:

1. Separate decorator to opt-out of the guard:

@Controller("public")
export class PublicDataController {

  @UseAbilityAllowAll()
  @Get("data")
  public async getPublicData() {}
}

2. Extend the interface of the UseAbility guard:

@Controller("public")
export class PublicDataController {

  @UseAbility({
    subject: PublicData,
    action: Actions.read,
    disableUserCheck: true,
  })
  @Get("data")
  public async getPublicData() {}
}

This could allow you to mix public and non-public access control for the same resources, and maintain a simpler external interface. However it would make contract with the Permissions more complicated as user could be undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant