简体   繁体   中英

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

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

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM