ember-can icon indicating copy to clipboard operation
ember-can copied to clipboard

feat: Add ability helper

Open ghost opened this issue 4 years ago • 1 comments

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 --}}

ghost avatar Jul 26 '20 17:07 ghost

Looks interesting

frykten avatar Jul 27 '20 08:07 frykten