FireStore:获得匹配时权限被拒绝

时间:2017-12-24 06:44:08

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

我有下一个firestore规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{usuarios=**} {
        allow write: if get(/usuarios/$(request.auth.uid)).data.level == 0 || get(/usuarios/$(request.auth.uid)).level == 0;
    }
  }
}

我得到了#34;许可被拒绝"当我尝试这个查询时:

firebase.firestore().collection('usuarios').doc(uid).set({...});

这实际上是我的数据库:

enter image description here

pd:我想在我的数据库中添加有关新用户的信息(通过他的UID)

1 个答案:

答案 0 :(得分:2)

由于您按get个请求付费,因此费用非常昂贵且效率低下。相反,您应该编写规则,就好像您正在定义数据库的结构一样。这就是我的意思:

service cloud.firestore {
  match /databases/{database}/documents {
    match /usuarios/{uid} {
      // Give write access to any field within the document who's id is the uid
      // Add other validation as needed.
      allow write: if uid == request.auth.uid
          // Give admin access to anyone who's `level == 0`
          // Make sure to add the `databases...` boilerplate
          || get(/databases/$(database)/documents/usuarios/$(request.auth.uid)).data.level == 0;

      // If necessary, give access to sub-collections.
      // (We can't do it for uid or it will become a path object that we can't use as a string.)
      /{sub=**} {
        allow write: if uid == request.auth.uid;
      }
    }
  }
}

如果您希望看到完全刷新的规则做得非常相似,我会有open source exemple。干杯!