如何设置 firebase 读取规则并在匹配时读取所有兄弟姐妹

How to set firebase read rule and read all siblings if one matches

我将我的 firebase 规则设置如下:

    {
  "rules": {"users": {
       "$uid": {
         ".read": "auth != null",
        ".write": "$uid === auth.uid"

    }}
  }
}

这只允许写入具有匹配 uid 的节点并读取每个 uid 的所有子节点。但是我希望它就像我使用 uid 下的子节点进行查询一样,只能读取匹配的子节点及其兄弟姐妹...

例如这是我的 json 结构:

 {
"users" : {

      "AJkK4yZJsoseeefrJ7i6KIOUBDghtrhgthrtDi1" : {
        "lat" : 20.5001,
        "long" : 68.3755,
        "number" : "9876543210",
        "time" : 1499599788090
      }
    }
}

我想用号码查询,设置读取规则为只有号码匹配的地方才能读取经纬度和时间。规则怎么写?

更新:我现在的问题是,如果number的值在android中匹配,如何使用number查询数据库并获取其他siblings?我试过这个但没有用:}

  friend = mDatabase.getReference("users");
  friend.keepSynced(true);


  Query z = LocationActivity.this.friend.orderByChild("number").equalTo("9876054321");

  z.addListenerForSingleValueEvent((new ValueEventListener() {
        long lastseen;
        public void onDataChange(DataSnapshot dataSnapshot) {
            try {
                for (DataSnapshot zoneSnapshot: dataSnapshot.getChildren()) {
                    lastseen = (Long)zoneSnapshot.child("time").getValue();
                    friendLatitude = (Double) zoneSnapshot.child("lat").getValue();
                    friendLongitude = (Double) zoneSnapshot.child("long").getValue();
                }


            } catch (Exception e) {
            }}

它returns值为空,任何帮助将不胜感激。

为了查询每个节点,用户需要对父节点下的ALL个节点具有读取权限。

这是因为无法使用安全规则在 Firebase 中查询数据。

附加侦听器时会强制执行 Firebase 读取权限。为了查询一个节点,您必须对该节点具有读取权限(正如 Bradley 还解释的那样)。因此,为了能够查询用户,您必须对 /users 具有读取权限。由于任何 user that has read permission to /users can also read any data under that,您不能使用安全规则来过滤用户有权访问的节点。

这被称为 rules are not filters and is one of the common pitfalls for developers new to Firebase security model. I recommend that you read the documentation I linked already and some of the many questions/answer about the topic

您的用例最简单的解决方案似乎是将 .read 规则提升到 users:

{
  "rules": {
    "users": {
      ".read": "auth != null",
      "$uid": {
        ".write": "$uid === auth.uid"
      }
    }
  }
}

}

不确定这是否回答了问题,但我发现这个 post 正在寻找以下关于 Firebase 存储的解决方案.. 我可以控制哪些兄弟节点可以访问:

  match /uploadRoot/{userId}/{uploadCategory}/{allPaths=**} {

      // allow read access for client upload/download
      allow read, write:
        if request.auth != null
          && request.auth.uid == userId
          && (
              uploadCategory == 'userTextFiles'
              || uploadCategory == 'userImages'
            );