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
但是当我尝试保存时,我得到
Error: Missing or insufficient permissions.
js错误。最佳答案
我知道这是一个老问题,但以防万一它对其他人有帮助:
我认为问题出在“匹配”语句中,这里:
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/