【问题标题】:Rules Firestore, get resource from parent?规则 Firestore,从父级获取资源?
【发布时间】:2019-09-29 15:54:43
【问题描述】:

我正在尝试为我的应用设置安全规则。

我有一个如下所示的集合:

Scores
  |          
  |                  
  |                  
  |
  |___ AutoID______________history
  |     |___value: 22         |__20190506
  |     |___userID: "abc"     |___dateString:20190506
  |___ AutoID                 |___value:22
          |___value: 45
          |___userID: "def"

我认为这样做可以:

match /scores/{docID=**} {
    allow read: if userIsLoggedIn();
    allow create, update: if request.resource.data.userID == request.auth.uid;
    allow delete: if resource.data.userID == request.auth.uid;
}

用户可以达到分数,但不能达到Collection“历史”。 我认为问题来自resource.data.userID

Collection“历史”无法访问resource.data.userID。我想从父文档访问resource.data.userID

有可能吗?

【问题讨论】:

  • 我不确定我是否正确理解了您的问题/问题,所以只指出一些事情。 1)匹配中的/scores/{docID=**}表示这些规则授予的权限也适用于scores下的所有集合。 2) 读入安全规则本身不受安全规则影响。

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


【解决方案1】:

如果您要使用的文档字段不在正在检查与规则匹配的读/写访问权限的文档中,您将必须get() 另一个文档(即使它位于父路径中)使用其完整路径,以便您可以在规则中使用其字段值。阅读更多关于accessing other documents的文档。

如果您不在规则中使用 glob 通配符,而是按名称调用子集合,这可能会更容易,这样您就可以更轻松地使用单独的通配符值构建到父文档的路径。像这样的:

match /scores/{scoreId}/history/{id} {
  allow create, update:
    if get(/databases/$(database)/documents/scores/$(scoreId)).data.userID == request.auth.uid;
}

这不是一个确切的规则,但我希望你能看到我在这里做什么,以便制定你自己的规则。

【讨论】:

  • “如果你不使用通配符可能会更容易” 是否有允许使用通配符的答案?通配符使我免于在层次结构中编写一堆冗余规则。如果我想在某个地方看到父母,我会失去通配符的好处吗?
猜你喜欢
  • 1970-01-01
  • 2019-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-10
  • 1970-01-01
  • 2023-04-08
  • 2021-12-27
相关资源
最近更新 更多