指向存储安全规则中的Firestore文档以读取用户角色

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

我对以下用于保护我的 Firestore 的代码有疑问。 我想允许我的管理员访问所有用户的每个文件。为此,我编写了此函数,但 firestore 无法识别某些内容。 “无效的变量名称:数据库。” 获取也无法识别... 您能帮我更正这段代码并解释一下系统给我的错误是什么吗?

rules_version = '2';

service firebase.storage {
  match /b/{bucket}/o {
  
    function isAdmin(userId) {
      return get(/databases/$(database)/documents/users/$(userId)).data.role == "admin";
    }
    
    match /{allPaths=**} {
      allow read, write: if isAdmin(request.auth.uid);
    }
   
    match /invoices/{userId}/{soldeUid}/{fileName} {
      allow write: if request.auth != null
                   && request.auth.uid == userId
                   && soldeUid.matches("^[a-zA-Z0-9_-]*$")
                   && fileName.matches("^[a-zA-Z0-9_.-]*$");
      allow read: if request.auth != null
                  && request.auth.uid == userId;
    }
  }
}

many thank to all ! 

尝试将函数定位到开头/结尾。 修改get函数。

firebase google-cloud-firestore google-cloud-storage firebase-security
1个回答
0
投票

您对 Cloud Storage 使用 Firestore 安全规则语法。

您可以在 Cloud Storage 安全规则中访问 Firestore 文档,但语法不同,如 doc:

中所示
function isAdmin(userId) {
  return firestore.get(/databases/(default)/documents/users/$(userId)).role == "admin";
}
© www.soinside.com 2019 - 2024. All rights reserved.