首页 文章

Firebase数据库规则:用户权限

提问于
浏览
0

我想为不同的用户角色做出不同的许可 .

例如,当用户不是 super-owner 时,不要让他在我的项目表中写 .

这就是我试图做的事情:

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

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

更新:我试过这个:

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

但我得到 Error saving rules - Line 10: No such method/property 'authority'.

1 回答

  • 2

    您收到此错误,因为 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'"
    

    shortest version

    ".read": "root.child('users/'+auth.uid+'/authority').val() !== null",
    ".write": "root.child('users/'+auth.uid+'/authority').val() == 'super-owner'"
    

相关问题