【问题标题】:How do I set rules in firestore for user groups如何在 Firestore 中为用户组设置规则
【发布时间】:2019-08-29 00:35:57
【问题描述】:

我是 firestore 新手,我尝试从一组用户那里获取所有资源。 就我而言,我使用身份验证令牌来跟踪用户组(我也会在云存储上使用它)。

组对象类似于:

{"managers": { "user1_ID": "owner", "user2_ID": "client" } }

然后在身份验证令牌中设置组 UID:

{ "groups": { "group1_ID": "owner", "group2_ID": "client" } }

然后在 Firestore 规则中我想设置如下内容:

 match /groups/{groupId} {        
   allow create: if request.auth.uid != null;
   allow read: request.auth.token[groupId] != null;
   allow delete, update: if request.auth.token[groupId] == 'owner';
 }

但是现在,当我拥有组 ID 并且读取文档的规则不允许我找到用户所在的所有组时,我可以获取或更新文档。

我尝试运行的代码是:

this.unsubscribe = db.collection("groups").onSnapshot(querySnapshot => {
   var groups = [];
   querySnapshot.forEach(function(doc) {
      groups.push({uid: doc.id, ...doc.data()});
   });
   this.setState({sites})
})

我认为(但尚未测试)的唯一解决方案是从身份验证令牌中获取每个组并请求每个组,但我认为这可能不是一个好方法。

我已经使用 firestore 资源对象 name 和 id 进行了测试,甚至在文档中插入了 id。

【问题讨论】:

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


    【解决方案1】:

    我终于明白了。 对于谁正在尝试这样的东西,这是我的代码。

    规则:

    match /groups/{groupId} {        
      allow create: if request.auth.uid != null;
      allow read: if resource.data.managers[request.auth.uid] != null;
      allow delete, update: if resource.data.managers[request.auth.uid] == "owner";
    }
    

    客户端JS:

    this.unsubscribe = db.collection("groups").where(`managers.${firebase.auth().currentUser.uid}`, ">", "").onSnapshot(querySnapshot => {
      var groups= [];
      querySnapshot.forEach(function(doc) {
        groups.push({uid: doc.id, ...doc.data()});
      });
      this.setState({groups})
    })
    

    【讨论】:

      猜你喜欢
      • 2019-09-15
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      • 1970-01-01
      • 2019-11-03
      相关资源
      最近更新 更多