【发布时间】:2016-05-10 10:06:37
【问题描述】:
第一次在 firebase 工作并设置复杂的(对我而言)规则。
我有一组具有“parentId”属性的子项。我只希望某些角色和用户阅读此表。具体来说,我希望任何教练或裁判角色都能够像任何孩子的父母一样阅读。
我认为问题出在集合级别 .read 的“data.child('parentId').val() === auth.uid”。我觉得我完全遵循了这里的示例,但它仍然无法正常工作:https://www.firebase.com/docs/security/api/rule/data.html
谁能指出我在安全规则中做错了什么?
我知道安全规则不会过滤,我不认为这是该特定问题的一个示例,因为我的查询专门将结果限制为我试图通过安全规则允许的结果(尽管我可能弄错了)。
这是当前的安全规则。 cmets应该解释我的目标:
"children": {
// give ability of any coach or referee to read any child. Give refs ability to write to any child. Allow parent to query for their own children.
".read": "(data.child('parentId').val() === auth.uid) ||(root.child('users').child(auth.uid).child('role').val() === 'coach') || (root.child('users').child(auth.uid).child('role').val() === 'referee')",
".write": "root.child('users').child(auth.uid).child('role').val() === 'referee'",
".indexOn": ["parentId", "isApproved"],
"$child": {
// give a parent read and write access to only their specific children.
".read": "root.child('children').child($child).child('parentId').val() === auth.uid",
".write": "root.child('children').child($child).child('parentId').val() === auth.uid"
}
}
在我的代码中,我正在通过他们的 parentId 查询所有孩子。这是我的 javascript 失败并显示“错误:permission_denied:客户端无权访问所需数据。”:
factory.getChildrenByParent = function(parentId){
return new Promise(function(resolve, reject){
childrenRef.orderByChild('parentId').equalTo(parentId)
.once('value', function(childrenResponse){
var children = childrenResponse.val();
processChildren(children);
resolve(children);
},
function(err){
if(err){
reject(err);
}
})
})
}
cmets 应该解释我的目的。问题是用户无法通过父母 id(等于 auth.uid)查询孩子。 他们可以用这个查询一个:
factory.getChildById = function(id){
return new Promise(function(resolve, reject){
childrenRef.orderByKey().equalTo(id)
.once('value', function(childResult){
var childObjects = childResult.val();
processChildren(childObjects);
var child = Utilities.getFirst(childObjects);
resolve(child);
},
function(err){
if(err){
reject(err);
}
})
})
}
我尝试过使用模拟器,但我无法弄清楚 .orderByChild('parentId').equalTo(parentId) 的语法,并且 Chrome 开发工具网络在我触发此命令时未显示任何活动...
【问题讨论】:
标签: firebase firebase-security