我一直在阅读 CASL 库的文档,显然您可以使用 JSON 定义功能,如下所示:
import { Ability } from '@casl/ability';
export default new Ability([
{
action: 'read',
subject: 'Post'
},
{
action: 'delete',
subject: 'Post',
conditions: { published: true },
inverted: true
}
])
但是现在我正在尝试这样一个虚拟示例,但它不起作用,不知道为什么:
import { Ability } from '@casl/ability';
const ability = new Ability([{ action: 'read', subject: 'article' }], {
detectSubjectType:subject => subject.subject
})
console.log(ability.can('read', {subject: 'article'})); // Returns false