blitz-guard
blitz-guard copied to clipboard
Ability to provide array for resource input
What do you want and why?
I would like the ability to add an array of resources or have instead of calling the can
function for each resource for the same ability.
Possible implementation(s)
Internally I am not sure how this library works, but I wrote my own function (untested) to do the same thing.
const cans = (
ability: string,
resources: Prisma.ModelName[],
can: _CanType<Prisma.ModelName, any>,
guard?: (args: any, resource: Prisma.ModelName) => Promise<boolean>
): void => {
resources.forEach((resource) =>
guard ? can(ability, resource, (args) => guard(args, resource)) : can(ability, resource)
)
}
and in usage, I would have this
cans("create", mainResources, can)
vs. having to write this
can("create", "Calendar")
can("create", "Event")
can("create", "Group")
can("create", "Media")
Obviously, I tailored the function to my specific use case, but hopefully, it should shed some light on what I would like added as a core feature. I'm more than happy to help with this, so let me know what you think.
I also wanted to add that we don't have to modify the existing can
and cannot
functions but instead add a cans
and cannots
function. Other than that, this is an awesome library
Hi @CalebDelbridge
In this case I don't see it necessary to add a helper function since you can always do
["Calendar", "Event", "Group", "Media"].forEach(r => can("create", r, () => {...}))
However, do you see something that I'm missing here? More than welcome to add that helper on the next release if you happen to make a PR.
Thanks!