是否需要明确为每个子集合定义安全规则?

问题描述 投票:0回答:1

考虑Firebase文档中的示例

service cloud.firestore {
  match /databases/{database}/documents {
    match /cities/{city} {
      allow read, write: if <condition>;

        // Explicitly define rules for the 'landmarks' subcollection
        match /landmarks/{landmark} {
          allow read, write: if <condition>;
        }
    }
  }
}

这里的地标子集合具有明确的安全规则。我们假设我在城市下面有其他子集。如果我想要的规则与城市规则相同,我需要将它们定义为与地标一样的单独块吗?或者我可以将/cities/{city}改为/cities/{city=**}

哪种方法最好?感谢您的时间。

firebase google-cloud-firestore firebase-security-rules
1个回答
0
投票

从你的问题来看,我认为你有两个子集合,地标和大写字母;所以你需要不同的子集合'landmarks'规则。要实现这一点,你必须明确定义每个子集合安全规则。

你的规则应该是这样的,

service cloud.firestore {
match /databases/{database}/documents {
match /cities/{city} {
  allow read, write: if <condition>;

    // Explicitly define rules for the 'landmarks' subcollection
    match /landmarks/{landmark} {
      allow read, write: if <condition>;
    }

 // Explicitly define rules for the 'capital' subcollection
    match /capital/{capitals} {
      allow read, write: if <condition>;
    }
}
}
}

如果你写这样的规则

service cloud.firestore {
match /databases/{database}/documents {
match /cities/{city=**} {
  allow read, write;
}
}
 }

此规则将适用于cities集合中的每个子集合,即使您为某些子集合明确写入规则也是如此。

© www.soinside.com 2019 - 2024. All rights reserved.