【问题标题】:Firestore security rules based on role基于角色的 Firestore 安全规则
【发布时间】:2019-06-15 12:25:27
【问题描述】:

我已经编写了这些 Firestore 安全规则,只允许获得批准的用户使用数据:

service cloud.firestore {
  match /databases/{database}/documents {

    match /bikes/{document} {

     function getRole(role) {
        return get(/databases/$(database)/documents/employees/$(request.auth.uid)).data.isApproved
      }

      allow read: if getRole() == true;
      allow write: if getRole() == true;

    }
  }
}

但出现此错误

core.js:12501 ERROR 错误:权限缺失或不足。 在新的 FirestoreError (index.cjs.js:346)

怎么了?

附加代码:

1) Firestore record

1) 角度代码

getUserData() {
    this.afAuthState.authState.subscribe(user => {
      console.log(user)
      console.log("user uid")
      console.log(user.uid)
      if (user) {
          this.dao.getEmployeeByUID(user.uid).subscribe( res => {
            if (res) {
              console.log(res)
              this.employee = res
              this.role = this.employee.role
            }
            else console.log("please ask the admin for confirmation")
          })
        }
        else console.log("please login")
      })
  }

当我应用规则时,我无法获取此 (getEmployeeByUID) 数据。当没有规则时 - 很好

【问题讨论】:

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


【解决方案1】:

您的规则是在名为 docs 的集合下查找匹配项,但您没有这样的集合。看来您的规则应该放在名为 employees 的集合上。

【讨论】:

  • 没关系,我用的是“自行车”系列
  • 您所做的只是更新您的规则以使用“bikes”集合而不是“docs”。这是你调用getEmployeeByUID时查询的集合吗? (无法判断 - 您应该显示正在执行的确切查询。)
  • getEmployeeByUID(uid) { return this.afs.doc(this.fsCollectionName+'/'+uid).valueChanges(); } 这个?它是一个简单的查询。 fsCollectionName = "员工";
  • fsCollectionName 是什么?您到底要在这里查询什么集合?
  • 它是对 firestore 集合的字符串引用
猜你喜欢
  • 2020-05-23
  • 2020-11-28
  • 1970-01-01
  • 2018-03-22
  • 2018-11-27
  • 2019-07-23
  • 1970-01-01
  • 2018-08-04
  • 2019-04-26
相关资源
最近更新 更多