【问题标题】:Firebase database rule does not work with AngularFire collection readingFirebase 数据库规则不适用于 AngularFire 集合读取
【发布时间】:2020-05-18 12:50:53
【问题描述】:

我有以下数据库规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    function isAdmin() {
        return get(/databases/$(database)/documents/administrators/$(request.auth.uid)).data.admin == true;
    }
    match /users/{userId}/{document=**} {
      allow read: if request.auth.uid != null;
      allow write: if request.auth.uid == userId;
    }
    match /administrators/{document=**} {
      allow read, write: if isAdmin();
    }
    match /projects/{project} {
        function getRole(rsc) {
        return rsc.data.members[request.auth.uid];
        }
      allow create: if request.auth.uid != null;
      allow read: if request.auth.uid != null && ( getRole(resource) == 'owner' || getRole(resource) == 'participant' );
      allow write: if request.auth.uid != null && ( getRole(resource) == 'owner' );
    }
    match /statuses/{document=**} {
      allow read: if request.auth.uid != null;
      allow write: if isAdmin();
    }
  }

}

当我使用经过身份验证的 userId 测试读取集合 projects 的文档时,一切都很好。 当我尝试使用 AngularFire 读取同一个集合时,我收到错误 FirebaseError: Missing or enough permissions。

this.projectsCollection = this.afs.collection<Project>('/projects', ref =>
        ref.orderBy('projectId'));
this.projects = this.projectsCollection.valueChanges();
this.projects.subscribe(data => {
      this.isLoading = false;
    }, error => {
      this.isLoading = false;
      console.log(error);
    })

您能帮我找出错误吗? 谢谢

【问题讨论】:

    标签: javascript google-cloud-firestore firebase-authentication firebase-security angularfire2


    【解决方案1】:

    我确实更深入地阅读了文档,在这个链接上我发现了问题: https://firebase.google.com/docs/firestore/security/rules-conditions#rules_are_not_filters

    我使用过滤器更改了查询,使用成员数组而不是地图对象更改了集合:

    this.projectsCollection = this.afs.collection<Project>('/projects', ref =>
            ref
              .where('members', 'array-contains', this.userUid)
              .orderBy('projectId'));
    

    现在一切正常。

    【讨论】:

      【解决方案2】:

      使用成员字段作为查询的一部分:

      this.projectsCollection = this.afs.collection<Project>('/projects', ref =>
              ref.where(`members.${this.userUid}`, 'in', ['owner', 'participant'])
              .orderBy('projectId'));

      【讨论】:

        猜你喜欢
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 2018-08-12
        • 1970-01-01
        • 2018-01-19
        • 2021-04-17
        相关资源
        最近更新 更多