首页 > 解决方案 > 用于在文档中收集的 Firestore 安全规则

问题描述

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

数据的结构是这样的:

集合(“连接”).document(Uid1).collection(Uid2).().()

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

标签: firebasegoogle-cloud-firestorefirebase-security

解决方案


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

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。


推荐阅读