gpt4 book ai didi

google-cloud-firestore - Firestore 规则 : What if a give document doesn't exist but is used in a get?

转载 作者:行者123 更新时间:2023-12-04 01:42:25 25 4
gpt4 key购买 nike

考虑这个代码:

match /shocking_contents_main_app/{shocking_content_main_app} {  // Do not specify any read/update/delete rules - OK, last check 2019/06/03
allow create: if request.auth.uid != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.can_report_shocking_content == true
&&
exists(/databases/$(database)/documents/users/$(request.resource.data.reported_account_id))
&&
request.resource.data.reported_login == get(/databases/$(database)/documents/users/$(request.resource.data.reported_account_id)).data.login;
}

这些规则是在报告一些令人震惊的内容的背景下设置的。

最后一行检查报告的登录名是否等于报告的用户 ID 的登录名。 “问题”如下:文档 $(request.resource.data.reported_account_id)路径下 /databases/$(database)/documents/users/不可能存在。如果它不存在,Firestore 安全规则是否会拒绝查询(预期行为)?换句话说:我必须使用 exists(...) ?

第一行还有另一个例子:我检查登录用户是否可以报告其他人的内容( get(/databases/$(database)/documents/users/$(request.auth.uid)).data.can_report_shocking_content == true )。但我不使用 exists(...) .

最佳答案

如果 get()调用失败,整个检查变为假。重要的是要意识到即使 allow 中只有一个子句条件失败,整个条件都将失败。

所以这条规则将返回 true :

allow read: if true || false;

但是下一条规则将失败:
allow read: if true || get(document_that_does_not_exist);

如果您可能希望在文档不存在时获得积极的结果,您确实需要使用 exists() 来检查它是否存在。第一的。

所以这将返回 true再次:
allow read: if true || exists(document_that_does_not_exist);

关于google-cloud-firestore - Firestore 规则 : What if a give document doesn't exist but is used in a get?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56877606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com