是否有针对firebase应用中受保护内容的正确授权规则的最佳做法方法
到目前为止我的一些研究:
方法1:秘密网址
我需要知道能够查看/编辑文档的网址
不是真正的授权,因为任何有权访问该URL的登录用户都可以编辑/修改它。
无法索引我有权访问的所有文档
方法2:使用firebase授权规则将用户添加到文档中,并在读/写之前检查用户是否为document.users。
取自: Protected content in Firebase possible?
{
"documents": {
"$documents_id": {
// any friend can read my post
".read": "auth.id === data.child('owner').val() || root.child('users/'+data.child.owner.val()+'/users/'+auth.id).exists()",
// any friend can edit my post
".write": "auth.id === data.child('owner').val() || root.child('users/'+data.child.owner.val()+'/users/'+auth.id).exists()"
},
users:{
// List of user.ids that have access to this document
}
}
}
优点:
缺点:
方法3 :Firebase授权规则(方法2),以及每个用户都有权访问的document_ids数组的冗余用户存储。此用户存储仅用于查询用户有权访问的所有文档。 即:
{
"documents": {
"$documents_id": {
// any friend can read my post
".read": "auth.id === data.child('owner').val() || root.child('users/'+data.child.owner.val()+'/users/'+auth.id).exists()",
// any friend can edit my post
".write": "auth.id === data.child('owner').val() || root.child('users/'+data.child.owner.val()+'/users/'+auth.id).exists()"
}
},
"users":{
"$user":{
".read": "auth.id=$user.id",
".write": "auth.id=$user.id"
"$documents":{
// All the documents i have access to. This list gets ammended whenever I am granted/stripped access to a document.
}
}
}
}
优点:
缺点:
方法4:群组
按Granting access to Firebase locations to a group of users
使用群组我们为数据存储中的每个文档都有一个组
无法轻松查询firebase以获取用户可以访问的所有文档
有更好的方法吗?
答案 0 :(得分:13)
你已经很好地枚举了选项,而且你肯定是在正确的轨道上。正如您所发现的那样,无法根据安全规则进行查询。这是故意完成的,因为(取决于您的安全规则)这可能非常昂贵(Firebase通常会因此而避免复杂查询)。
所以你的方法3是完全正确的方法。为这些情况复制数据实际上是一种非常常见的做法。有关此内容更详细的博客文章,请参阅Denormalizing Your Data is Normal。
您还可以使用重复的文档列表执行方法1。如果您希望能够仅使用URL(包含密钥ID)将某人“邀请”到文档,这将非常有用。或者你可以将两者结合起来(有些文件是“公开但不公开”,有些文件是“私人邀请朋友”或其他什么。)