我正在建立博客,并且正在使用Firestore作为数据库。我正在尝试编写安全规则,以便只有订阅的用户才能查看和的内容,但我希望每个人都可以在首页上看到文章的标题和照片。
我的数据库架构如下:
我有一个收藏文章,其中每个文档都是单独的文章,其中包含标题和照片等字段。在每个文章文档中,都有一个“受保护的”子集合来保存文章的内容,我希望仅订阅的用户才能获得该受保护的信息。
到目前为止,我的规则是
match /articles/{articleId} {
allow read: if true;
allow write: if request.auth.uid == 'someId';
match /articles/{articleId}/protected/{protecedId} {
allow read: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.subscription == true;
}
}
但是,这阻止了我的主页加载和显示任何文章。有人可以帮我弄清楚我的规则有什么问题吗?
更新:我的规则现在看起来像这样:
match /articles/{articleId} {
allow read: if true;
allow write: if request.auth.uid == 'someid';
match /protected/{protectedId} {
allow read: if true;
allow write;
}
}
然而,我的主页还是无法加载。唯一的解决方案似乎是将match / {document = **}添加到我的规则中。但是,这意味着我所有其他规则都被覆盖。
您没有在规则中正确声明您的hierarchical data structure。
如上面链接的文档中所述,“当嵌套匹配语句时,内部匹配语句的路径总是相对与外部匹配语句的路径”。
因此,您应该删除/articles/{articleId}/protected/{protectedId}
路径的第一部分,如下所示:
match /articles/{articleId} {
allow read: if true;
allow write: if request.auth.uid == 'someId';
match /protected/{protectedId} {
allow read: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.subscription == true;
}
}