我想知道是否有可能以这样的方式实施 Firebase 安全性,以允许我的应用程序的每个用户完全访问他们自己位置的数据,同时允许他们自己的用户启用或禁用访问类型其他用户自己的数据?或者基本上,是否可以在我的应用程序的用户之间以严格执行的方式实现简单的共享、Dropbox 或 Google Drive 样式?
问问题
2241 次
1 回答
5
由于 Firebase安全规则允许您引用 Firebase 中的数据,因此您可以将安全规则建立在您可以为其创建数据的任何内容上。所以是的,您可以允许用户以您可以计划的任何方式共享他们自己的数据。
为了设计一个基于保管箱想法的简化示例,我可以在我的数据下有一个“共享”文件夹,以及一个存储访问权限的安全文件夹:
/security/$user_id/$friend/... // where I put the access rights
/folders/$user_id/shares/... // where I put the shared files
现在我可以通过将用户名和他们可以访问的文件夹列表放入我的 Firebase 数据来控制对它的访问:
/security/$user_id/$friend_id = /never/gonna/give/you/up = true
现在在我的安全规则中,我可以这样写:
{
"security": {
"$user_id": { // only authenticated user may read/write his rules
"shares": {
".read": "auth.id === $user_id",
".write": "auth.id === $user_id"
}
}
}
"folders": {
"$user_id": {
// only authenticated user may read/write his folders
".read": "auth.id === $user_id",
".write": "auth.id === $user_id",
"shares": {
// but my friends can read data in shares
".read": "root.child('security/'+$user_id+'/'+auth.id+'/shares').val() === true"
}
}
}
}
请注意,对此(暂时)的一个限制是安全规则不能递归或以任何嵌套方式工作。但是,由于规则是允许的(如果路径的任何父级允许访问,则允许访问),您可以解决此问题。
您可能需要对子路径的最大数量设置硬性限制,并在规则中手动声明它们,如下所示:
// allow sharing up to 3 levels deep
"shares": {
".read": "root.child('security/'+$user_id+'/'+auth.id+'/shares').val() === true",
"$child1": {
".read": "root.child('security/'+$user_id+'/'+auth.id+'/shares/'+$child1).val() === true",
"$child2": {
".read": "root.child('security/'+$user_id+'/'+auth.id+'/shares/'+$child1+'/'+$child2).val() === true",
"$child3": {
".read": "root.child('security/'+$user_id+'/'+auth.id+'/shares/'+$child1+'/'+$child2+'/'+$child3).val() === true",
}
}
}
}
不是最漂亮的东西,而是一个很好的临时解决方案,直到 Firebase 获得一些嵌套功能。
于 2013-09-16T16:02:49.837 回答