【问题标题】:security rules problems with firestoreFirestore 的安全规则问题
【发布时间】:2021-03-01 23:59:29
【问题描述】:

这里我有一点关于 Firestore 安全规则的问题

我创建了一个在线记事本,我希望这些笔记只能由 groupId 标识的特定用户访问。我指定我已经为我的城市指定了一个 groupId,以便能够将我识别的用户与我识别的城市相匹配。 就我的代码而言,一切都很好,但在 Firestore 规则级别上我没有成功。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users /{documents = **} {
        allow create,read,write : if request.auth != null
    }
    match /cities/{city}/citees/{citee}{
        allow read : if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.groupId == get(/databases/$(database)/documents/cities/$(city).data.groupId
        }
    }
}

【问题讨论】:

  • 我不是 100% 清楚你的功能目标是什么。您能否以更精确的方式解释它(图表?),特别是链接到城市的 groupId 和“某些用户”的 groupId 之间的区别。
  • 当然。只有与城市具有相同 groupId 的用户才能访问它。

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


【解决方案1】:

其中存在语法错误。在$(city) 之后似乎缺少)。应该是这样的:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users /{documents = **} {
        allow create,read,write : if request.auth != null
    }
    match /cities/{city}/citees/{citee}{
        allow read : if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.groupId == get(/databases/$(database)/documents/cities/$(city)).data.groupId
    }
  }
}

【讨论】:

  • 我想我明白了,我需要能够从我的用户数据库中检索变量,但在我看来,当我在城市匹配时,我无法访问它们,因为我收到一条错误消息: " 调用 [get] 函数时路径不正确:/databases/(默认)/documents/users/"
  • 您确定您的 Firestore 中有 users 集合吗?您可以在问题中添加数据库结构吗?
【解决方案2】:

是的,我可以访问数据库,因为当我尝试单独恢复我的“uers”集合时,我可以做到

【讨论】:

    猜你喜欢
    • 2019-08-14
    • 2021-01-16
    • 2019-08-20
    • 2019-02-06
    • 1970-01-01
    • 2018-10-19
    • 2019-07-05
    • 2020-06-19
    • 1970-01-01
    相关资源
    最近更新 更多