首页 > 解决方案 > 如何使用 CASL 限制要修补(更新)的字段值?

问题描述

在阅读指南并搜索谷歌一段时间后,我无法在 CASL 中找到解决我的用例的方法。

预期的

status字段可能具有以下值之一:'created'、'cancelled'、'kept'、'broken'...但某些值(如 'k​​ept、broken')只能由具有特殊角色的用户更新,例如 'supervisor' ,而不是普通用户。)

我的代码

// define rules
async function defineAbilitiesFor(user) {
    if(user) {
        can('patch', 'appointments', { user_id: user.id, status: 'created' });
        // No where to define rule for limit status to be patched
    }
}

// test rules
const appointment = {..., user_id:1, status:'created' };
const toPatch = { status: 'cancelled' };
ability.can('patch', appointment); // No where to check `toPatch` data

$in不适合我的情况

can('patch', 'appointments', { user_id: user.id, status: {$in: ['created','cancelled'] }); 不能解决两个问题:

标签: javascriptnode.jsaccess-controlcasl

解决方案


推荐阅读