Firestore对子集合的规则

时间:2018-06-18 21:36:18

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

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

所有文档(游戏或关卡)都有一个{ dogname: 'doggo', catname: 'attack', dogage: 51, catage: 98, key51: '', key73: '', key47: '' } 字段,其中包含创建它们的用户的uid。我已尝试此规则,但仍然出现authorId错误:

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;
    }
  }
}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

  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”和“温度”。对我来说效果很好。

相关问题