【问题标题】:Firestore Security Rules for collection within a document用于在文档中收集的 Firestore 安全规则
【发布时间】:2020-02-01 11:25:40
【问题描述】:

我正在尝试保护我在 Firestore 中的数据。我已经阅读了文档并设法为我的数据库的大部分内容安排了规则,但我遇到了一个特定的情况:

数据的结构是这样的:

collection("connections").document( Uid1 ).collection( Uid2 ).().()

我想允许 Uid1 和 Uid2 访问整个树。此功能的规则是什么?

【问题讨论】:

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


    【解决方案1】:

    您应该使用递归通配符语法{name=**},如doc 中所述。

    service cloud.firestore {   
        match /databases/{database}/documents {
         // Matches any document in the cities collection as well as any document in a subcollection.
            match /cities/{document=**} {
               allow read, write: if <condition>;
            }   
         } 
    }
    

    当使用递归通配符语法时,通配符变量将 包含整个匹配的路径段,即使文档是 位于深度嵌套的子集合中。例如,规则 上面列出的将匹配位于 /cities/SF/landmarks/coit_tower,以及文档的值 变量将是SF/landmarks/coit_tower

    请注意,您需要使用规则版本 2。

    【讨论】:

      【解决方案2】:

      感谢您的回答。我之前已经在使用通配符语法,但在这种特殊情况下,我很难将它与“userId”通配符一起实现。但我现在可以弄清楚了。如果有人想实现类似的结构,这是我的工作解决方案:

          match /connections/{userId}/{document=**} {
              allow read: if request.auth.uid == userId;
          }
      
          match /connections/{otheruser}/{userId}/{document=**} {
              allow read: if request.auth.uid == userId;  
          }
      
      

      【讨论】:

        猜你喜欢
        • 2020-06-19
        • 2018-09-26
        • 2019-07-23
        • 1970-01-01
        • 2019-10-15
        • 1970-01-01
        • 2019-03-03
        • 2019-02-01
        • 2020-12-26
        相关资源
        最近更新 更多