Firestore 安全规则中的动态路径
dynamic path in firestore security rules
我有以下设置
collection document collection document
'users' userId 'test_*' 'results'
一个用户有可能进行多次测试,这就是为什么子集合可以是test_1或test_12等。最新测试的数量存储在
/databases/$(database)/documents/user/$(userId)/settings/sharing).data.number_of_tests
该值可以是例如 3(如果用户进行了 3 次测试),这意味着 firestore 规则中的路径应如下所示:
/users/{userId}/test_3/results
是否可以仅更改路径描述的一部分,在我的例子中,根据我通过 get(...) 获得的数据库反馈,仅更改“test_”后面的数字?
目前我尝试了以下但它不起作用..
match /users/{userId}/test_{cycle}/results {
allow read: if cycle == string(get(/databases/$(database)/documents/user/$(userId)/settings/sharing).data.number_of_tests)
}
有谁知道这是否可行以及我如何才能让它发挥作用?
谢谢!!
我找到了解决方案。我调用一个函数从我的数据库中提取数据,将字符串放在一起,然后将 return 值与路径进行比较。
// FUNCTIONS AREA
function getNumber(userId) {
let number = string(get(/databases/$(database)/documents/user/$(userId)/settings/sharing).data.number_of_tests);
let name = "test_";
let returnValue = name+number;
return returnValue
}
//CALL THE FUNCTION
match /users/{userId}/{test}/results {
allow read: if test== getNumber(userId);
}
我有以下设置
collection document collection document
'users' userId 'test_*' 'results'
一个用户有可能进行多次测试,这就是为什么子集合可以是test_1或test_12等。最新测试的数量存储在
/databases/$(database)/documents/user/$(userId)/settings/sharing).data.number_of_tests
该值可以是例如 3(如果用户进行了 3 次测试),这意味着 firestore 规则中的路径应如下所示:
/users/{userId}/test_3/results
是否可以仅更改路径描述的一部分,在我的例子中,根据我通过 get(...) 获得的数据库反馈,仅更改“test_”后面的数字?
目前我尝试了以下但它不起作用..
match /users/{userId}/test_{cycle}/results {
allow read: if cycle == string(get(/databases/$(database)/documents/user/$(userId)/settings/sharing).data.number_of_tests)
}
有谁知道这是否可行以及我如何才能让它发挥作用?
谢谢!!
我找到了解决方案。我调用一个函数从我的数据库中提取数据,将字符串放在一起,然后将 return 值与路径进行比较。
// FUNCTIONS AREA
function getNumber(userId) {
let number = string(get(/databases/$(database)/documents/user/$(userId)/settings/sharing).data.number_of_tests);
let name = "test_";
let returnValue = name+number;
return returnValue
}
//CALL THE FUNCTION
match /users/{userId}/{test}/results {
allow read: if test== getNumber(userId);
}