【问题标题】:Firebase rules for collection group subcollection: resource.data is empty?集合组子集合的 Firebase 规则:resource.data 为空?
【发布时间】:2022-01-11 13:01:02
【问题描述】:

我使用集合分组的子集合,所以(在我的理解中)我必须使用通配符:

match /{path=**}/actions/{action} {
          allow read, write: if request.auth != null;
}

到目前为止,这是有效的。 在实施另一条规则时它也有效:

match /{path=**}/actions/{action} {
          allow read, write: if request.auth != null
                           && request.auth.token.email !='';
}

但是我想检查用户是否可以访问文档中的数据并且出现了问题:

match /{path=**}/actions/{action} {
          allow read, write: if request.auth != null
                           && request.auth.token.email in resource.data.access;
}
  

导致

FirebaseError: Missing or insufficient permissions.

与:

this.afs.collectionGroup("actions", ref => 
  ref.where("owner.email", "==", user.email)
).valueChanges({ idField: 'id' }).pipe(take(1))

到目前为止,我找不到有关如何访问通配符中数据的更多信息。

【问题讨论】:

  • 您能否编辑您的问题以显示也产生此错误的代码?
  • 我编辑了我的问题

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


【解决方案1】:

正在访问的文档的数据存在于resource 而不是request。所以规则应该是:

request.auth.token.email in resource.data.access;

请注意,此处resource 在操作子集合 (/actions/{doc}) 中有文档的数据。

您可以在documentation 中找到有关数据验证的更多信息。

【讨论】:

  • 你是对的,谢谢,我尝试了很多变种......错误仍然是一样的 - 我编辑了我的问题以获取正确的代码
  • @bastifix 您应该使用request.auth.token.email in resource.data.owner.email,因为电子邮件存储在嵌套字段owner.email 而不是数组中。如果规则是关于其他人访问该文档并且您在文档中的任何位置都有他们的电子邮件,那么请分享该文档的屏幕截图。
  • 我是否必须为我在 resource.data 中使用的字段创建索引?它现在正在使用索引,我试图理解为什么;-)
猜你喜欢
  • 2020-02-02
  • 2020-07-01
  • 2018-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
  • 2021-12-07
  • 1970-01-01
相关资源
最近更新 更多