【问题标题】:Firebase rule that needs to check for value in array (contains() in security rules)需要检查数组中的值的 Firebase 规则(安全规则中的 contains())
【发布时间】:2015-11-13 13:01:36
【问题描述】:

如何在 firebase 规则中检查数组中的值。我正在尝试做这样的事情:

  root.child('connections/'+auth.uid).child('friends').child(someFriendId).exists()

所以在当前用户的connections节点下,如果friends数组中存在someFriendId,则允许访问。 'someFriendId' 不是朋友数组的键,它是使用 firebase push() 方法自动生成的 ID。

【问题讨论】:

  • 看起来这是 Firebase 安全规则的限制,因此数据应该构造为带有键的对象而不是数组。我不明白为什么会这样,如果您不能对数组的内容使用任何类型的安全性,为什么要启用 $FirebaseArray 和使用 push() 自动生成的密钥。如果有人能详细说明这一点,将不胜感激。

标签: angularjs firebase


【解决方案1】:

一般情况下,avoid arrays in distributed data,并在结构化数据指南中阅读 Arrays in Firebase

您不能在 Firebase 安全规则中执行“包含”。相反,您需要将用户存储为密钥,类似于 we demonstrate this use case in the security docs

因此,根据您的示例,唯一的更改是将数组索引替换为用户 ID,并将值更改为布尔值(或任何其他有用的值)。那么您的安全规则可以构造为:

root.child('connections/'+auth.uid+'/friends/'+someFriendId).exists();

【讨论】:

  • 谢谢,这就是我最终所做的。
猜你喜欢
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多