【问题标题】:Firestore rules on subcollection子集合的 Firestore 规则
【发布时间】:2018-11-27 19:01:09
【问题描述】:

我在 Firestore 上有一个带有“级别”子集合的“游戏”集合。我正在尝试设置安全规则,以便您只能访问您创建的游戏或关卡。

所有文档(游戏或关卡)都有一个 authorId 字段,其中包含创建它们的用户的 uid。我尝试了这条规则,但仍然出现Missing or insufficient permissions 错误:

service cloud.firestore {
  match /databases/{database}/documents {    
    match /games/{document=**} {
        allow read, write: if document.data.authorId == request.auth.uid;
    }
  }
}

我错过了什么?

我也尝试了以下规则,但没有成功:

service cloud.firestore {
  match /databases/{database}/documents {    
    match /games/{game}/levels/{level} {
        allow read, write: if level.data.authorId == request.auth.uid;
    }
  }
}

service cloud.firestore {
   match /games/{game} {
     allow read, write: if game.data.authorId == request.auth.uid;     

       match /levels/{level} {
          allow read, write: if level.data.authorId == request.auth.uid;
       }
    }
}

【问题讨论】:

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


    【解决方案1】:

    根据参考文档,resource 是包含用户尝试写入的文档数据的对象。您可以使用其data 属性来获取其字段值。

    service cloud.firestore {
      match /databases/{database}/documents {    
        match /games/{document=**} {
            allow read, write: if resource.data.authorId == request.auth.uid;
        }
      }
    }
    

    【讨论】:

    • 啊是的,它有效。 document 只是文档名称,不是完整对象。
    【解决方案2】:
      match /databases/{database}/documents {
         match /users/{uid} {
          allow read, write: if request.auth.uid == uid;
        }
        match /data/{uid}/temperature
        /{document=**}{
        allow read, write: if request.auth.uid == uid;
        }
        match /data/{uid}/blood_pressure
        /{document=**}{
        allow read, write: if request.auth.uid == uid;
        }
       
      }
    }
    

    我这样做是为了只为经过身份验证的用户访问子集合“blood_pressure”和“temperature”。对我来说效果很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 2020-12-18
      • 2019-08-17
      • 1970-01-01
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多