【问题标题】:How to write a security rule to allow read without using auth variable如何编写安全规则以允许在不使用 auth 变量的情况下读取
【发布时间】:2017-07-16 02:36:36
【问题描述】:

我正在尝试编写安全规则,但我在编写它时有点困惑。就我而言,我没有使用 Firebase 对用户进行身份验证,但我在数据库中有节点,该节点具有由用户名命名的子节点。我正在尝试实现这样的逻辑:对于该节点的任何子节点,如果值为真,那么他可以继续前进,否则不能。这是我的示例节点

"Customers":{
    "John":"true",
    "Jack":"false"
  }

  "Messages":{
     "Message1":{
  ....
   }, 
  },

这是我感到困惑的规则节点。我尝试使用“$”通配符变量,但出现该变量未知的错误。

  "rules":{
    "Messages":{
     ".read":"root.child('Customers').child($name).val()===true",
     ".write":"root.child('Customers').child($name).val()===true"
    }
  }

我认为“$”变量不能这样使用。那么我该怎么做呢?

【问题讨论】:

    标签: firebase-realtime-database firebase-security


    【解决方案1】:

    您如何决定检查哪个用户的价值?您必须有一个值进行比较。如果您希望用户可以看到自己的消息这样的逻辑,您应该在消息节点下添加一个Username 字段。喜欢;

    "Messages":{
         "Message1":{
           "John": {
            },
         ....
       }, 
      }
    

    你可以通过这个字段做到这一点;

    "rules":{
        "Messages":{ 
          "$userId" : {
             ".read":"root.child('Customers').child($userId).val()===true",
             ".write":"root.child('Customers').child($userId).val()===true"
           }
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      相关资源
      最近更新 更多