【发布时间】:2020-04-01 20:24:59
【问题描述】:
我正在使用@react-native-firebase/firestore
我必须以错误的方式思考集合或文档,但如果直接通过doc 获取文档时被拒绝,我希望以下安全规则阻止我通过onSnapshot 获取文档,但事实并非如此案例
// I get permission denied when doing this
firestore().doc('users/KRImYj0Lrmv00P9KRAuJ').get()
// I do not get permission denied when doing this and can access the document I can't get above
firestore().collection('users').onSnapshot()
安全规则
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{user} {
allow read: if resource.data.is_deleted == false
allow write: if false;
}
}
}
更新完整代码
// FIRST
try {
await firestore()
.collection('users')
.where('is_deleted', '==', true)
.onSnapshot((snapshot) => {
if (snapshot) {
snapshot.forEach((s) => {
console.log('User', s.data())
})
}
})
}
catch (error) {
console.log(error)
}
// SECOND
try {
const docs = await firestore()
.collection('users')
.where('is_deleted', '==', true)
.get()
console.log(docs)
}
catch (error) {
console.log(error)
}
第一个返回快照,我可以记录文档
FIRST LOGS
User {"fname": "Steve", "is_deleted": true, "lname": "Kaspar", "name": "Steven Kaspar", "name_change_count": 2}
User {"fname": "Brian", "is_deleted": true, "lname": "Fitz"}
SECOND LOGS
[Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation.]
【问题讨论】:
-
我很困惑 - 你是说
get()是不允许的,但onSnapshot()是允许的?请编辑问题以明确什么是有效的。 -
@DougStevenson - 抱歉,恰恰相反。我不能
get()但我可以通过onSnapshot()得到它 -
我根本不希望第二个查询起作用,因为它缺少 is_deleted==false 的过滤器。请确保您的规则已保存并且您的查询是正确的。另外,如果您有其他规则,请务必在此处显示。
-
@DougStevenson - 这就是我所期待的。这是整个规则集,所以我不知道接下来要尝试什么。继续打破并重试,直到它起作用我猜哈哈
-
同意 Doug 的观点:这看起来很奇怪。一些帮助调试的步骤: 1) 你可以尝试在集合上做一个
get()而不是onSnapshot()吗? 2) 你能编辑显示你如何处理onSnapshot()吗?我特别好奇您是否/如何处理错误。
标签: firebase react-native google-cloud-firestore firebase-security react-native-firebase