gpt4 book ai didi

node.js - Firestore : Different rules for creating and updating documents

转载 作者:搜寻专家 更新时间:2023-10-31 23:26:16 24 4
gpt4 key购买 nike

我有一个特定的需求,即允许我的用户创建匹配特定结构的 Firestore 文档,但不允许这些相同的用户在创建时更新该结构。

简而言之,我希望我的用户能够创建新文档但不能更新它们。

我定义了以下安全规则:

service cloud.firestore {
match /databases/{database}/documents {
function isAuthenticated() {
return request.auth.uid != null
}

match /conversations/{conversationId} {
function isParticipant() {
return resource.data.participants[request.auth.uid] == true
}

function NewConversation() {
// One of the participants must be the current user
return request.resource.data.participants[request.auth.uid] == true
}

allow read, delete: if isAuthenticated() && isParticipant()
allow create: if isAuthenticated() && NewConversation()
// allow update: if isAuthenticated() && NewConversation()
}
}
}

然而,事实证明,无论我如何尝试创建文档,我总是得到告密者 permission-denied错误。我很确定我要保存的文档包含 participants将当前用户的 Id 作为键映射到 true ,正如规则所期望的那样。

如果我也允许 update,我可以成功创建文档通过取消注释第三条规则来操作。

这是 Firestore 错误还是我误解了什么?

documentation (以及 createupdate 操作的明确分离)似乎表明这是可能的。

为了完整起见,这是我提出的未通过测试的客户端请求:

it('allows creating new conversations', async function() {
// clients.primary is a Firestore instance authenticated with
// the primary user used below as a key
const doc = await clients.primary.collection('/conversations').add({
participants: {
[users.primary.uid]: true,
[users.secondary.uid]: true,
},
})
})

最佳答案

尝试:

function isAuthenticated() {
return request.auth != null && request.auth.uid != null
}

如果您的请求不包含“auth”,firebase 将崩溃并可能返回权限违规。

关于node.js - Firestore : Different rules for creating and updating documents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48911815/

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