【问题标题】:Firestore security rules: get parameter to use in security rulesFirestore 安全规则:获取要在安全规则中使用的参数
【发布时间】:2020-07-07 05:05:43
【问题描述】:

我目前正在编写一些安全规则来保护我的 Firestore 数据库,但我遇到了一些问题。我希望能够从我的查询参数中获取数据以放入我的规则中(我希望能够使用clientId):

db.collection('Users').doc(userId).where('connections', 'array-contains', clientId).get();

我希望能够在我的 firestore 安全规则中使用 clientId,以便我可以使用该客户端 ID 查询另一个集合,因为我不认为 firestore 安全规则在 get 查询中提供“where”功能。这有可能吗?

像这样: request.auth.uid in get(/databases/$(database)/documents/Users/$(clientId)).data.connections

感谢您的帮助。

【问题讨论】:

  • 您找到解决方案了吗?我正在努力解决同样的问题。
  • @matsmats 不,我没有不幸。我认为目前不可能。

标签: firebase google-cloud-firestore firebase-authentication firebase-security


【解决方案1】:

我也为同样的问题苦苦挣扎了一段时间,终于设法解决了:

数据库

/familyMembers/{familyMemberId}
{
    parents 
        userId1
            userId: "userId1"
        userId2
            userId: "userId2"
}

安全规则

match /familyMembers/{familyMemberId}/{document=**} {
    allow read, write: if resource.data.parents[request.auth.uid] != null;
}

match /anotherCollection/{familyMemberId}/{document=**} {
    allow read, write: if request.auth.uid in get(/databases/$(database)/documents/familyMembers/$(familyMemberId)).data.parents;
}

从 Swift 代码查询

db.collection("familyMembers").whereField("parents.\(userId).userId", isEqualTo: userId).getDocuments()

参考:https://groups.google.com/forum/#!searchin/firebase-talk/jannica%7Csort:date/firebase-talk/1rTmJmNyJNQ/x2P8vPdqAQAJ

【讨论】:

  • 您的参考 URL 似乎没有为我返回任何合适的参考。可以验证吗?
  • @JeremyCaney 我不确定您通过验证是什么意思。这是我在 Firebase Google Group 中发布的一个非常相似的问题,我在此处发布的答案是对此的总结。
  • 当我点击链接时——至少在移动设备上——我只是被重定向到 Google 网上论坛主页。我没有链接到包含任何相关信息的页面。
  • 奇怪,当我在移动设备上打开链接时也会发生这种情况。如果你在电脑上打开它应该可以工作。
猜你喜欢
  • 2018-10-19
  • 2019-07-05
  • 2020-06-19
  • 2020-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多