故障排除简单的自定义壁垒安全规则功能

发布于 2025-01-27 08:13:00 字数 1842 浏览 2 评论 0 原文

我正在编写自定义云功能(官方文档)根据官方文档

我将自定义函数应用于正确的路径/collection/{document}/subcollection/{subcollectionDocuments} 其中每个 subcollectionDocument 都有 userId 不是零的字段。这些文档没有 uid 字段,但是我在下面的2和4中尝试过。

下面的自定义函数的所有版本(AlterStoreQuestor 1,2,3和4)在云Firestore规则Playground Simulator中生成“在对象上未定义的属性资源”。

我需要将某些内容传递到自定义功能中,还是我犯了其他错误?

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
  
  function belongsToRequestor1() {
      return
      request.auth.uid == resource.data.userId;
    }


  function belongsToRequestor2() {
      return
      request.auth.uid == resource.data.uid;
    }

  function belongsToRequestor3() {
      return
      request.auth.uid == request.resource.data.userId;
    }

  function belongsToRequestor4() {
      return
      request.auth.uid == request.resource.data.uid;
    }
   
   …
   match /collection/{documents}/subcollection/{subcollectiondocuments} {
    
   allow update: if
      belongsToRequestor1(); 
      // or belongsToRequestor2(); or belongsToRequestor3(); or belongsToRequestor4();

   …

   } }

我不确定如何实现此答案对另一个问题的问题”您想测试使用其字段值的规则。” subcollectionDocuments 中的每个文档都有一个由Firebase自动生成的ID。

更新:按要求添加数据库屏幕截图(带有虚假数据)(properties = collection and评论== subcollection):

感谢您的任何帮助!

I'm writing a custom cloud function (official documentation) in Firestore security rules that checks to make sure the submitting user is the content owner, per the official documentation.

I'm applying the custom function to the correct path /collection/{documents}/subcollection/{subcollectiondocuments} where every subcollectiondocument has a userId field that's not null. These documents do not have a uid field, but I tried it anyway in 2 and 4 below.

All versions of the custom function below (belongsToRequestor 1,2,3 and 4) generate a "Property resource is undefined on object" error in the Cloud Firestore rules playground simulator.

Do I need to pass something into the custom function, or am I making some other mistake?

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
  
  function belongsToRequestor1() {
      return
      request.auth.uid == resource.data.userId;
    }


  function belongsToRequestor2() {
      return
      request.auth.uid == resource.data.uid;
    }

  function belongsToRequestor3() {
      return
      request.auth.uid == request.resource.data.userId;
    }

  function belongsToRequestor4() {
      return
      request.auth.uid == request.resource.data.uid;
    }
   
   …
   match /collection/{documents}/subcollection/{subcollectiondocuments} {
    
   allow update: if
      belongsToRequestor1(); 
      // or belongsToRequestor2(); or belongsToRequestor3(); or belongsToRequestor4();

   …

   } }

I'm not sure how to implement this answer to another question to "…enter the path to an actual document that exists if you want to test your rule that uses its field values." Each document in subcollectiondocuments has an id auto-generated by firebase.

Update: adding database screenshot (with fake data), as requested (properties = collection and reviews == subcollection):

screenshot with fake data

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文