-
-
Notifications
You must be signed in to change notification settings - Fork 259
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
Check conditions without instance of class #101
Labels
Comments
Would be better to ask questions in gitter chat. Recently such case was discussed. There are at least 4 ways to handle this :)
const ability = new Ability(abilityArray, {
subjectName(subject) {
if (subject && subject.subjectType) {
return subject.subjectType;
}
}
});
const authSettings = {
subjectType: 'Post',
tenant_id,
};
const abilityCan = ability.can('read', authSettings); If doesn't fit your needs, please describe your usecase in details |
Thank you, I'm using a variant that defines a non-enumerable property: const SUBJECT_NAME = Symbol('casl/subjectName');
const abilitySubject = (subjectName, object) => {
Object.defineProperty(object, SUBJECT_NAME, {
value: subjectName,
configurable: true,
writable: true
});
return object;
};
const ability = new Ability(abilityArray, {
subjectName(subject) {
if (subject && subject[ SUBJECT_NAME ]) {
return subject[ SUBJECT_NAME ];
}
}
});
const authSettings = {
tenant_id,
};
const abilityCan = ability.can('read', abilitySubject('Post', authSettings)); |
Nice! |
10 tasks
Any changes to this problem lately? Because those solutions seems very hacky 👎 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How is it possible to check abilities with conditions and without instance of class?
One can either pass a string of characters (example: 'Post'), or a class instance where the name of the subject is extracted from the constructor.
How to test an object that is not an instance?
The text was updated successfully, but these errors were encountered: