Firestore返回的权限不足,即使它不应该

我为我的 Firestore 数据库设置了以下规则:


rules_version = '2';

service cloud.firestore {

  match /databases/{database}/documents {


    match /collections/{document=**} {

        allow read;

        allow write: if isAdmin();

        }


    match /general/{document=**} {

        allow read;

        allow write: if isAdmin();

        }


    match /inquiries/{document=**} {

        allow write;

        allow read: if isAdmin();

        }


    match /orders/{document=**} {

        allow write;

        allow read: if isAdmin() || resource.data.userID == request.auth.uid;

        }


    match /products/{document=**} {

        allow read;

        allow write: if isAdmin();

        }


    match /users/{userId} {

        allow write, read: if belongsTo(userId);

        }


    function belongsTo(userId) {

        return request.auth.uid == userId

        }


    function isAdmin() {

        return resource.data.admin == true;

    }

  }

}

如您所见,每个人都可以阅读 /products 及其文档以及子集合。哪个适用于产品,但不知何故无法读取产品的子集合(每个产品都有一个名为)。collection-colors


FirebaseError:权限缺失或不足。


导致错误的代码:


retrieveCollectionColors(name) {

    this.db.collectionGroup('collection-colors', ref => ref.where('product', '==', name))

      .valueChanges().subscribe( (val: []) => {

      this.collectionColors.next(val);

    }, error => {

      console.log(error);

    });

}


慕妹3146593
浏览 140回答 1
1回答

慕桂英546537

您现在拥有的规则根本不适用于集合组查询。您需要为此编写一个特殊规则。从文档中:基于集合组保护和查询文档在您的安全规则中,您必须通过为集合组编写规则来明确允许集合组查询:确保 rules_version = '2'; 是您的规则集的第一行。集合组查询需要{name=**} 安全规则版本 2 的新递归通配符行为。使用 为您的集合组编写规则match /{path=**}/[COLLECTION_ID]/{doc}。因此,如果您想允许“collection-colors”的集合组查询,它看起来像这样:match /{path=**}/collection-colors/{doc} {   allow read: ...}这将适用于具有给定名称的所有子集合。您不能根据父集合的名称选择性地允许或禁止子集合。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript