要编写能访问路径上方文档的Firestore安全规则,您可以使用Firestore的安全规则语法来实现此功能。
以下是一个实现该功能的示例代码:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// 允许读取路径上方的文档
function isDocumentAbovePath(documentPath, path) {
// 获取路径上方文档的路径
let documentAbovePath = documentPath.substring(0, documentPath.lastIndexOf(path));
// 检查路径上方文档是否存在并可读
return exists(/databases/$(database)/documents/$(documentAbovePath)) &&
get(/databases/$(database)/documents/$(documentAbovePath)).data.readable == true;
}
// 为路径上方的文档设置访问规则
match /{collection}/{document} {
allow read: if isDocumentAbovePath(resource['__name__'], '/$(collection)/$(document)/');
allow write: if false; // 禁止写入操作
}
// 其他规则...
}
}
在上面的代码中,我们定义了一个名为isDocumentAbovePath
的函数,用于检查路径上方的文档是否存在并可读。然后,我们在match /{collection}/{document}
规则中使用该函数来设置路径上方文档的访问规则。
请注意,上述代码仅用于演示目的,您可能需要根据自己的需求进行修改和调整。
希望以上信息对您有所帮助!如果您有任何其他问题,请随时提问。
上一篇:编写内联函数的EBPF探针