Mha*_*mad 1 firebase firebase-security google-cloud-firestore
我只想更新文档中的某些字段,所以我hasOnly在文档here中找到了函数,
但它不起作用,例如,下面的规则不起作用
function isValid(data){
return data.keys().hasOnly(['name','email','password'])
}
Run Code Online (Sandbox Code Playgroud)
当我在客户端 SDK 中更新时
firestore.document("users/user_doc")
.update(mapOf(
"name" to "Jack",
"email" to "jack@gmail.com",
"password" to "12345"
)).addOnCompleteListener {
if (it.isSuccessful){
Log.d("app", "success")
}else{
Log.d("app", "failed")
}
}
Run Code Online (Sandbox Code Playgroud)
但它显示缺少权限的错误
并且hasAll函数总是返回true!那么这个功能存在吗?为什么它不起作用?
谢谢。
小智 13
另一种选择是:
allow update: if request.resource.data.diff(resource.data).changedKeys().hasOnly(["name", "email", "password"]);
请记住,request.resource contains如果写入操作成功,将显示该文档。它也包含现有字段,而不仅仅是您正在更新的字段。
如果有更多字段,您将需要检查这些字段是否未被写入操作修改。因此,您将检查它们在request.resource和中是否具有相同的值resource。我经常在我的规则中使用这样的函数:
function isUnmodified(key) {
return request.resource.data[key] == resource.data[key]
}
Run Code Online (Sandbox Code Playgroud)
如果您的文档有一个额外的 field created,您将确保它没有被修改:
return data.keys().hasOnly(['name','email','password', 'created']) && isUnmodified('created')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
507 次 |
| 最近记录: |