ember-can
ember-can copied to clipboard
feat: Add ability helper
I'd like to propose the following changes to support more interactions with the library.
Motivation
I like the idea of sharing permissions using ember-can abilities but I would like to be able to be able to get more data from my abilities. For example I would like to be able to get a reason as to why an ability comes out negative.
Proposal
Given the following ability :
// app/abilities/post.js
import { computed } from '@ember/object';
import { Ability } from 'ember-can';
export default Ability.extend({
// only an admin can edit a post, if and only the post is editable
canEdit: computed('user.isAdmin', 'model.isNotEditable', function() {
if (!this.get('model.isNotEditable')) {
return {
can: false,
reason: 'This post cannot be edited'
}
}
if (!this.get('user.isAdmin')) {
return {
can: false,
reason: 'You need to be an admin to edit a post'
}
}
return true;
})
});
I would still be able to get my ability's value using can and cannot helpers but could also use an helper to get the value of the ability instead of a boolean :
{{ability "write post" post}}
{{!-- returns { can: ..., reason: ... } or true --}}
This helper would also support a way that would allow me to get the specific subproperty of the ability in question :
{{ability "write post:reason" post}}
{{!-- returns 'This post cannot be edited', 'You need to be an admin to edit a post' or undefined --}}
Looks interesting