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

Lazy Pattern #294

Closed
corzani opened this issue Apr 20, 2020 · 4 comments
Closed

Lazy Pattern #294

corzani opened this issue Apr 20, 2020 · 4 comments
Labels

Comments

@corzani
Copy link
Contributor

corzani commented Apr 20, 2020

Hi,
Consider the following simple case:

function defineAbilitiesFor(user) {
    const {can, cannot, rules} = new AbilityBuilder();

   can('read', 'Something', ... a json that is a result of some computations ...);
   can('edit', 'Something', ... a json that is a result of some computations ...);
   can('delete', 'Something', ... a json that is a result of some computations ...);

return new Ability(rules);
}

It would be nice to specify all the possible rules as above but let CASL executing them lazily in case we use a function as parameter instead of a json.
Is there a way to define several complex rules avoiding unwanted code execution?

Something like:

can('read', 'Something', () => check1())
can('edit', 'Something', () => check2());
can('delete', 'Something', () => check3());
toMongoQuery(abilityAbove, 'XYZ', 'read') // Should have executed only check1 lambda

Is there any specific known pattern to handle similar cases using CASL (in my case the mongoose package)? Would it make sense as a feature?
Thanks

@corzani corzani changed the title Lazyness to Ability Lazy Pattern Apr 20, 2020
@stalniy
Copy link
Owner

stalniy commented Apr 20, 2020

Hi,

Thanks for the issue! Not sure that I correctly understand the usecase but you can already do this. CASL evaluates conditions lazily at the moment when you call ability.can(). Check Customize Ability

If you want to define async conditions, then this is not available for now and probably won't be implemented. You can find reasons and alternatives in #160 . Someday I plan to put this in a cookbook section.

Not sure what you expect from toMongoQuery but it returns an object of mongo conditions and it was not designed to work with functions. At least, I cannot imagine how it should work.

Does it make sense or you have something else in mind?

@corzani
Copy link
Contributor Author

corzani commented Apr 20, 2020

@stalniy , it's exactly that bit (the Customize Ability section), I did miss it.
Regarding toMongoQuery part... Unfortunately, to check my permissions, I need to execute several queries due to the complexity of the problem and build a json filter. With that custom Ability you pointed at I can make something dynamic that can work even if they do not support async functions.
Thanks again

@stalniy
Copy link
Owner

stalniy commented Apr 20, 2020

Cool! So, can we close the issue?

@corzani
Copy link
Contributor Author

corzani commented Apr 20, 2020

Yes, please do.

@stalniy stalniy closed this as completed Apr 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants