【问题标题】:Firestore Rules: Only show documents if document field is falseFirestore 规则:仅当文档字段为 false 时才显示文档
【发布时间】:2019-12-24 19:16:03
【问题描述】:

我有一个 NSFW 系统,它会检查文档是否为 NSFW,如果是,则将文档字段 isNSFW 更新为 true。这很好用,但现在我不想通过设置规则向所有用户显示这些文档,而不是查询出来。

这是我拥有的,但它不起作用...... javascript

service cloud.firestore {
    match /databases/{database}/documents {
        match /{document=**} {
            allow read, write: if request.auth.uid != null;
        }
    }
    match /users/{user} {
        allow read: if true;
    }
    match /docs/{doc} { // THIS HERE
        allow read: if resource.data.isNSFW == false; 
    }
}

我尝试在resource 之前添加request.,但还是不行。

更新: javascript

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // match /{document=**} {
    //  allow read, write: if request.auth.uid != null;
    // }
    match /users/{user} {
        allow read: if currentUser().uid != null;
    }
    match /docs {
      allow read: if existingData().users[currentUser().uid] == false; 
    }
    match /docs/{doc} {
      allow write: if currentUser().uid != null;
    }
  }
  // MARK - Funcs ---------------
  function existingData() {
    return resource.data
  }

  function incomingData() {
    return request.resource.data
  }

  function currentUser() {
    return request.auth
  }

  function isSignedIn() {
    return request.auth != null;
    }
}

得到错误:

Listen for Query(docs where users.`40S88coPQObEWSeiYMZIJlIKJkI2` == false order by __name__) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}

【问题讨论】:

  • 如果没有看到您的代码,就不可能确定它为什么不起作用。但请记住:规则本身并不是过滤器。您的代码还需要显式查询 isNSFW 为 false 的文档。

标签: google-cloud-firestore firebase-security


【解决方案1】:

如果您使用的是 Firebase 数据存储,您可以简单地查询 1 您的数据:

var myDB = db.collection("YOUR-DB-COLLECTION");
var query = myDB.where("isNSFW", "==", false);

如果您使用的是 GCP 数据存储,则可以采用其他方式 2:

const query = datastore
  .createQuery('YOUR-COLLECTION')
  .filter('isNSFW', '=', false);

【讨论】:

    【解决方案2】:

    您的匹配项嵌套不正确。它们应该在以match /databases/{database}/documents 开头的块内。此外,您应该强烈考虑删除 /{document=**} 上的匹配项,因为这将使每个人在登录后都可以阅读数据库中的任何文档,而忽略所有其他规则。

    【讨论】:

    • 侦听查询(docs where users.40S88coPQObEWSeiYMZIJlIKJkI2 == false order by name)失败:状态{code=PERMISSION_DENIED,description=Missing 或权限不足。,原因=null} 是我现在得到的
    猜你喜欢
    • 2018-09-29
    • 2018-03-29
    • 2018-04-03
    • 2019-10-16
    • 2019-03-30
    • 2018-07-04
    • 2023-02-13
    • 2020-02-07
    • 1970-01-01
    相关资源
    最近更新 更多