我对以下用于保护我的 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函数。
您对 Cloud Storage 使用 Firestore 安全规则语法。
您可以在 Cloud Storage 安全规则中访问 Firestore 文档,但语法不同,如 doc:
中所示function isAdmin(userId) {
return firestore.get(/databases/(default)/documents/users/$(userId)).role == "admin";
}