我正在设置 Firestore 安全规则,但遇到一个问题,即我想使用 hasOnly 函数限制仅更新文档中的一个特定字段。问题是我使用模拟器不断得到“拒绝”结果。我肯定做了一些简单的错误...我试图防止有人可以更新文档中的其他字段update_requested_time
,但允许他们更新此特定字段。
当使用 Firestore 文档中有关 hasOnly() 的示例时 —['a', 'b'].hasOnly(['b', 'a']) == true
— 规则返回 true。但是当使用我自己的时却没有,因此我认为我在这部分出了问题request.resource.data.keys()
.
特定场景中的特定规则定位字段:
match /scenarios/{scenario} {
allow read: if true;
allow update: if request.auth.uid != null
&& request.resource.data.keys().hasOnly(['update_requested_time']) == true;
我发送的模拟器请求(通过身份验证更新):
{"__name__":"/databases/(default)/documents/scenarios/test1","data":{"update_requested_time":"2019-02-05T11:00:00.000Z"}}
我的完整规则:
service cloud.firestore {
match /databases/{database}/documents {
match /scenarios/{scenario} {
allow read: if true;
allow update: if request.auth.uid != null
&& request.resource.data.keys().hasOnly(['update_requested_time']) == true;
match /comments/{comment} {
allow read: if true;
allow create: if request.auth.uid != null;
allow delete,update: if request.auth.uid != null && request.auth.uid == resource.data.user;
}
match /outputs/{tile} {
allow read: if true;
}
match /mutations/{tile} {
allow read: if true;
allow create,update: if request.auth.uid != null;
}
}
match /users/{user} {
allow read: if true;
allow update: if request.auth.uid != null && request.auth.uid == user;
}
}
}
firestore规则+模拟器的屏幕截图 https://i.stack.imgur.com/XY4lc.png
看起来现在正确的方法是
request.resource.data.diff(resource.data).affectedKeys().hasOnly(["update_requested_time"])
发现于this https://groups.google.com/g/google-cloud-firestore-discuss/c/WmDd5L6K-Qc/m/Dqnel8VPAgAJ对话
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)