使用对另一个文档的引用的Firestore安全规则
问题描述:
我想将安全规则基于对另一个对象的引用。我有一组用户和角色集合。用户对象具有一个称为“角色”的字段,该字段是对角色集合中特定文档的引用。使用对另一个文档的引用的Firestore安全规则
users
id
name
role <-- reference to particular role
roles
id
name
isSuperUser
这里的目标是使具有特定角色(与isSuperUser作用==真)编辑任何其他角色或它的子集的用户;
这里是我的规则,我还以为会工作:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId=**} {
allow read, write: if request.auth.uid == userId;
}
match /roles/{roleId=**} {
function isSuperUser() {
return get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role.isSuperuser == true;
}
allow read: if request.auth.uid != null;
allow write: if isSuperUser();
}
}
我已经证实了以下工作,但它不是真的有用...
get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role != null;
如果有一个更好的方式来完成角色基地的安全,我都听过。
缺乏任何调试工具使得这很令人沮丧。
答
您是否试图将通配符移至嵌套路径?
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, write: if request.auth.uid == userId;
match /{role=**} {
allow read: if request.auth.uid != null;
allow write: if isSuperUser();
}
}
}
}
基本上我正在寻找同样的东西。我有一个“鱼”系列,由用户拥有。我不用存储用户标识,而是存储对用户的引用。所以现在,我想检查fish.user.id是否与auth.uid相同。这是可能的,或者我应该只存储userId? –