-
-
Notifications
You must be signed in to change notification settings - Fork 269
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
Ability.merge #43
Comments
After some thinking and writing code I decided that such function just adds complexity and requires to answer on additional questions, like:
In most cases, such functionality will be needed only backend side, so it would be good not to pollute frontend bundle with code which will never be used. To achieve the same functionality we can do this: // should define `cannot` rules
function hardwareRules() {
const { can, cannot, rules } = AbilityBuilder.extract()
if (!hardware.supports('wifi')) {
cannot('manage', 'wifi')
}
cannot(...)
return rules
}
function userRules(user) {
const { can, cannot, rules } = AbilityBuilder.extract()
can(...)
cannot(...)
can(...)
return rules
}
const ability = new Ability(userRules(user).concat(hardwareRules())) So, a user is able to split rules (instead of |
I will update documentation with the example above and close this issue |
Example and explanation is added in https://stalniy.github.io/casl/abilities/2017/07/23/use-cases.html |
Allows to merge 2 and more
Ability
instances into. OnlyAbility
instances with the same options can be merged together, otherwise exception should be thrown.This feature will allow to merge rules of few abilities, basically this is a helper method for doing the next thing:
This may be useful if you want to define some rules dynamically in the database and some statically in the code
Design:
Merged abilities are added at the end
In this case,
hardwareAbility
may enforce/overwrite anyuserAbility
.ability
is modified andhardwareAbility
is not changed,update
andupdated
events are emitted onAbility
instance.In case
userAbility
andhardwareAbility
has different options for subject detection (i.e.,subjectName
)Ability.merge
should throw an error telling that merged abilities are not compatibleThe text was updated successfully, but these errors were encountered: