Skip to content

Commit

Permalink
fix(ability): negates inverted rules in ruleToAST (#602)
Browse files Browse the repository at this point in the history
The inverted rules must be negated in order to build the correct AST condition.
  • Loading branch information
ccatterina committed Apr 30, 2022
1 parent 396fddc commit c1bdc60
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/casl-ability/spec/rulesToAST.spec.js
Expand Up @@ -44,7 +44,12 @@ describe('rulesToAST', () => {
expect(ast).to.deep.equal({
operator: 'and',
value: [
{ operator: 'eq', field: 'private', value: true },
{
operator: 'not',
value: [
{ operator: 'eq', field: 'private', value: true }
]
},
{
operator: 'or',
value: [
Expand Down
5 changes: 3 additions & 2 deletions packages/casl-ability/src/extra.ts
@@ -1,4 +1,4 @@
import { Condition, buildAnd, buildOr } from '@ucast/mongo2js';
import { Condition, buildAnd, buildOr, CompoundCondition } from '@ucast/mongo2js';
import { PureAbility, AnyAbility } from './PureAbility';
import { RuleOf } from './RuleIndex';
import { RawRule } from './RawRule';
Expand Down Expand Up @@ -45,7 +45,8 @@ function ruleToAST(rule: RuleOf<AnyAbility>): Condition {
if (!rule.ast) {
throw new Error(`Ability rule "${JSON.stringify(rule)}" does not have "ast" property. So, cannot be used to generate AST`);
}
return rule.ast;

return rule.inverted ? new CompoundCondition('not', [rule.ast]) : rule.ast;
}

export function rulesToAST<T extends AnyAbility>(
Expand Down

0 comments on commit c1bdc60

Please sign in to comment.