单个属性的Firebase实时数据库规则与同一对象树中其他属性的规则不同

单个属性的Firebase实时数据库规则与同一对象树中其他属性的规则不同,firebase,firebase-realtime-database,firebase-authentication,firebase-security,Firebase,Firebase Realtime Database,Firebase Authentication,Firebase Security,我试图使firebase实时数据库规则正确运行,但单个属性规则存在问题 我的firebase对象类似于此示例 "products": { "KUg68BknfYWuEjAKla5": { "cat": "Pizzas", "likes": 132, "item_price": 39.9, "item_price_single": 39.9, "name": "Mussarela",

我试图使firebase实时数据库规则正确运行,但单个属性规则存在问题

我的firebase对象类似于此示例

"products": {
        "KUg68BknfYWuEjAKla5": {
          "cat": "Pizzas",
          "likes": 132,
          "item_price": 39.9,
          "item_price_single": 39.9,
          "name": "Mussarela",
          "observation": "",
          "product_id": "-KUg68BknfYWuEjAKla5",
          "product_image": "massapan-mussarela.jpg",
          "quantity": 1
        }
我现在对这个对象的规则是这样的

  "products": {
    ".read": true,
    ".write": "root.child('users').child(auth.uid).child('admin').val() == 'user_is_admin'" 
  ".indexOn": ["status", "categoryId"]
},
所以基本上我允许每个人读对象,但只有管理员写对象。我的问题是,单个属性“likes”需要由每个经过身份验证的用户编写。我通常会使用”。阅读:“auth!=null”,,但我不知道如何在我的规则中组合它们。我可以用.write设置多行吗?我需要把它们合并成一行吗?我尽了我所能,但没有成功


thx预先

您可以创建一个喜欢该类别/项目的用户列表,而不是存储喜欢的数量。大概是这样的:

likes: {
  userid_1: true,
  userid_2: true
}
这样,您可以允许用户仅编辑其路径。就像您通常使用用户列表一样:

".write": "auth.uid === $user"
真正的价值可以是任何东西,因为不喜欢内容的用户不会出现在列表中

您只需计算列表中的项目数即可获得喜欢的数量


不,您不能有多个写入规则。而是使用“.validate”。读写规则层叠,因此如果其中一个为真,则所有其他规则都将被忽略。验证规则不层叠,它们都需要为真

您可以指定对规则中特定子节点的访问权限。比如说

products
  product_0
    likes: 123
    item_price: 1.99
  product_1
    likes: 222
    item_price: 4.99
只允许所有人读取likes节点,但限制对管理员的写入的规则看起来是这样的(未测试,但与此类似)


另一方面,item_price节点可以被所有人写入和读取。任何人都无法访问其他子节点。

谢谢,我明白了,我将在下次更新时大胆地从产品列表中删除类似的节点。然而,自从有一个移动应用程序已经运行,我需要为所有用户更新(加上itunes上的空闲时间批准等),我首先按照Jay的建议设置规则(现在一个一个)。有趣的是,你们的评论验证了我在这种情况下如何使用validade而不是。写吗?你能给我举个例子吗?我是新手,这对我来说还是有点棘手。明白。这是另一个问题。我不能将root.child('users').child(auth.uid).child('admin').val()='user_is_admin'| |$id==auth.id'这两种不同的东西组合起来吗
{
  "rules": {
    "products": {
      "$each_product": {
        "likes": {
         ".read": true,
         ".write": "root.child('users').child(auth.uid).child('admin').val() == 'user_is_admin'"
        },
       "item_price": {
         ".read": true,
         ".write": true
       }
      }
    }
  }
}