如何在 Firebase 安全规则中使用通配符

问题描述 投票:0回答:1

我试图在我的 Firebase 安全规则中使用通配符,但它不像在线文档描述的那样工作。

我想返回整个 itineraryList 集合,但安全规则不起作用。

match /itinerary/{userId=**}/itineraryList/{doc} {
  allow read: if request.auth.uid == userId; 
  allow write: if request.auth.uid == userId;
}

此处允许经过身份验证的用户访问整个列表的正确语法是什么?

firebase google-cloud-platform google-cloud-firestore firebase-security
1个回答
2
投票

更新您的评论:

如果您想向任何经过身份验证的用户授予对

itinerary
集合(包括子集合)下所有文档的读取权限,请执行以下操作:

service cloud.firestore {
  match /databases/{database}/documents {
      match /itinerary/{docId=**} {
          allow read: if request.auth.uid != null;
      }   

      //possibly add another rule for write

  }
}

初步回答:

这是因为通过执行

{userId=**}
,您正在使用“recursive通配符语法”,请参阅https://firebase.google.com/docs/firestore/security/rules-struct#recursive_wildcards。它将对应于“整个匹配路径段”。

你应该这样做:

service cloud.firestore {
  match /databases/{database}/documents {
      match /itinerary/{userId}/itineraryList/{doc} {
        allow read: if request.auth.uid == userId; 
        allow write: if request.auth.uid == userId;
      }  
  }
}

您还可以观看有关 Firestore 安全规则的 Firebase 官方视频,其中解释了这一点:https://www.youtube.com/watch?v=eW5MdE3ZcAw

© www.soinside.com 2019 - 2024. All rights reserved.