保护多个用户Firestore的数据

时间:2018-02-07 15:18:37

标签: security firebase firebase-realtime-database firebase-authentication google-cloud-firestore

我如何设置Firestore规则,以便以下列方式仅限某些用户访问某些集合文档/集合:

SSH_PID

事情是,我想只允许读取和写入“client-1-store”文档及其文档和子集合,如果当前登录用户的id设置在每个用户允许的数组内“商店“,但我不知道我怎么能这样做......

1 个答案:

答案 0 :(得分:1)

创建map of values

,而不是创建用户ID数组
//base collection
clients:{
  //document
  client-1-store:{
    users-allowed:{
      "user-1-id": true,
      "user-2-id": true,
      "user-3-id": true,
      "user-4-id": true,
    }
  }
}


match /clients/{storeId=**} {
  allow read: if get(/databases/$(database)/documents/clients/$(storeId).data.users-allowed.$(request.auth.uid)) == true;
}

但是,这并不具有allowedUsers子集合,每个用户ID都保存为文档。然后,您可以使用exists条件。这还允许您为用户的文档添加更多粒度以获取详细的CRUD权限。

相关问题