【问题标题】:Firestore rules: why the caller does not have permission to execute the specified operation?Firestore 规则:为什么调用者没有执行指定操作的权限?
【发布时间】:2021-12-26 16:21:39
【问题描述】:

我有一个包含以下字段的文档的集合

  • uid:创建文档的用户的用户ID
  • anotherId:它是另一个 id
  • andAnotherId: 是另一个id
  • date:创建日期,以秒为单位加上 1,000,000 秒

在颤振中我有以下查询

    _firestore
      .collection('subscriptions')
      .where('anotherId', isEqualTo: '1')
      .where('andAnotherId', isEqualTo: '2')
      .where('date', isGreaterThanOrEqualTo: dateInSeconds)
      .limit(1)
      .get()
      .then(....

我想设置一个规则,只有创建文档的用户才能阅读该文档。我已经写了以下内容,但它不起作用。有谁知道为什么??

    match /subscriptions/{item} {
      allow read: if request.auth != null
                  && request.auth.uid == resource.data.uid;
   }

请注意,如果我编写此规则,它会起作用(但我想避免其他用户可以读取其他用户创建的文档)

    match /subscriptions/{item} {
      allow read: if request.auth != null;
   }

【问题讨论】:

  • 您能分享该集合中文档的屏幕截图吗?还可以尝试记录当前用户的 UID 并检查它是否与您文档中的 UID 匹配。
  • 您好,我已经添加了我的文档的屏幕截图。 firestore 中的 uid 与我登录时在 Flutter 应用中看到的相同
  • 请注意,我的集合中的某些文档可能没有 uid 字段,但是查询的其余部分应返回带有 uid 字段的文档。我不确定是否所有文档都必须具有 uid 字段才能使其正常工作

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


【解决方案1】:

我已经通过如下更改查询来解决,但我仍然不明白为什么它之前不起作用

_firestore
  .collection('subscriptions')
  .where('uid', isEqualTo: uid)
  .where('anotherId', isEqualTo: '1')
  .where('andAnotherId', isEqualTo: '2')
  .where('date', isGreaterThanOrEqualTo: dateInSeconds)
  .limit(1)
  .get()
  .then(....

【讨论】:

  • 之前没用,因为rules are not filters。相反,他们只是检查读取操作是否符合条件。因此,您的规则中的request.auth.uid == resource.data.uid 表示“为了允许读取操作,它必须仅查询uid 字段与登录用户相同的文档”。由于您在此答案的查询中添加了该条件,因此现在规则允许。
  • 我明白了。谢谢
猜你喜欢
  • 2021-07-25
  • 2020-07-13
  • 2021-05-20
  • 2021-03-03
  • 2019-06-26
  • 1970-01-01
  • 2020-03-31
  • 2021-08-09
  • 1970-01-01
相关资源
最近更新 更多