我正在 Firestore 中实现一本食谱书,其中每个用户都可以查看所有用户创建的所有食谱,但只有食谱的原始作者才可以编辑或删除食谱。任何用户都可以创建新配方。
我的问题是,我无法设置子集合的权限以“侦听”子集合父文档的字段。
每个配方文档包含三件事。一个名为name
存储菜谱名称的字段称为creatorUID
哪里的request.auth.uid
创建者 uid 的存储和一个名为的子集合ingredients
包含带有一些随机字段的文档。
service cloud.firestore {
match /databases/{database}/documents {
function isSignedIn() {
return request.auth != null;
}
match /ListOfRecipes/{recipe} {
allow read, create: if isSignedIn();
allow update, delete: if resource.data.creatorUID == request.auth.uid;
match /{list=**} {
allow read: if isSignedIn();
// Should return true if recipe.creatorUID has same value as request.auth.uid
allow write: if recipe.creatorUID == request.auth.uid;
}
}
}
}
问题是,使用这些规则只能创建配方文档。由于数据库显示,子集合及其文档尚未创建
FirebaseError:[code=permission-denied]:权限缺失或不足。
FirebaseError:权限缺失或不足。
这些调用是从 Angular 客户端及其官方库发出的。
规则不会级联,因此您需要对规则捕获的文档执行所需的任何检查。
一般来说,{x=**}
规则常常是一个错误,并且使用=**
仅适用于极其特定的用例。
根据您的问题,我假设您的数据模式是这样的:
/ListofRecipes/{recipe_document}/List/{list_document}
在这种情况下,您需要对规则进行如下配置:
service cloud.firestore {
match /databases/{database}/documents {
function isSignedIn() {
return request.auth != null;
}
match /ListOfRecipes/{recipe} {
allow read, create: if isSignedIn();
allow update, delete: if resource.data.creatorUID == request.auth.uid;
function recipeData() {
return get(/databases/$(database)/documents/ListOfRecipes/$(recipe)).data
}
match /List/{list} {
allow read: if isSignedIn();
allow write: if recipeData().creatorUID == request.auth.uid;
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)