Jar*_*ler 5 firebase-security google-cloud-firestore
考虑这段代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
这些规则是在报告一些令人震惊的内容的背景下设置的。
最后一行检查报告的登录名是否等于报告用户 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()
调用失败,则整个检查都会变为 false。重要的是要认识到,即使条件中只有一个子句allow
失败,整个条件也会失败。
所以这条规则将返回true
:
allow read: if true || false;
Run Code Online (Sandbox Code Playgroud)
但下一条规则将会失败:
allow read: if true || get(document_that_does_not_exist);
Run Code Online (Sandbox Code Playgroud)
如果您希望在文档不存在时得到积极的结果,那么您确实需要exists()
首先使用来检查它是否存在。
所以这会true
再次返回:
allow read: if true || exists(document_that_does_not_exist);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1442 次 |
最近记录: |