仅允许从文档中的 uid 更新(firestore 安全规则)?

时间:2021-07-28 16:42:34

标签: firebase google-cloud-firestore firebase-security

假设我有一个“报价”文档,其中保存了原始创建者和他的 uid 也称为“uid”字段。

如何修改以下规则以仅允许 request.auth.uid 与文档字段中的那个匹配的用户更新文档?

这是我现在的规则

function signedInOrPublic() {
  return request.auth.uid != null;     
}     

match /offer/{offerDcoument} {       
  allow read: if request.auth.uid != null 
  allow create: if signedInOrPublic() 
  allow update: <ONLY ALLOW IF THE UID FIELD MATCHES request.auth.uid CONDITION>
}

1 个答案:

答案 0 :(得分:2)

您可以使用 resource 对象访问该字段:

match /offer/{offerDcoument} {       
  allow read: if request.auth.uid != null 
  allow create: if signedInOrPublic() 
  allow update: if request.auth != null && resource.data.uid == request.auth.uid;
                                                      // ^^^ fieldname
}