gpt4 book ai didi

database - 我可以在 Cloud Firestore 中创建一个仅追加的集合吗?

转载 作者:搜寻专家 更新时间:2023-10-30 22:28:43 25 4
gpt4 key购买 nike

我想将游戏事件和审核日志从我的应用写入 Cloud Firestore。一旦写入,我不希望用户能够修改或删除这些事件/日志。

我该怎么做?

最佳答案

Cloud Firestore 中的规则使得从移动和 Web 客户端将集合甚至整个数据库放入仅附加系统变得非常简单。

例子

下面是一组规则,可将根集合 audit_logs 转变为仅追加集合。

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();
}
}
}

让我们分解最重要的部分。

函数:permission_granted()

function permission_granted() {
return request.auth != null; // Change this to your logic.
}

这只是一个占位符,用于限制插入新文档或读取集合中的现有文档。在这种情况下,它允许任何使用 Firebase Auth 登录的人 -> 你可能希望它更具限制性。

它只返回 truefalse,稍后我们将使用它来实际执行。

匹配:根集合audit_log

匹配/audit_logs/{log} { ...

这个很简单,我们只是匹配关于根收集的任何请求,称为 audit_logs。由于 {log} 部分,问题中的文档 ID 可通过 $(log) 获得。

阻止任何仅附加的修改

allow update,delete: if false;

updatedelete 这两个不是 append-only 的写入方法,所以这里我们只是普遍禁止任何移动和网络 SDK 执行它们。

允许休息

allow read, create, list: if permission_granted();

最后,使用我们之前设置的 permission_granted 函数,我们允许读取、列出和创建集合中的新文档。

关于database - 我可以在 Cloud Firestore 中创建一个仅追加的集合吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47212286/

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