简体   繁体   English

在 Security Firstore Rules 中是否可以只让用户根据字段值读取数据?

[英]in Security Firstore Rules does it possible to only make users to read data depending on field value?

for example in Firstore i have collection called products and every doc has boolen field called isAllow: false例如,在Firstore中,我有一个名为products的集合,每个文档都有一个名为isAllow: false的布尔字段

now in Security Firstore Rules How to make users can read only the docs with true value of isAllow field and the same with write.. i read the documentation but couldn't understand exactly what i want现在在 Security Firstore Rules How to make users can only read only the docs with true value of isAllow field and the same with write.. 我阅读了文档,但无法准确理解我想要什么

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

As Dharmaraj answered, you can allow only reading of those documents in security rules with:正如 Dharmaraj 回答的那样,您可以只允许阅读安全规则中的这些文档:

match /products/{product} {
  allow read: if resource.data.isAllow == true;
}

But keep in mind that security rules are not filters on their own, and instead merely ensure the client doesn't try to read data it isn't permitted to.但请记住, 安全规则本身并不是过滤器,而只是确保客户端不会尝试读取不允许的数据。 To meet the security rule above, you'll also need to use a query to read the allowed data:为了满足上述安全规则,您还需要使用查询来读取允许的数据:

firebase.firestore()
  .collection("products")
  .where("isAllow", "==", true)

Yes,是的,

match /collection/{document} {
  allow read: if resource.data.isAllow == true;
}

Here resource.data is a map of all of the fields and values stored in the document and the above rule will allow read operation only when isAllow field in the document being accessed is true.这里resource.data是存储在文档中的所有字段和值的 map,只有当被访问文档中的 isAllow 字段为真时,上述规则才允许读取操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase firstore 云函数的安全规则 - Firebase firstore security rules for cloud functions 添加安全规则,让用户只能在 Firebase 中读取包含其 uid 的数据 - Add security rules to let users read data that contains their uid only in Firebase Firebase - 如何设置安全规则,只有登录用户才能读写? - Firebase - How to set security rules so that only logged in users can read and write? 设置 Firebase Firestore 安全规则,以便只有用户可以 CRUD 自己的数据,其他所有内容都将被忽略 - Setting Firebase Firestore security rules so only users can CRUD their own data and all else is ignored firebase规则如何让用户只能读取他的数据 - firebase rules how to make user can read only his data Firebase 安全规则 (.read) - Firebase security rules (.read) firebase 安全规则只对一个特定字段进行写访问 - firebase security-rules write access to only one specific field Firebase 安全规则 Map 方法 get() 是否算作文档读取? - Does Firebase security rules Map method get() count as a document read? 如何使用 Firestore 安全规则让用户只能访问自己的资源 - How to use Firestore Security Rules to let users only access their own resources Firestore 安全规则和读取限制 - Firestore Security Rules and Read Limits
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM