Skip to content

Conversation

@Dallin343
Copy link
Contributor

We ran into a use case for guarding endpoints based on request data instead of static data. Specifically, in our findOne(id: string) controller methods, we want to guard based on whether the requesting user can access the resource with the given id. Previously, we were required to use the AuthzService to check the user's access within the findOne function, but mixing decorators and function calls for authorization doesn't seem as clean.

This PR adds support for providing a resourceFromContext function during initialization (which acts as the default) and in the guard (which overrides the default). resourceFromContext is given the execution context of the function, so it can access data from the request, and it returns a resource to be enforced against Casbin. The resource that is returned can be a single resource or an array of resources, and a BatchApproval option determines approval behavior in the array case. With BatchApproval.ANY, as long as any of the resources returned from resourceFromContext pass enforcement, the request continues. With BatchApproval.ALL, all resources must pass enforcement.

I would like to hear thoughts on this use case, and this approach. Are there any apparent or potential problems with this feature or approach? I aimed to maintain existing behavior by default, with this "resource specific guarding" requiring intentional usage to have any effect.

@hsluoyz
Copy link
Member

hsluoyz commented Jan 7, 2025

@nodece @Shivansh-yadav13 please review

Copy link

@nodece nodece left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, LGTM.

@hsluoyz hsluoyz merged commit 7ea8f53 into node-casbin:master Jan 9, 2025
7 checks passed
github-actions bot pushed a commit that referenced this pull request Jan 9, 2025
# [2.12.0](v2.11.0...v2.12.0) (2025-01-09)

### Features

* Add support for guarding based on request data ([#173](#173)) ([7ea8f53](7ea8f53))
@github-actions
Copy link

github-actions bot commented Jan 9, 2025

🎉 This PR is included in version 2.12.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants