javascript - 如何使用 CASL 限制要修补(更新)的字段值?
问题描述
在阅读指南并搜索谷歌一段时间后,我无法在 CASL 中找到解决我的用例的方法。
预期的
- 普通用户只能修补他的约会。
- 普通用户只能
appointment.status
从“已创建”修补到“已取消”(而不是其他值,例如“保留”)。
(status
字段可能具有以下值之一:'created'、'cancelled'、'kept'、'broken'...但某些值(如 'kept、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'] });
不能解决两个问题:
- 普通用户仍然可以将约会更新为其他状态,例如“中断”;(因为约会只能由主管用户设置)
- 普通用户可以将约会从“取消”更新为“已创建”;(因为预约只能取消一次。)
解决方案
推荐阅读
- swift - 从图像中检测的阿兹台克代码无法快速运行
- javascript - javascript - 当我创建一个空项目数组时,我不能使用 map
- amazon-web-services - 为什么在暂停执行时无法访问似乎在范围内的对象?
- python - Google Coral:模型大小有限制吗?
- visual-studio-2017 - Specflow 在执行后跳过测试并出现错误:解压执行程序时出错:
- c - 如果套接字返回 SSL_ERROR_WANT_READ 或 SSL_ERROR_WANT_WRITE 再次执行 SSL_shutdown 是否安全
- css - 定位三个列表元素 - 2 和 3 直接在 1 下方
- c++ - 解决了下面给定的问题,但无法获得最终输出
- angular - ERROR 错误:找不到名称为“certificateTextPlacements”的控件。FormArray 内的 FormArray
- javascript - 加载 componentDidMount 后重新加载渲染函数