Javascript Firestore规则-动态路径
我试图在Firestore的规则中创建动态连接 我有一个用户集合和一个角色集合,用户集合中的每个用户都有一个“字符串”字段,该字段与角色集合中相应角色的firebase生成ID相匹配 为了实现这一点,我尝试动态获取ID,然后从角色中检索适当的数据 我的代码如下:Javascript Firestore规则-动态路径,javascript,firebase,firebase-authentication,google-cloud-firestore,Javascript,Firebase,Firebase Authentication,Google Cloud Firestore,我试图在Firestore的规则中创建动态连接 我有一个用户集合和一个角色集合,用户集合中的每个用户都有一个“字符串”字段,该字段与角色集合中相应角色的firebase生成ID相匹配 为了实现这一点,我尝试动态获取ID,然后从角色中检索适当的数据 我的代码如下: service cloud.firestore { match /databases/{database}/documents { match /{collections}/{collection=**} { f
service cloud.firestore {
match /databases/{database}/documents {
match /{collections}/{collection=**} {
function getRole() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role;
}
function getPermissions(role) {
return get(/databases/$(database)/documents/roles/$(role)).data.test;
}
allow read: if true;
// Role equals to 12345
// allow write: if getRole() == 12345; <-- THIS WORKS
// allow write: if getPermissions(12345) == true; <-- THIS WORKS
// Need to put dynamic getRole() which equals to 12345 inside of getPermissions()
// instead of the static 12345
// THIS DOES NOT WORK
allow write: if getPermissions(getRole()) == true;
}
}}
service cloud.firestore{
匹配/databases/{database}/documents{
匹配/{collections}/{collection=**}{
函数getRole(){
返回get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role;
}
函数getPermissions(角色){
返回get(/databases/$(database)/documents/roles/$(role)).data.test;
}
允许读取:如果为真;
//角色等于12345
//允许写入:如果getRole()==12345;对您的示例进行了测试,它对我有效
service cloud.firestore {
match /databases/{database}/documents {
function getRole() {
return get(/databases/$(database)/documents/test-user/$(request.auth.uid)).data.role;
}
function getPermissions(role) {
return get(/databases/$(database)/documents/test-role/$(role)).data.rights;
}
match /test-user/{userid} {
allow read: if true;
allow write: if getPermissions(getRole()) == 'all';
}
}
}
我的测试集的内容是
/test-user/0fludECgWKg88Gn5SjNYPc3Opmx2 (id of my test user) has:
{"role": "tr"}
/test-role/tr has:
{"rights": "all"}
如果/test role/tr
不存在或权限
没有值all
,则写入请求将被拒绝
在您的例子中,可能函数getPermissions
的类型就是string,测试需要一个布尔值