我想为不同的用户角色设置不同的权限。
例如,当用户不是超级所有者时,请勿让他在我的项目表中书写。
这是我尝试做的:
{
"rules": {
"items": {
".write": "auth.authority == 'super-owner'"
}
}
}
但是我注意到该authority
字段存储在我users
自己创建的节点中,并已释放到Firebase中auth
。如何users->authority
通过规则访问当前登录的用户?
更新:我确实尝试过:
".read": "root.child('users/'+auth.uid).authority.exists()",
但是我得到了错误保存规则-第10行:没有这样的方法/属性“权限”。
之所以会出现此错误,是因为root.child('users/'+auth.uid)
返回的实例当然DataSnapshot
没有authority
属性。但是它有hasChild
方法。
有关方法的完整列表,DataSnapshot
请访问以下链接。
https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot。
试试这个。
".read": "root.child('users/'+auth.uid).hasChild('authority')",
".write": "root.child('users/'+auth.uid).hasChild('authority') && root.child('users/'+auth.uid+'/authority').val() == 'super-owner'"
或这个
".read": "root.child('users/'+auth.uid+'/authority').exists()",
".write": "root.child('users/'+auth.uid+'/authority').exists() && root.child('users/'+auth.uid+'/authority').val() == 'super-owner'"
或最短版本
".read": "root.child('users/'+auth.uid+'/authority').val() !== null",
".write": "root.child('users/'+auth.uid+'/authority').val() == 'super-owner'"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句