基于Firestore角色的规则

时间:2020-08-08 16:18:21

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

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

当前用户在名为if (user.emailVerified) {..}的集合下有一个文档,该文档的名为users的字段的值为role

admin

iOS代码获取数据

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
    }

用户集合

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

users。{userId} .tickets集合

{
    "role": "admin"
}

1 个答案:

答案 0 :(得分:2)

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

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

相关问题