Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let AbilityBuilder DSL accept Constructor as subject #58

Closed
icedtoast opened this issue May 7, 2018 · 3 comments
Closed

Let AbilityBuilder DSL accept Constructor as subject #58

icedtoast opened this issue May 7, 2018 · 3 comments
Assignees

Comments

@icedtoast
Copy link
Contributor

It would be handy if the AbilityBuilder supported the constructor function in lieu of the subject name when creating rules with can. After all, it already has the subjectName option passed in.

For example:

import { AbilityBuilder } from '@casl/ability'
import Post from './post'

function defineAbilitiesFor(user) {
  return AbilityBuilder.define((can, cannot) => {
    if (user.isAdmin()) {
      can('manage', Post)
    } else {
      can('read', Post)
    }
  })
}

I am happy to submit a pull request with this feature.

@stalniy
Copy link
Owner

stalniy commented May 7, 2018

Hi

I’m ok with that. But you need to make sure that subject field in resulting rules produced by DSL is a string.

Because Ability creates an index (plain object) for that rules in order to find them quickly. I still don’t want to use Map Because this will require to add polyfill on front end side

Waiting for PR :)

@stalniy
Copy link
Owner

stalniy commented May 7, 2018

Don’t forget to update typescript definitions

@stalniy
Copy link
Owner

stalniy commented May 9, 2018

available in @casl/[email protected]! Thanks for contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants