Google cloud firestore 如何根据用户组/角色限制对FireStore中文档及其所有子集合的访问?(每个文档中没有组名)
我找到了这个很好的答案Google cloud firestore 如何根据用户组/角色限制对FireStore中文档及其所有子集合的访问?(每个文档中没有组名),google-cloud-firestore,firebase-security,Google Cloud Firestore,Firebase Security,我找到了这个很好的答案 match/leagues/{league}/{document=**}{ 允许读、写:if request.auth.uid== 获取(/databases/$(database)/documents/leagues/$(league)).data.creator } 然而,我没有让它发挥作用。我的实现如下所示: rules_version='2'; 服务云.firestore{ 匹配/databases/{database}/documents{ 匹配/users/
match/leagues/{league}/{document=**}{
允许读、写:if request.auth.uid==
获取(/databases/$(database)/documents/leagues/$(league)).data.creator
}
然而,我没有让它发挥作用。我的实现如下所示:
rules_version='2';
服务云.firestore{
匹配/databases/{database}/documents{
匹配/users/{user}{
允许读、写:如果为false;
}
匹配/employees/{user}{
允许读、写:如果为false;
}
//匹配/公司/{document=**}{
//允许读、写:如果为false;
// }
函数isSignedIn(){
return request.auth!=null;
}
函数getEmployeeData(){
返回get(/databases/$(database)/documents/employees/$(request.auth.uid)).data
}
//检查当前用户是否有权访问特定公司
功能访问公司(公司ID){
返回isSignedIn()&&getEmployeeData()['companyId']==companyId;
}
}
匹配/users/{user}{
允许获取:如果为真;
允许列表,创建:如果为false;
允许更新、删除:if request.auth.uid==user;
}
匹配/employees/{user}{
允许获取:if request.auth.uid==user | | hasRole('admin');
允许列表:如果有角色('admin');
允许更新:如果hasRole('admin');
允许创建、删除:如果为false;
}
match/companys/{company}/{document=**}{/My,因为这很容易引起误解。函数需要位于调用match语句中。现在它可以工作了
match/companys/{company}/{document=**}{
函数getEmployeeData(){
返回get(/databases/$(database)/documents/employees/$(request.auth.uid)).data;
}
允许读、写:如果getEmployeeData()['companyId']==公司;
}
该函数需要是match语句中的“anywhere upchain”。我使用了一个非常深入的数据库结构,在许多层次上都有方便的函数。在您的情况下,您可以这样放置函数:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
function getEmployeeData() {
return get(/databases/$(database)/documents/employees/$(request.auth.uid)).data;
}
您的所有其他匹配语句都可以使用它。安全规则本身不起任何作用。它们仅在客户端进行查询时才会触发。请编辑问题以显示查询没有按照您预期的方式使用这些规则,并明确您认为它应该做什么。如果您想做更多详细信息,请led调试,应该使用本地模拟器:我正在使用操场查询(get)一个文档。当我提出这个问题时,没有办法让它工作。与此同时,我使用了上一个工作版本,并再次实现了这些功能。作为轻度调试,我现在插入常量,以确定方程的哪一侧失败。当我输入时,如果getEmployeeData()['companyId']='4U4KZKXKRha6B04S5K';
(文档id),我现在得到的答案是,运行模拟时出现未知错误。请编辑问题,详细解释您正在执行的操作没有按照预期的方式工作。问题中应该有足够的信息,任何人都可以用来复制该行为。明白了,函数需要与where在同一匹配范围内它被称为。听起来合乎逻辑,但在我用作蓝图的文章中看起来有所不同。现在我又回到了正轨,希望它能运行起来