【发布时间】: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