Cloud Firestore 权限

Cloud Firestore permissions

我在我的 Cloud Firestore 中配置了以下权限,但我在项目可视化方面遇到了一些问题。

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{document=**} {
      allow read;
    }
    match /pages/{pageid} {

      // Returns true if the user is logged        
      function isSignedIn() {
        return request.auth != null;
      }

      // Returns true if the logged user is admin
      function isAdmin() {
        return isSignedIn()
            && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true;
      }

      // Returns true if the requested page has published marked as true (or should, at least :( )
      function isPublished() {
        return resource.data.published == true;
      }

      allow read: if isPublished() || isAdmin();
      allow write: if isAdmin();
    }
  }
}

我无法使 isPublished() 正常工作。内容未按应有的方式显示。 isAdmin() 正常工作。

我的数据将 属性 published 按预期配置为 boolean,除了一个以外的所有数据都被检查为 true。没有显示任何内容,浏览器中出现此错误:

core.js:14597 ERROR Error: Missing or insufficient permissions.

有人知道如何解决这个问题吗?

顺便说一句,我的代码基于文档:https://firebase.google.com/docs/firestore/security/rules-query#evaluating_constraints_on_queries

这是我尝试读取的数据示例:

我的 Angular 服务的构造函数获取页面列表:

constructor(
  public afs: AngularFirestore) {   

    this.pagesCollection = this.afs.collection('pages', ref => ref.orderBy('order', 'asc'));

    this.pages = this.pagesCollection.snapshotChanges().pipe(
      map(changes => {
        return changes.map(a => {
          const data = a.payload.doc.data() as Page;
          data.id = a.payload.doc.id;
          return data;
        })
      })
    );
}

getPages(){
  return this.pages;   
}

您正在尝试阅读您的规则不允许的所有文档。请记住,安全规则不会过滤文档。他们只是确保读取操作符合规则,而在您的情况下则不符合。

这意味着您需要通过仅请求已发布的文档来复制查询中规则的逻辑:

ref.where('published', '==', true).orderBy('order', 'asc')

有关这方面的更多信息,请参阅: