简体   繁体   English

在 firestore 安全规则中使用集合的父文档

[英]Use parent document of the collection in firestore Security Rules

This is my firestore collection path /chat/{doc}/messages这是我的 firestore 收集路径/chat/{doc}/messages

I written rules like below,我写了如下规则,

match /chat/{documents=**}{
  allow create: if request.auth.uid != null;
  allow read, update, delete: if request.auth.uid in resource.data.users;
}
 
match /chat/{doc}/messages/{documents=**}{
  allow read, create:if request.auth.uid != null;
  allow update, delete: if false;
}

The problem is messages collection under the doc .问题是doc下的messages收集。 I want to write rule like below for messages我想为消息编写如下规则

match /chat/{doc}/messages/{documents=**}{
  allow read, create:if request.auth.uid in doc.data.users;
  allow update, delete: if false;
}

I am not well experienced in firestore rules.我对 Firestore 规则经验不足。 Can someone help me to resolve the issues?有人可以帮我解决问题吗? Thanks in advance!提前致谢!

You can write firestore security rules that reference parent documents by querying for the parent document in the security rule with get :您可以通过使用get在安全规则中查询父文档来编写引用父文档的 Firestore 安全规则:

match /chat/{chatId}/messages/{messageId}{
  allow read, create: if request.auth.uid in get(/databases/$(database)/documents/chat/$(chatId)).data.users
  allow update, delete: if false;
}

This answer is based on Access other documents in the Firebase documentation.本答案基于Firebase文档中的Access other documents。

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

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