【问题标题】:Firestore role based ruleFirestore 基于角色的规则
【发布时间】:2020-11-28 17:26:22
【问题描述】:

我有以下规则应该允许我阅读文档,但我收到权限不足错误。有什么建议吗?

当前用户在名为 users 的集合下有一个文档,其中包含一个名为 role 的字段,其值为 admin

service cloud.firestore {
  match /databases/{database}/documents {
  
    match /users/{userId}/tickets/{ticketId} {
        allow update, delete, create: if false
        allow read: if get(/users/$(request.auth.uid)).data.role == "admin"
    }
  
    match /users/{userId} {
        allow delete: 
        if false
      
        allow read, write: 
        if request.auth != null && request.auth.uid == userId
      
      allow update: 
        if resource.data.points == request.resource.data.points && request.auth != null && request.auth.uid == userId
    }

iOS 代码获取数据

    func fetchTickets(contestId: String) -> SignalProducer<[Ticket], FetchError> {
        Firestore.firestore()
            .collection("users/*/tickets")
            .whereField("contest.id", isEqualTo: contestId)
            .getDocuments()
    }

用户集合

{
    "role": "admin"
}

users.{userId}.tickets 集合

{
    "contest": {
         "id": ""asdasd
    }
}

【问题讨论】:

  • 这里没有足够的信息。请编辑问题以显示所涉及的实际数据,以及执行被拒绝查询的客户端代码。我们需要查看整个系统中的数据流。

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


【解决方案1】:

首先,Firestore 查询不支持通配符。您将需要识别 单个 用户的票证并仅查询该一个子集合。或者,您需要对票证执行collection group query 以查询所有名为票证的子集合。如果您使用集合组查询,则需要completely different rules 来支持它。

其次,security rules are not filters。请务必仔细阅读该文档。您不能让规则根据另一个文档的 get() 过滤掉某些文档。这根本无法按照 Firestore 所需的方式进行扩展。客户端必须能够在查询中制定过滤器,这要求要过滤的数据必须在被查询集合中的文档中(它们不能在其他文档中)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-15
    • 2020-05-23
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多