Firebase数据库规则:用户权限

虚幻

我想为不同的用户角色设置不同的权限。

例如,当用户不是超级所有者时,请勿让他在我的项目表中书写。

这是我尝试做的:

{
  "rules": {
    "items": {
        ".write": "auth.authority == 'super-owner'"
    }
  }
  }

但是我注意到该authority字段存储在我users自己创建的节点中,并已释放到Firebase中auth如何users->authority通过规则访问当前登录的用户?

更新:我确实尝试过:

".read": "root.child('users/'+auth.uid).authority.exists()",

但是我得到了错误保存规则-第10行:没有这样的方法/属性“权限”。

祖拉·塞赫尼亚什维利(Zura sekhniashvili)

之所以会出现此错误,是因为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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章