gpt4 book ai didi

java - Firestore 安全规则 : use array_contains to determine authorization

转载 作者:行者123 更新时间:2023-11-29 07:25:58 28 4
gpt4 key购买 nike

我可以在 Firebase 安全规则中使用等效于 array_contains 的东西吗?

我有一个 Event 类(对应于 events 集合中的 Event 文档)。每个文档都有一个 subscribers 列表/数组,其中包含应该能够查询此事件的所有用户的 UID:

{
.
"subscribers" : ["uidA", "uidB", "uidC"],
.

}

更明确地说,用户应该能够运行查询:

db().collection("events").whereArrayContains("subscribers", "uidC");

如何编写此安全规则?

我试过:

match/events/{eventId} {
allow list: if request.auth.uid in resource.data.subscribers;
}

模拟器告诉我访问将被拒绝。

使用这样的函数也会报告访问被拒绝:

function isSubscriber() {
return resource.data.subscribers.includes(request.auth.uid);
}

match/events/{eventId} {
allow list: if isSubscriber();
}

最佳答案

感谢@DougStevenson 的建议,我使用了规则:

match/events/{eventId} {
allow list: if request.auth.uid in resource.data.subscribers;
}

然后让我的 Android 开发人员实际尝试从他的设备进行查询,结果成功了!

不要盲目相信 Firestore 安全规则的模拟器,它可能与现实不同!!!模拟器的代码可能与为实际评估生产规则而部署的代码不同,因此结果可能略有不同。

Firebase 安全规则文档:https://firebase.google.com/docs/reference/rules/rules.List (显然它也适用于 Firestore)。

关于java - Firestore 安全规则 : use array_contains to determine authorization,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52773084/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com