对`list`要求公司的FireStore安全规则

问题描述 投票:4回答:2

在公司的FireStore安全规则,是有可能使用list查询时,检查某些文档字段?

采用了棱角分明,我想用它userprofiles属性如下检索来自username收集单个文档:

let userprofile = this.afs.collection( 'userprofiles', ref => 
ref.where('username', '==', username ).limit(1);

我想这个查询由公司的FireStore安全规则是允许的,如果任:

  • 该USERPROFILE发布,或
  • 所述用户配置文件是未公开的,但相应的用户登录

这里是我公司的FireStore安全规则:

match /userprofiles/{userprofileId} {

    allow list:  if( resource.data.published==true || (    
                     resource.data.published==false &&
                     resource.data.uid==request.auth.uid )
                 );
    }
}

对于情况下,我使用的是完全相同的规则,以允许get要求,工作正常。然而,在上面的例子中查询导致一个list请求,而不是一个get。而在这种情况下,这些规则不允许查询。我越来越Error: Missing or insufficient permissions.

我记得读沿着对于列表查询线的东西,规则必须允许全部或任何文件,这在我的情况不适用。所以,我有点明白为什么它不工作。

我的问题是,我可以改变的东西,使之成为我的查询工作?或者,这是不可能的?任何想法解决方法? (除了明显的“由文档ID查询”或“使用户名的文档ID”)

angular firebase google-cloud-firestore firebase-security-rules
2个回答
1
投票

为了让“名单”进行查询,查询参数必须匹配的安全规则。请参考the documentation.

您的查询需要包括发布的==真实的陈述:

let userprofile = this.afs.collection( 'userprofiles', ref => 
ref.where('published', '==', true).where('username', '==', username ).limit(1);

而你的规则可以简化为:

match /userprofiles/{userprofileId} {

    allow list: if resource.data.published==true || 
                   resource.data.uid==request.auth.uid;
}

注意查询将只列出发表的UserProfiles。我不认为这将有可能查询“发表== true”或一次匹配的UID状态,您的代码将需要查询的两倍。


-3
投票

我迟到了,但这样的说法在我的情况下工作

match /userprofiles/{userprofileId} {
    allow read: if resource.data.published==true;
    }
}
match /userprofiles/{userprofileId} {
    allow read: if resource.data.uid==request.auth.uid;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.