如何通过安全规则禁止更新Firestore文档中的特定字段?

时间:2019-12-19 01:15:17

标签: firebase google-cloud-firestore firebase-security

我有一个事件文档,其中包含标题,位置,容量,坐标等字段。我想设置安全规则以允许更新文档,但是如果用户尝试更改容量字段,该请求将被拒绝

那么如何编写这样的安全规则?我可以这样做吗?

service cloud.firestore {
    function admin() {
        return get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true
    }
    match /databases/{database}/documents {
        match /events/{eventId} {

              allow update: if // (allow if user not trying to update capacity field) ???
        }
    }
}

1 个答案:

答案 0 :(得分:0)

将请求的文档更新内容与现有内容进行比较,以确保它没有更改:

allow update: if request.resource.data.capacity == resource.data.capacity;

request.resource是传入请求的文档。 resource是现有数据。

相关问题