Casl: <Can> without subject

Created on 27 Aug 2018  路  5Comments  路  Source: stalniy/casl

I have a flat component permission-wise, I do not really use the "of" -part (or subject) of the component.
Casl seems to work fine with me leaving it out but the React component gives prop type warnings.
I have worked around this by creating a custom can-component but I'm wondering if the prop-type validation could be loosened or if there is a more elegant way handling cases like:
<Can I="post" />

Tasks:

  • [x] update types for ForbiddenError.throwUnlessCan
  • [x] update types for can, cannot, relevantRuleFor, rulesFor,possibleRulesForofAbility`
  • [x] update types for can & cannot of AbilityBuilder
  • [x] casl/angular
  • [x] casl/aurelia
  • [x] casl/vue
  • [x] casl/react
  • [x] add tests for casl/ability
casability casreact casvue enhancement

Most helpful comment

will be available in casl 4.x

All 5 comments

I thought about this recently. Actually it's a valid use case when app has a simple permission system which is based only on actions.

My thoughts on this:

  • [ ] change d.ts to support can with single parameter for AbilityBuilder.can and for Ability.can
  • [ ] make subject optional for Vue and React <Can> components
  • [ ] provide a way to shake out conditions (including sift.js)
  • [ ] add this usecase to docs

CASL is awesome - thank you! Looking forward to this being addressed. Any thoughts on when it will be available?

Currently I鈥檓 pretty busy at my job, so it鈥檚 hard to say. But PRs are welcome :)

Meanwhile you can define permissions using all subject and create a simple wrapper around ability which uses all subject as default value.

After some thinking about this, I realised that on @casl/ability level it can be implemented without changes. The only thing which you need to do is to define custom subjectName function:

const subjectName = () => 'all'

const ability = AbilityBuilder.define({ subjectName }, (can) => {
  can('read')
  can('think')
})

console.log(ability.can('think')) // true
console.log(ability.can('write')) // false

The only thing which needs to be changed is Typescript declarations and complementary packages

will be available in casl 4.x

Was this page helpful?
0 / 5 - 0 ratings