允许更新 Firestore 中的单个字段 [英] Allow update on single field in firestore

查看:24
本文介绍了允许更新 Firestore 中的单个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想授予用户更新文档的权限.但仅当用户更新此文档的一个特定字段时.此用户不应更改所有其他字段.

I want to give a user the right to update a document. But ONLY if the user updates one specific field of this document. All other fields shouldn't be changed by this user.

这可以在 Firestore 中实现吗?

Is this possible in firestore?

我尝试过这样的事情:

function isUpdateToOpenField(attr) {
    return attr == get(/databases/$(database)/documents/stores/$(store)).data.open;
}

allow update: if isUpdateToOpenField(request.resource.data);

但我不知道如何比较更新是否对应于正确的字段.

But I don't know how to compare if the update corresponds to the right field.

推荐答案

Rules 语言中的 Map diffs 被引入来解决这个问题:

Map diffs in the Rules language were introduced to solve this:

function isUpdateToOpenField() {
    return request.resource.data.diff(resource.data).affectedKeys().hasOnly(['open']);
}

allow update: if isUpdateToOpenField();

这篇关于允许更新 Firestore 中的单个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆