Skip to content

Conversation

@ghost
Copy link

@ghost ghost commented Jul 26, 2020

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 ghost changed the title Subprop feat: Add ability helper Jul 26, 2020
@frykten
Copy link

frykten commented Jul 27, 2020

Looks interesting

@ghost ghost force-pushed the subprop branch from 84008ea to 28d8373 Compare August 3, 2020 13:13
@ghost ghost force-pushed the subprop branch 2 times, most recently from 18cd18a to 28d8373 Compare August 31, 2020 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants