我正在努力理解一些 Firestore 安全概念。我想根据 request.path
属性设置规则。
我的规则是这样的:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.path[0]=='appUsers';
}
}
}
...然后我使用 AngularFire2添加这样的文档...
this.myAngularFirestore.doc('/appUsers/'+this.auth.auth.currentUser.uid).set({name: this.auth.auth.currentUser.displayName})
.then(()=>console.log('Added user'))
.catch(err=>console.log('Could not add user:', err.message));
我认为这应该很简单 based on the docs但我得到的只是错误 - 权限缺失或不足
。
我知道我已正确登录,并且我知道如果我使用 allow read,write: if true;
打开安全性,查询就会起作用,那么我在这里没有得到什么?
request.path[0]
不应该评估为字符串 appUsers
并允许写入数据吗?
任何想法都值得感激地接受,因为到目前为止,我觉得这些规则在一起并没有多大乐趣。
大家干杯
最佳答案
我建议使用内置路径匹配器版本:
service cloud.firestore {
match /databases/{database}/documents {
match /appUsers {
allow read, write;
}
}
}
或者,如果您确实想在条件中指定:
service cloud.firestore {
match /databases/{database}/documents {
match /{pathSegment} {
allow read, write: if pathSegment == "appUsers";
}
}
}
request.path
是完整路径(例如 /projects/<projectId>/databases/(default)/documents/appUsers
,这意味着您实际上想要 request.path[5]
(因此我们提供更简单、更易读的方法来执行此操作)。
编辑(2018 年 4 月 2 日):request.path
支持List
和Map
访问:
-
List
:request.path[5] == "appUsers"
-
Map
:request.path['pathSegment'] == "appUsers"
,但请注意,它仅适用于通配符(例如{name}
)值
关于angular - 尝试使用 request.path 设置 Cloud Firestore 安全规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47275346/