Tim*_*Tim 1 javascript firebase-security google-cloud-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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,每个人都可以阅读 /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);
});
}
Run Code Online (Sandbox Code Playgroud)
您现在拥有的规则根本不适用于集合组查询。您需要为此编写一条特殊规则。从文档中:
基于集合组保护和查询文档
在安全规则中,您必须通过为集合组编写规则来明确允许集合组查询:
- 确保rules_version = '2';是规则集的第一行。集合组查询需要安全规则版本 2 的新递归通配符
{name=**}行为。- 使用 为您的收集组编写规则
match /{path=**}/[COLLECTION_ID]/{doc}。
因此,如果您想允许集合组查询“集合颜色”,它将如下所示:
match /{path=**}/collection-colors/{doc} {
allow read: ...
}
Run Code Online (Sandbox Code Playgroud)
这将适用于具有给定名称的所有子集合。您不能根据父集合的名称有选择地允许或禁止子集合。
| 归档时间: |
|
| 查看次数: |
563 次 |
| 最近记录: |