使用 Firebase RuleDataSnapshot 方法访问用户数据

Using Firebase RuleDataSnapshot Methods to access user data

如果我有这样的数据:

        root{
           PrivateRooms{
             <PrivateRoomName>{
               ...
             }
           }
           users{
             <uid>{
               permissionFrom{
                   <PrivateRoomName>:value is not private room name 
               }
                ...
             }
           }
         }

...我如何编写规则以根据用户的权限授予对 的访问权限children?

我认为这行得通:

{
  "rules": {
    "PrivateRooms" : {
      "$roomName":{
        ".read" : "root.child('users').child(auth.uid).child('permissionFrom').hasChild('$roomName') == true",
        ".write" : "root.child('users').child(auth.uid).child('permissionFrom').hasChild('$roomName') == true"
      }
    },
    "users" : {
      ".read" : true,
      ".write" : true
    }
  }
}

...但是一定是规则有问题,因为这个数据快照被跳过了:

var testRef = new Firebase('https://my.firebaseio.com/privateRooms/<privateRoomName>/privRoomChild');
testRef.once("value", function(snapshot){
    var testVal = snapshot.val();
    console.log(testVal);
});

在您的规则中,$roomName 是一个变量,因此不应用引号引起来。

  "$roomName":{
    ".read" : "root.child('users').child(auth.uid).child('permissionFrom').hasChild($roomName)",
    ".write" : "root.child('users').child(auth.uid).child('permissionFrom').hasChild($roomName)"
  }