Database 我可以仅在Cloud Firestore中创建集合附加吗?
我想将游戏事件和审核日志从我的应用程序写入云Firestore。一旦写入,我不希望用户能够修改或删除这些事件/日志Database 我可以仅在Cloud Firestore中创建集合附加吗?,database,firebase,firebase-security,google-cloud-firestore,database-security,Database,Firebase,Firebase Security,Google Cloud Firestore,Database Security,我想将游戏事件和审核日志从我的应用程序写入云Firestore。一旦写入,我不希望用户能够修改或删除这些事件/日志 如何做到这一点?云Firestore中的规则使得从移动和web客户端将一个集合,甚至整个数据库创建到一个仅附加的系统中变得非常简单 例子 下面是一组将根集合audit\u log转换为仅附加集合的规则 service cloud.firestore { match /databases/{database}/documents/ { function permis
如何做到这一点?云Firestore中的规则使得从移动和web客户端将一个集合,甚至整个数据库创建到一个仅附加的系统中变得非常简单 例子 下面是一组将根集合
audit\u log
转换为仅附加集合的规则
service cloud.firestore {
match /databases/{database}/documents/ {
function permission_granted() {
return request.auth != null; // Change this to your logic.
}
match /audit_logs/{log} {
allow update,delete: if false;
allow read, create, list: if permission_granted();
}
}
}
让我们把最重要的部分分解一下
功能:已授予的权限()
这只是一个占位符,用于限制插入新文档或读取集合中的现有文档。在这种情况下,它允许使用Firebase Auth->登录的任何人使用更严格的限制
它只返回true
或false
,稍后我们将使用它来实际执行
匹配:根集合审核日志
匹配/审核日志/{log}{
...
}
这一个很简单,我们只是匹配所有关于根集合的请求,名为audit\u logs
。由于{log}
一段,问题中的文档Id可通过$(log)
获得
阻止仅附加的任何修改
不只是附加的两种写入方法是update
和delete
,因此这里我们普遍禁止任何移动和web SDK执行它们
允许其余的
最后,使用前面设置的permission\u authorized
函数,我们允许在集合中读取、列出和创建新文档
service cloud.firestore {
match /databases/{database}/documents/ {
function permission_granted() {
return request.auth != null; // Change this to your logic.
}
match /audit_logs/{log} {
allow update,delete: if false;
allow read, create, list: if permission_granted();
}
}
}
allow update,delete: if false;
allow read, create, list: if permission_granted();