firebase - 变量值的 Firestore 安全规则日志记录

标签 firebase firebase-security google-cloud-firestore

service cloud.firestore {
  match /databases/{database}/documents {

   match /companies/{companyName=**}{
        allow read,write: if isUserAuthorized(companyName,get(/databases/$(database)/documents/companies/$(companyName)/users/$(request.auth.uid)).data.company) == true;
   }
  }
}
function isUserAuthorized(requestedCompanyName,userCompanyName) {
  return requestedCompanyName == userCompanyName;
}

在firestore的上述规则中,我试图确保只有公司的员工才能访问该公司的数据。 firestore 上的数据结构类似于以下屏幕截图。
数据保存在此路径中的位置/companys/{companyName}/users/{uid}/company

enter image description here

但是当我尝试保存时,我得到 Error: Missing or insufficient permissions. js错误。
  • 有什么明显的我做错了吗?
  • Firestore 端是否有任何日志记录,我可以在其中查看发出的请求以及规则中使用的变量的值。
  • 或者有没有办法测试规则?
  • 最佳答案

    我知道这是一个老问题,但以防万一它对其他人有帮助:

    我认为问题出在“匹配”语句中,这里:

    match /companies/{companyName=**}{
        allow read,write: if isUserAuthorized(companyName,get(/databases/$(database)/documents/companies/$(companyName)/users/$(request.auth.uid)).data.company) == true;
    }
    

    根据 this ,

    When using the recursive wildcard syntax, the wildcard variable will contain the entire matching path segment, even if the document is located in a deeply nested subcollection. For example, the rules listed above would match a document located at /cities/SF/landmarks/coit_tower, and the value of the document variable would be SF/landmarks/coit_tower.



    当匹配路径中的最后一项以 =** 结尾时,您将路径的整个剩余部分分配给变量 companyname .因此,从您的示例中获取值,您可能会期望 companyname包含 spoor ,但实际上它包含 /spoor/users/iKHFF...除非请求是专门针对“spoor”文件的。因此,您的 get() 中的路径没有指向正确的文档。

    确实,如果有可用的调试工具就太好了,但到目前为止,除了反复试验之外,我还没有找到任何其他工具。很不幸,希望在他们退出测试版之前得到修复。

    关于firebase - 变量值的 Firestore 安全规则日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47156788/

    相关文章:

    java - 来自 Firebase 的纬度和经度坐标不正确,并且所有帖子的纬度和经度坐标均相同

    json - Firebase 安全规则?

    firebase - 查看 Cloud Firestore 查询/命令历史记录

    java - WorkManager 一次执行 enqueueUniquePeriodicWork() 多次?

    android - 使用 FirestoreRecyclerAdapter 进行索引查询

    使用 REST API 进行 firebase 电子邮件/密码身份验证

    android - 完成 Firebase 函数(Uri 和 String)Android Java 时出现 ClassCastException

    google-cloud-firestore - 使用exist()的Cloud firestore规则是否算作读取?

    ios - 如何确认 Firestore 中的数据已快速附加?

    javascript - 如何编写 Firebase 规则来检查访问 token ?