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

Can I use top-level operators in can/cannot rules? #252

Closed
jnardone opened this issue Jan 15, 2020 · 2 comments
Closed

Can I use top-level operators in can/cannot rules? #252

jnardone opened this issue Jan 15, 2020 · 2 comments
Labels

Comments

@jnardone
Copy link

I'm trying to track down some failures when we went from 3.2.0->3.4.0 (well, 3.3.0 is where the breakage is, likely with the bump of sift). Our automated tests have some failures and the rules they're failing on seem to follow a specific pattern.

  can('read', 'thing', {
    $and: [{ thing: { $ne: 50 } }]
  });

Namely: it appears any rule that has a top-level operator like this in the conditions section. We do both $or and $and and these worked in 3.2.0 but fail now.

So:

  • is this a syntax that should work?
  • if not, do you have any suggestions on how this would be expressed? We want to append additional things to compare against the resource row
@stalniy
Copy link
Owner

stalniy commented Jan 16, 2020

Casl has never had documented support for $or and $and. If use them you make your life much more complicated. Why? because there is a built-in behavior which allows to use $and and $or under the hood.

All direct rules (specified by can) are logically OR-ed. So the next 2 samples are equivalent:

// 1st
can(‘read’, ‘thing’, { 
   $or: [{ prop1: 1, prop2: 2 }]
})  

//2nd
can(‘read’, ‘thing’, { prop1: 1 })
can(‘read’, ‘thing’, { prop2: 2 })

And all inverted rules (specifies by cannot) are logically AND-ed.

This allows fine grained control on what to allow user to do. With more complicated rules, things become more complex.

First, try to redefine rules in suggested way. sift now supports custom query creation, so in 4.x you won’t be able to use $and and $or operators

@stalniy
Copy link
Owner

stalniy commented Jan 16, 2020

This is documented in “combining abilities” section here - https://stalniy.github.io/casl/abilities/2017/07/20/define-abilities.html

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