【问题标题】:firestore security for the update of specific field within document用于更新文档中特定字段的 Firestore 安全性
【发布时间】:2018-05-24 03:00:29
【问题描述】:

我有以下数据结构作为 Firestore 文档 我想为每个参与者设置安全规则。

{
 event_name: 'XXX',
 created_by: 'userid'
 participants: {
  userid_a: true,
  userid_b: true,
  userid_c: true,
 }
}

参与者被设置为类似数组的数据,以使用参与者的用户ID查询集合。 我正在按以下方式更新数据

const eventDoc = this.afs.doc('event/' + event_id );
participant_obj = {}
participant_obj[`participant_obj.${user_id}`] = true;
eventDoc.update(participant_obj)

然后,我想设置安全规则 只有授权用户才能更新到他们自己的参与者状态。

match /event/{eventId} {
 match /participant_obj {
  allow write: if request.resource.data.keys()[0] == request.auth.uid
 }
}

但它不起作用,因为participant_obj不是文档路径也不是集合路径,participant_obj只是文档中的数据。

如何设置安全性以更新文档中的特定字段?

【问题讨论】:

    标签: firebase-security google-cloud-firestore


    【解决方案1】:

    这样怎么样;

    match /event/{eventId} {
      allow write: if request.resource.data.participant_obj.keys()[0] == request.auth.uid 
                   && request.resource.data.participant_obj.size() == 1
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-10
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 2022-08-13
      相关资源
      最近更新 更多