【问题标题】:Firebase Rules Using root [duplicate]Firebase 规则使用 root [重复]
【发布时间】:2017-12-12 00:43:48
【问题描述】:

数据库:

"user_locations": { 
   "123": {
     "user_id": "kcf3566"
   }

}

规则:

"user_locations": { 
   "$loc_id": {
     ".read": "auth.uid == root.child('user_locations/$loc_id/user_id').val()",
     ".write": "auth != null"
   }

}

根据上面的代码,我试图只允许在路径 user_locations > $key > uid 具有匹配 uid 的用户能够读取数据。我尝试了上面的规则,但是我无法访问数据。

触发问题的代码:

$scope.get_user_locations = function () {

    firebase.auth().onAuthStateChanged(function (user) {
        if (user) {

            var returned_locations = firebase.database().ref('user_locations/');
            returned_locations.on('value', function (snapshot) {
                $scope.user_locations = snapshot.val();
            });


        } else {
            $state.go('login');
        }
    });

};

【问题讨论】:

  • 你能显示你正在尝试但不起作用的代码吗?
  • 顺便说一下,这是一个 AngularJS 1 项目。这是 user_locations 方法:pastebin.com/w4TKn24v
  • Firebase 在您附加侦听器时会检查安全规则。您将侦听器附加到user_locations。由于您没有对/user_locations 的读取权限,因此该侦听器被拒绝。
  • 您似乎正在尝试使用安全规则来过滤用户有权访问的数据。目前这是不可能的:Firebase 安全规则不能用于过滤数据。这在文档中称为rules are not filters,在previous questions about that topic 中也有相当多的介绍。

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


【解决方案1】:

安全规则不对路径字符串中的变量执行字符串插值。将您的规则更改为:

"rules": {
  "user_locations": { 
     "$loc_id": {
       ".read": "auth.uid == root.child('user_locations').child($loc_id).child('user_id').val()",
       ".write": "auth != null"
     }
   }

【讨论】:

  • 过去尝试过您的建议,但没有任何运气...
  • 嗯。该规则适用于我在 Firebase 控制台中使用模拟器。正如弗兰克在评论中所问的那样,请发布尝试读取该位置的代码。
  • 您的代码在/user_locations 读取,而不是/user_locations/$loc_id,因此不应用该规则。
  • @BobSnyder 对我来说,读取规则似乎与"auth.uid == data.child('user_id').val()" 相同,这使得正在发生的事情更清楚。请参阅我对问题的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-20
  • 2017-10-27
  • 2017-08-05
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 2021-09-30
相关资源
最近更新 更多