Firebase 已修复:订阅用户的Firestore安全规则

Firebase 已修复:订阅用户的Firestore安全规则,firebase,security,google-cloud-firestore,firebase-security,Firebase,Security,Google Cloud Firestore,Firebase Security,我正在建立一个博客,我正在使用firestore作为数据库。我正在尝试编写安全规则,以便只有订阅的用户才能查看和文章的内容,但我希望每个人都能在主页上看到文章标题和照片 我的数据库架构如下所示: 我有一个文章集,其中每个文档都是一个单独的文章,包含标题和照片等字段。在每个文章文档中都有一个子集合“受保护”,它保存文章的内容,我希望只有订阅的用户才能获得受保护的信息 到目前为止,我的规则是 match /articles/{articleId} { allow read: if true

我正在建立一个博客,我正在使用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=**}添加到我的规则中。但是,这意味着我的所有其他规则都被覆盖

固定的:


我需要为我的所有集合和子集合编写规则。

您没有在规则中正确声明

如上面链接的文档所述,“嵌套匹配语句时,内部匹配语句的路径始终与外部匹配语句的路径相对”

因此,您应该删除
/articles/{articleId}/protected/{protectedId}
路径的第一部分,如下所示:

service cloud.firestore {
    match /databases/{database}/documents {
       match /articles/{articleId} {
          allow read: if true;
          allow write: ....;

          match /protected/{protecedId} {
            allow read: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.subscription == true;
          }
       }
    }
}

嗯,真奇怪。有了这些规则,每个人都应该能够阅读文章集合(
firebase.firestore().collection('articles').get()。然后(…)
)。我将修改答案以包含所有规则代码。也许你的问题在其他地方。当我为所有集合和子集合添加规则时,修复了它。