Angular Firestore根据权限过滤文档

问题描述 投票:0回答:1

我在查询文档具有不同权限的集合时遇到问题。

无论如何都要对集合执行查询以防止在我无法读取的文档上获得权限被拒绝。如果我一次查询一个,我就能实现这一点。我需要查询集合以返回多个文档,其中我可能没有权限。我可以保留一个特定用户有权查询的文档ID列表,但是我找不到任何关于指定文档数组的文档。

采集:

Library > { libraryID } > {
   Users:{ uid, uid, uid },
   title: 'Title of library'
}

规则:

match /Library/{libraryID} {
  allow read: if get(/databases/$(database)/documents/Library/$(libraryID)).data.Users[(request.auth.uid)] == true;
}

如果用户标识位于数据中,则权限受到限制。

这很好用:

this.afs.collection<Library('Library').doc({libraryID}); // Everything works great

但是我需要集合中的多个项目,所以当我尝试下面的例子时,如果失败,因为我没有对集合中所有文档的权限:

this.afs.collection<Library('Library'); //PERMISSION DENIED

我希望我可以使用查询来解决这个问题,但因为它正在查看文档,它也会失败:

this.afs.collection<Library('Library',ref => ref.where('Users/'+this._uid, '==', 'true')); // PERMISSION DENIED

所以我希望我可以指定我可以访问的文档(这是我需要的)并将它们返回到集合中

this.afs.collection<Library('Library').doc([{libraryID},{libraryID2},{libraryID3}])

如果有人知道实现这一目标的解决方案,或者实现这一目标的结构重构,那将是一个巨大的帮助。

angularjs firebase google-cloud-firestore angularfire5
1个回答
0
投票

试试这条规则

match /Library/{libraryID} {
  allow read: if exists(/databases/$(database)/documents/Library/$(libraryID)/Users/$(request.auth.uid));
}

如果当前用户id出现在Users节点中,则允许读取。

© www.soinside.com 2019 - 2024. All rights reserved.