Firebase firestore 安全规则允许文档 ID 包含字符串

use*_*039 2 firebase firebase-security google-cloud-firestore

我有以下内容:

\n
match /direct/{postId} {\n   allow read, write: if postId.includes(request.auth.uid);\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

但是,我只想在文档 ID (postId) 包含字符串的情况下允许读取、写入。.includes 在安全规则中对我不起作用。

\n

编辑:它应该匹配一个子字符串而不是整个文档 ID

\n

编辑2

\n

数据库的屏幕截图

\n

称为 direct 的集合,其中每个文档 ID 都是一个字符串,其中包含 2 个 uid\xe2\x80\x99 名称。

\n

任何帮助,将不胜感激。谢谢。

\n

Fra*_*len 8

文档ID是一个字符串,据我所知,安全规则中的String类没有include方法。

不过它确实有一个matches方法,因此您可以使用它来测试文档 ID 是否包含带有正则表达式的子字符串。

就像是:

match /direct/{postId} {
   allow read, write: if postId.matches(request.auth.uid);
}
Run Code Online (Sandbox Code Playgroud)

postId与用户 UID 匹配的工作读取:

在此输入图像描述

尝试阅读另一个文档:

在此输入图像描述

更新:测试子字符串:

  allow read: if postId.matches(".*"+request.auth.uid+".*");
Run Code Online (Sandbox Code Playgroud)