gpt4 book ai didi

javascript - 用于 Firebase 规则的 JsonPath

转载 作者:行者123 更新时间:2023-12-03 03:57:13 27 4
gpt4 key购买 nike

我有以下数据:

enter image description here

我有以下代码:

   findChats(): Observable<any[]> {
return this.af.database.list('/chat/', {
query: {
orderByChild: 'negativtimestamp'
}
}).map(items => {
const filtered = items.filter(
item => (item.memberId1 === this.me.uid || item.memberId2 === this.me.uid)
);
return filtered;
});
}

如果我使用上面的 JsonPath ('/chat/') 访问数据库,它会返回所有匹配的行,然后代码会过滤结果。

我定义了以下数据库规则:

{
"rules": {
"chat": {
"$key": {
".read": true
}
},
....

我在模拟器中测试:

/chat/-Ko7w9XTtuRVN4p6CMp7/memberId

它找到了一个匹配项。

现在,当我使用此规则运行代码时,我得到:

Error: permission_denied at /chat: Client doesn't have permission to access the desired data

问题

我无法获取代码 JsonPath 来通过规则的原因是因为它只是 /chat/ 吗?

为了使其匹配规则,代码 JsonPath 是否还必须包含 $keymemberId

例如/chat/-Ko7w9XTtuRVN4p6CMp7/memberId

正如您从我的代码中看到的,我正在尝试获取已过滤的聊天列表,并且代码不知道$key值是什么。那么是否不可能对查询应用仅允许访问匹配行的规则?

更新

我的问题类似于this one 。我尝试以下方法但没有成功:

{
"rules": {
".write": "auth != null",
"chat": {
"$id": {
".read": true
}
},

更新

我目前有以下规则,该规则有效,但还不够。它检查用户是否已通过身份验证,但不检查其 auth.id 是否等于 memberId1memberId2

{
"rules": {
"chat": {
".read": "auth != null",
".write": "auth != null"
},
"message": {
".read": "auth != null",
".write": "auth != null"
}
}
}

最佳答案

您收到错误的原因是 Firebase 规则级联。

请查看 this doc 的“读写规则级联”和“规则不是过滤器”部分。

级联本质上意味着以下内容:-

.read and .write rules work from top-down, with shallower rules overriding deeper rules. If a rule grants read or write permissions at a particular path, then it also grants access to all child nodes under it.

因此,如果规则限制对更高节点的访问(或者如果规则不存在),则您无法访问该节点。

因此,就您的情况而言,您可以通过从客户端代码访问 /chat/-Ko7w9XTtuRVN4p6CMp7/memberId 路径来测试它,您会发现它有效。

Firebase 文档中的以下示例复制了您的情况:-

Rules are applied in an atomic manner. That means that a read or write operation is failed immediately if there isn't a rule at that location or at a parent location that grants access. Even if every affected child path is accessible, reading at the parent location will fail completely. Consider this structure:

{
"rules": {
"records": {
"rec1": {
".read": true
},
"rec2": {
".read": false
}
}
}
}

Without understanding that rules are evaluated atomically, it might seem like fetching the /records/ path would return rec1 but not rec2. The actual result, however, is an error:

关于javascript - 用于 Firebase 规则的 JsonPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44891317/

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