【问题标题】:Different security rules for different Firestore collections不同 Firestore 集合的不同安全规则
【发布时间】:2019-02-24 20:28:59
【问题描述】:

我目前是第一次使用 Firestore,并尝试稍微了解一下安全规则。我现在的问题很简单,我可以通过做更多的研究找到答案,但我想确保我做的是正确的事情,所以我认为在这里问会更好。

如果我在 Firestore 中有两个集合,一个名为“A”,另一个名为“B”,如果我只想让经过身份验证的用户在 A 和每个人中读取、写入、更新、删除...在 B 中读取,但只是经过身份验证的用户在 B 中写入、更新、删除...。

编辑: 以下是他们将 B 的规则应用于所有集合的当前规则:

service cloud.firestore {
   match /databases/{database}/documents {
      match /{document=**} {
        allow read: if true;
        allow write: if request.auth.uid != null;
      }
   }

}

【问题讨论】:

  • 请把 Firestore 规则放到帖子里。 (我在我的手机上,所以更容易直接从这里阅读),我会尽力帮助你。 :)
  • firebase.google.com/docs/firestore/security/rules-structure - 以下是您如何构建它的方法。使用“match /{}”类似这样的东西:match / {B}

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


【解决方案1】:

如果你查看documentation on authentication in security rules,你会发现这些规则:

service cloud.firestore {
  match /databases/{database}/documents {
    // Allow the user to access documents in the "cities" collection
    // only if they are authenticated.
    match /cities/{city} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

针对您的用例进行了修改,类似于:

service cloud.firestore {
  match /databases/{database}/documents {
    match /A/{id} {
      allow read, write: if request.auth.uid != null;
    }
    match /B/{id} {
      allow read;
      allow write: if request.auth.uid != null;
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    相关资源
    最近更新 更多