【问题标题】:Firestore Security Rule Allows Reads But Not CreatesFirestore 安全规则允许读取但不允许创建
【发布时间】:2018-06-21 22:20:44
【问题描述】:

我有一个名为“读数”的顶级收藏。 Readings 中的每个文档都有一个名为“patientUid”的字段。

我有以下安全规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }

    //Takes a uid and returns true if it's the uid of the current user
    function isTheUidOfCurrentUser(uid) {
        return request.auth.uid == uid;
    }

    //A patient or one of their doctors can view their readings
    match /readings/{reading} {
        allow read, write: if isTheUidOfCurrentUser(resource.data.patientUid);
    }

  }
}

目前,如果用户读取其中一个读数,则一切正常,但由于某种原因用户无法创建读数。最奇怪的是控制台中的模拟器在尝试创建读数时甚至不显示规则匹配。

我在这里做错了什么。为什么规则只匹配读取,而不匹配写入?

【问题讨论】:

    标签: google-cloud-firestore firebase-security


    【解决方案1】:

    问题是 resource.data 表示写入发生之前的文档。因此,在创建文档时,resource.data 没有 PatientUid 字段。

    一旦我改变了

    resource.data.patientUid
    

    request.resource.data.patientUid
    

    它起作用了,因为 request.resource 代表了写入后文档的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 2016-02-26
      • 2021-01-02
      • 1970-01-01
      相关资源
      最近更新 更多