【问题标题】:Firestore Security Rules: check for value of list of mapFirestore 安全规则:检查地图列表的值
【发布时间】:2021-02-05 01:00:52
【问题描述】:

文档看起来像这样

matches: [
{uid: ___ , ...},
{uid: ___ , ...},
]

如何检查请求此操作的用户的 uid 是否在匹配的 uid 之一中?我试过了,但没有用。

get(/databases/$(database)/documents/users/$(request.auth.uid)).data.matches.uid中的uid

感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    你不能。安全规则语言不支持for 循环或map 之类的东西。您需要以不同的方式存储数据。这些模式中的任何一个都可以工作:

    // data structure
    matches: ["uid1", "uid2", "uid3"]
    // rules
    allow: if uid in resource.data.matches;
    
    // data structure
    matches: {"uid1": true, "uid2": true}
    // rules
    allow: if resource.data.matches[uid] == true;
    

    或者,如果您知道 matches 数组的长度不能超过(例如,五个元素),您可以手动扩展语句:

    allow: if resource.data.matches[0].uid == uid ||
              resource.data.matches[1].uid == uid ||
              // ...repeat three more times
    

    【讨论】:

    猜你喜欢
    • 2018-03-22
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 2018-10-19
    • 2019-07-05
    相关资源
    最近更新 更多