如何根据兄弟节点保护 Firebase 节点?
How to protect a Firebase Node depending on sibling Node?
我需要在 Firebase 中实施一些规则来保护我的数据。
假设我有节点 a 和节点 b。结构如下:
{
a:
[
a1: {uid: 123},
a2: {uid: 321},
a3: {uid: 567}
],
b:
[
a1: {data: 'foo.bar'},
a2: {data: 'foo.bar'},
a3: {data: 'foo.bar'}
]
}
如上例,a的子节点是a的keys节点 b。如何根据节点 b 的 uid 限制对节点 b 的访问 data =]a?
例如。节点a中uid123的user,他的key是a1,所以,在节点b中只能从子节点a1中获取数据(b -> a1).
我认为这样的规则系统符合您的要求:
{
"rules": {
"b": {
"$key": {
".read": "auth.uid == root.child('a').child($key).child('uid').val()",
".write": "auth !== null"
}
}
}
}
在此示例中,$key
是一个 $location
variable,它匹配节点列表 b
下的任何键。
然后我们可以使用这个 $key
变量来匹配 current user's auth.uid
to the uid
of the corresponding node under a
, using root.child()
.
结果是,如果 a
下具有相同键的对应子项的 uid
与当前用户的 uid
.
我需要在 Firebase 中实施一些规则来保护我的数据。
假设我有节点 a 和节点 b。结构如下:
{
a:
[
a1: {uid: 123},
a2: {uid: 321},
a3: {uid: 567}
],
b:
[
a1: {data: 'foo.bar'},
a2: {data: 'foo.bar'},
a3: {data: 'foo.bar'}
]
}
如上例,a的子节点是a的keys节点 b。如何根据节点 b 的 uid 限制对节点 b 的访问 data =]a?
例如。节点a中uid123的user,他的key是a1,所以,在节点b中只能从子节点a1中获取数据(b -> a1).
我认为这样的规则系统符合您的要求:
{
"rules": {
"b": {
"$key": {
".read": "auth.uid == root.child('a').child($key).child('uid').val()",
".write": "auth !== null"
}
}
}
}
在此示例中,$key
是一个 $location
variable,它匹配节点列表 b
下的任何键。
然后我们可以使用这个 $key
变量来匹配 current user's auth.uid
to the uid
of the corresponding node under a
, using root.child()
.
结果是,如果 a
下具有相同键的对应子项的 uid
与当前用户的 uid
.