Firestore规则:仅在document字段为false时显示文档

时间:2019-12-24 19:16:03

标签: google-cloud-firestore firebase-security

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

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

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; 
  }
}

我尝试在request.之前添加resource,但仍然无法正常工作。

更新:

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}

2 个答案:

答案 0 :(得分:0)

您的匹配项未正确嵌套。它们应位于以<div class="menu-grid-container"> <div class="menu-block item-1"> <h2 class="menu-title">Promos</h2> </div> <div class="menu-block item-2"> <img src="https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg" alt="" class="menu-img"> <span class="menu-item">6 Alitas</span> <span class="price">9.99€</span> </div> <div class="menu-block item-3"> <img src="https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg" alt="" class="menu-img"> <span class="menu-item">Menu Familiar</span> <span class="price">9.99€</span> </div> <div class="menu-block item-4"> <img src="https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg" alt="" class="menu-img"> <span class="menu-item">Menu 2</span> <span class="price">9.99€</span> </div> <div class="menu-block item-5"> <h2 class="menu-title">Menus</h2> </div> <div class="menu-block item-6"> <img src="https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg" alt="" class="menu-img"> <span class="menu-item">Menu 1</span> <span class="price">9.99€</span> </div> <div class="menu-block item-7"> <img src="https://hatrabbits.com/wp-content/uploads/2017/01/random.jpg" alt="" class="menu-img"> <span class="menu-item">Menu 2</span> <span class="price">9.99€</span> </div> </div>开头的块中。另外,您应该强烈考虑删除match /databases/{database}/documents上的匹配项,因为这将使每个人都可以在登录后读取数据库中的任何文档,而忽略所有其他规则。

答案 1 :(得分:0)

如果您使用的是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);