Skip to content

Commit

Permalink
fix(ability): returns null from rulesToQuery if ability has only …
Browse files Browse the repository at this point in the history
…inverted rules

Fixes #170
  • Loading branch information
stalniy committed Mar 25, 2019
1 parent 6125cd3 commit 744061c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/casl-ability/spec/query.spec.js
Expand Up @@ -42,6 +42,15 @@ describe('rulesToQuery', () => {
expect(Object.keys(query)).to.be.empty
})

it('returns `null` if specified only inverted rules', () => {
const ability = AbilityBuilder.define((can, cannot) => {
cannot('read', 'Post', { private: true })
})
const query = toQuery(ability, 'read', 'Post')

expect(query).to.be.null
})

it('returns `null` if at least one inverted rule does not have conditions', () => {
const ability = AbilityBuilder.define((can, cannot) => {
cannot('read', 'Post', { author: 123 })
Expand Down Expand Up @@ -80,6 +89,7 @@ describe('rulesToQuery', () => {

it('AND-es conditions for inverted rules', () => {
const ability = AbilityBuilder.define((can, cannot) => {
can('read', 'Post')
cannot('read', 'Post', { status: 'draft', createdBy: 'someoneelse' })
cannot('read', 'Post', { status: 'published', createdBy: 'me' })
})
Expand Down
4 changes: 3 additions & 1 deletion packages/casl-ability/src/extra.js
Expand Up @@ -25,7 +25,9 @@ export function rulesToQuery(ability, action, subject, convert) {
}
}

return rules.length > 0 ? query : null;
const isOnlyInvertedRules = query.$and && query.$and.length === rules.length;

return rules.length === 0 || isOnlyInvertedRules ? null : query;
}

export function rulesToFields(ability, action, subject) {
Expand Down

0 comments on commit 744061c

Please sign in to comment.