首页 > 解决方案 > Firestore 安全规则:检查地图列表的值

问题描述

一个文件看起来像这样

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

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

输入get(/databases/$(database)/documents/users/$(request.auth.uid)).data.matches.uid

谢谢你的帮助!

标签: firebasegoogle-cloud-firestorefirebase-authenticationfirebase-security

解决方案


你不能。安全规则语言不支持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

推荐阅读