blitz-guard icon indicating copy to clipboard operation
blitz-guard copied to clipboard

Ability to provide array for resource input

Open Jaxenormus opened this issue 3 years ago • 2 comments

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.

Jaxenormus avatar Aug 14 '21 20:08 Jaxenormus

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

Jaxenormus avatar Aug 14 '21 20:08 Jaxenormus

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!

ntgussoni avatar Sep 10 '21 15:09 ntgussoni