Firebase - 如果存在特定用户 ID,则 Firestore 规则从嵌套集合中获取数据

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

我的数据结构如下:

stage/data/groups(coll)/

       group_name

       group_id

       participants(coll)/userid

我的要求是编写具有以下条件的 firebase 规则。

  1. 如果用户 A 调用组集合,那么如果 A 用户 ID 在参与者列表中可用,则它应该返回所有组。
  2. 如果特定用户在参与者列表中不可用,则该用户不应访问该组。

我尝试过以下规则,但没有帮助。

rules_version = '2';    
service cloud.firestore {
  match /databases/{database}/documents {
      
    function isAuthorised(document){
      return exists(/document/participants/$(request.auth.uid))
    }
      
    match /{document=**} {
      allow read, write: if isAuthorised(document);
    }
  }
}

如果是 ASP.NET API 代码,我只会迭代该列表并仅返回有效列表。由于我是 Firebase 的新手,我正在学习保护数据的复杂规则。

编辑: 更新了规则如下,如果我传递硬编码的组 id 而不是将“$(groupid)”传递给存在验证方法,它就会起作用。

match /{document=**}/groups/{groupid} {
  allow read, write: if 
  isAuthenticated() && 
  exists(/databases/$(database)/documents/stage/data/groups/$(groupid)/participants/$(request.auth.uid));
}
google-cloud-firestore firebase-security
1个回答
0
投票

从文档中的示例来看,

exists
似乎需要您要检查的文档的完整路径。您正在传递相对路径,这是行不通的。

function isAuthorised(document){
  return exists(/databases/$(database)/documents/participants/$(request.auth.uid))
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.