用于在文档中收集的Firestore安全规则

时间:2020-02-01 11:25:40

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

我正在尝试在Firestore中保护我的数据。我已经阅读了文档,并设法为我的数据库的上半部分安排了规则,但是我遇到了某些情况:

数据结构如下:

collection(“ connections”)。document(Uid1).collection(Uid2)。()。()

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

2 个答案:

答案 0 :(得分:2)

您应使用doc中所述的递归通配符语法{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。

答案 1 :(得分:0)

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

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

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