我正在学习CASL,并尝试删除一篇带有条件的文章,但是得到了这个错误Cannot execute "delete" on "Article"。这是CodeSandBox链路。
下面是示例代码:
const { createMongoAbility, ForbiddenError } = require("@casl/ability");
const rules = [
{
action: "read",
subject: "Article"
},
{
inverted: true,
action: "delete",
subject: "Article",
conditions: { published: true },
reason: "You are not allowed to delete this article"
}
];
const ability = createMongoAbility(rules);
// this can be pulled from a database
class Article {
constructor(attrs) {
Object.assign(this, attrs);
}
}
const anotherArticle = new Article({
authorId: 2,
published: false,
content: "Lorem Ipsum"
});
try {
// checking ability before taking some action
ForbiddenError.from(ability).throwUnlessCan("delete", anotherArticle);
} catch (error) {
console.log(error.message); // throwing `Cannot execute "delete" on "Article"`
}请帮帮我。谢谢
发布于 2022-11-02 05:52:14
CASL JS的创建者回答说:
您声明不可能删除已发表的文章,但您从未说过可以删除文章。这就是为什么你会犯错误
因此,这意味着我已经宣布了倒排许可,而且不可能删除文章。

https://stackoverflow.com/questions/74266082
复制相似问题