firebase - 将 contains() 与 firebase 规则一起使用
问题描述
我正在尝试使用以下文档在 Firestore 中设置自定义规则:https ://firebase.google.com/docs/reference/security/database/#containssubstring
我正在为此使用自定义声明。
什么有效:
match /calendar/{calendar} {
allow read: if request.auth.token.customData == "calendar-read,calendar-write";
}
什么不起作用,我不知道为什么:
match /calendar/{calendar} {
allow read: if request.auth.token.customData.contains("calendar-read");
}
这会给我:“FirebaseError:缺少或不足的权限”
解决方案
由于您使用的是 Firestore,因此您应该使用Firestore String的 API 文档,而不是 Realtime Database String。您将看到 Firestore 字符串没有称为“包含”的方法。
如果要检查字符串中是否包含子字符串,则应改用matches(),并提供正则表达式进行匹配。
allow read: if request.auth.token.customData.matches(".*calendar-read.*");
request.auth.token.customData
如果在其中的任何位置包含字符串“calendar-read”,则上述内容应返回 true 。
如果您使用逗号分隔的值列表,请考虑使用split()和list.hasAny()来更清楚。
推荐阅读
- appium-android - Mobile Recorder 自动启动和关闭应用程序
- c++ - upper_bound 如何与自定义对象类型 val 一起工作?
- sass - 如何将 Sass 变量导出到 Javascript?
- vue.js - 添加 , 到数字输入框 vue
- xampp - XAMPP 无法打开 PHP 文件
- mysql - 如何在 MySQL 5.7 中使用变量实现按功能分区
- rust - 启用项目功能时启用 Rust nightly 功能
- c# - 如何在特定时间触发 .NET Core 3.1 托管服务?
- matlab - 多次单击鼠标
- serverless-framework - ServerlessError:未找到无服务器插件“sls”