Firebase - 另一条路径上基于用户的规则访问

Firebase - User Based Rule Access On Another Path

我希望仅当用户在其帐户下列出此设备 ID 时才允许读取 firebase 中的设备列表。

正如 Anant 在此处找到的答案中所推荐的那样:Security Rules - lists of data that can be read? 我实现了这个 firebase 数据结构:

{ 
  devices: { 
    111: {status: 10}, 
    222: {status: 5}
  }, 
  users:  {
    aaa: { 
      name: 'Jim', 
      devices: { 
        111: {name: 'Test Device'}
      } 
    } 
    bbb: { 
      name: 'Jane', 
      devices: { 
        111: {name: 'Test Device'},
        222: {name: 'New Device'}
      } 
    } 
  }
}

为了澄清输出,名为 Jim 的用户 "aaa" 应该只能从设备列表中读取设备 ID“111”并获取它的子元素。名为 Jane 的用户 "bbb" 可以看到设备“111”和“222”及其子元素

所以我实施了几个规则试验并以以下结果结束:

{
    "rules": {
        "users":{
          "$user_id":{
            ".read": "auth.uid == $user_id",
            "devices": {
              "$devices_id":{
                ".read":true
              }
            }
          }
        },
        "devices":{          
          "$devices_id":{
            ".read": "root.child('users').child(auth.uid).child('devices').hasChild('$devices_id')",
            ".write": false
          }
        }
    }
}

读取设备数据时,此规则应查看 /root/users//devices/$devices_id 并查看发出请求的用户下是否存在 device_id。然后允许读取该记录。根据上面链接的 post 这应该是可能的,但是 post 没有表达应该为 "conversations" 的主列表设置什么权限(在我的例子中是设备)...Anant只说 "Make sure to set the permissions on the top-level conversations list appropriately"。有人可以阐明如何做到这一点吗?

根据上述规则,我的这些请求仅被拒绝。我也试过: "root.child('users').child(auth.uid).child('devices').child('$devices_id').exists()" 这也行不通...

如能提供有关此过程的任何指导,我们将不胜感激。我也对实现这一目标的其他数据结构和规则结构持开放态度。这是一个新项目,现阶段可修改性很强。

谢谢!

感谢@Kato!上面的实现是正确的,除了 $device_id.

周围的字符串文字

改变

.hasChild('$devices_id')

.hasChild($devices_id)

修复了问题,仅允许从顶级设备结构中读取用户下分配的设备。