我特别需要允许我的用户创建与特定结构匹配的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(以及create
和update
操作的明确分离)似乎表明这是可能的。
为了完整性,这是我正在进行的客户端请求,但未通过测试:
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,
},
})
})
答案 0 :(得分:0)
尝试:
function isAuthenticated() {
return request.auth != null && request.auth.uid != null
}
如果您的请求不包含“ auth”,firebase将会崩溃并可能返回权限冲突。