如何创建允许文档成员编写该文档的 Firebase 安全规则
How to create Firebase security rule that allows members of a doc to write that doc
我的 firebase 数据库包含家庭。这些家庭有 parents
和地址等其他属性。我想允许 parent 对他们的家庭文件进行写访问,并允许 read-only 访问其他家庭。我如何用安全规则语法表达这一点?
假设数据如下所示:
families (collection)
"TheSmiths" (doc) {
parents: [{ firstname: "John", role:"dad", userId:"xxxx" },
{ firstname: "Joan", role:"mom", userId:"yyyy" } ],
address: "123 Oak Lane"
}
"TheJoneses" (doc) etc.
到目前为止,我在规则方面的最佳表现是这样的:
service cloud.firestore {
match /databases/{database}/documents {
// any user can read the families
match /families/{anyDocument} {
allow read: if request.auth != null;
}
// here's my problem
match /families/{anyDocument} {
allow read, write: if resource.data.parents.userId == request.auth.uid;
}
}
}
第二条规则试图说,"allow read and write of a family doc if the user's id is stored in one of the parents. The problem I'm having is syntax for "“parent之一。也许我可以说:
if ((resource.data.parents[0].userId == request.auth.uid) ||
(resource.data.parents[1].userId == request.auth.uid))
可以吗?即使我可以,出于编辑目的,数据可能包含多于或少于两个可以被视为 parent 的成年人。我什至接近?
查看 Firebase docs
你可以试试这个
match /families/{anyDocument} {
allow read: if request.auth != null;
allow write: if resource.data.parents.userId in [(request.auth.uid)];
}
表示如果您已通过身份验证则允许读取,如果您在 parents/userId
地图路径中列出则允许写入。
我没有检查自己,希望这对您有所帮助。
我的 firebase 数据库包含家庭。这些家庭有 parents
和地址等其他属性。我想允许 parent 对他们的家庭文件进行写访问,并允许 read-only 访问其他家庭。我如何用安全规则语法表达这一点?
假设数据如下所示:
families (collection)
"TheSmiths" (doc) {
parents: [{ firstname: "John", role:"dad", userId:"xxxx" },
{ firstname: "Joan", role:"mom", userId:"yyyy" } ],
address: "123 Oak Lane"
}
"TheJoneses" (doc) etc.
到目前为止,我在规则方面的最佳表现是这样的:
service cloud.firestore {
match /databases/{database}/documents {
// any user can read the families
match /families/{anyDocument} {
allow read: if request.auth != null;
}
// here's my problem
match /families/{anyDocument} {
allow read, write: if resource.data.parents.userId == request.auth.uid;
}
}
}
第二条规则试图说,"allow read and write of a family doc if the user's id is stored in one of the parents. The problem I'm having is syntax for "“parent之一。也许我可以说:
if ((resource.data.parents[0].userId == request.auth.uid) ||
(resource.data.parents[1].userId == request.auth.uid))
可以吗?即使我可以,出于编辑目的,数据可能包含多于或少于两个可以被视为 parent 的成年人。我什至接近?
查看 Firebase docs
你可以试试这个
match /families/{anyDocument} {
allow read: if request.auth != null;
allow write: if resource.data.parents.userId in [(request.auth.uid)];
}
表示如果您已通过身份验证则允许读取,如果您在 parents/userId
地图路径中列出则允许写入。
我没有检查自己,希望这对您有所帮助。