Firebase规则导致奇怪的行为

时间:2017-03-01 18:08:13

标签: json firebase firebase-realtime-database firebase-security

对于下面的Firebase规则集。

"users": {

    //".read": "(auth != null) && (auth.uid === newData.child('id').val())",

  // A list of users and their associated metadata, which can be updated by the single user or a moderator.
  "$userId": {
    ".write": "(auth != null) && (auth.uid === $userId)",
    ".read": "(auth != null) && (auth.uid === $userId)",
    ".validate": "($userId === newData.child('id').val())",
    "invites": {
      // A list of chat invitations from other users, append-only by anyone.
      "$inviteId": {
        // Allow the user who created the invitation to read the status of the invitation.
        ".read": "(auth != null) && (auth.uid === data.child('fromUserId').val())",
        ".write": "(auth != null) && (!data.exists() || $userId === auth.uid || data.child('fromUserId').val() === auth.uid)",
        ".validate": "newData.hasChildren(['fromUserId','fromUserName','roomId']) && (newData.child('inviteId').val() === $inviteId)"
      }
    }
    }   
}

当我尝试使用规则模拟器作为用户2456gd时,/users/2456gd/下的网址$userId - 验证步骤成功。但是,如果尝试/users/2456gd/invites/abcd2 - $userId下的验证步骤失败。

我使用的数据:

{
"id":"2456gd",
"fromUserId":"2456gd",
"fromUserName":"abc",
"roomId":"family",
"inviteId":"abcd2"
}

任何可能的解释?以及如何处理它。

感谢。

Screenshot of the Simulator Results Details

1 个答案:

答案 0 :(得分:0)

正如你在这张照片中看到的那样

Validation failed

验证在/users/$userId/处失败,因为那里的数据没有id作为其子级。 /user/$userId的数据看起来像这样

{
    "invites": {
        "abcd2": {
            "id":"2456gd",
            "fromUserId":"2456gd",
            "fromUserName":"abc",
            "roomId":"family",
            "inviteId":"abcd2"
        }
    }
}

由于此子数据验证失败,因此该数据没有id。它只有一个invites的孩子。