带有通配符的 Firebase 安全规则

Firebase security rules with wildcards

我有这样的数据结构:

如何在 Firebase 安全规则中访问 /Restaurant/-KK37k6g5cYYippEHpZ3/User/-KK37k6g5cYYippEHpZ4/id 的值?两个按键应该是通配符。我需要这样的东西:

"Restaurant": {
        "$id": {
            ".read": "auth.uid != null",
            ".write": "data.child($id).child('User').child($anotherWildcard).child('id').val() === auth.uid"  
        }
    }

不确定我是否完全理解您的要求,但这是我的想法。

您的规则中的第一个问题是您指定了 child($id),但您已经在 $id 内。它隐含在您的 data 中,您指的是 $id.

要解决您的主要问题,您不需要另一个通配符。您可以只使用 hasChild 来验证 auth.uid 是否在 restaurant/user.

"Restaurant": {
   "$id": {
      ".read": "auth.uid != null",
      ".write": "auth.uid != null && data.child('User').hasChild(auth.uid)"
   }
}