【问题标题】:Is it possible to write rules using template string variables in Cloud Firestore?是否可以在 Cloud Firestore 中使用模板字符串变量编写规则?
【发布时间】:2019-09-19 13:31:54
【问题描述】:

我想写一个规则来做以下事情:

allow update: if request.auth.uid == resource.data.players.$(request.auth.uid).id;

目标是检查一个取决于 uid 是什么的动态字段。

$(something) 模板语法适用于 document paths,但不适用于实际的文档属性。

有没有办法使用 Firestore 规则来做到这一点?

【问题讨论】:

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


    【解决方案1】:

    当然可以,而且你非常接近。这是我必须只允许版主或管理员更新的规则:

    allow update: if request.auth.uid in request.resource.data.moderators || get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true
    

    你的关键是:

    $(request.auth.uid)
    

    【讨论】:

    • 这仅在您的参数位于文档路径中时才有效。就我而言,我想检查特定字段的存在,并且该字段应该是request.auth.uid 的值。这不是文档 ID,而是文档中的值
    • 这就是第二部分的作用。这将检查文档中的“admin”字段,其中到达那里的路径是动态的:get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin
    猜你喜欢
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 2017-08-14
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 2011-02-21
    • 2017-03-24
    相关资源
    最近更新 更多