Har*_*Kal 6 firebase google-cloud-firestore firebase-security-rules
match /UserProfile {
match /{uId}{
allow get: if isUserLoggedIn() && !isUserBlocked(uId);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用上述安全规则从UserProfile / {uId}获取数据时,在Firestore和代码中抛出以下错误,提示权限不足:
Error running simulation — Error: simulator.rules line [199], column [28]. Function not found error: Name: [get].
Run Code Online (Sandbox Code Playgroud)
现在上面两个函数的定义在这里
function isUserLoggedIn(){
return request.auth != null;
}
function isUserBlocked(uId){
return (('blocked' in get(/databases/$(database)/documents/UserSettings/$(uId)).data) && (request.auth.uid in get(/databases/$(database)/documents/UserSettings/$(uId)).data.blocked));
}
Run Code Online (Sandbox Code Playgroud)
第一个功能运行得很好,但是第二个功能却不能
它抛出该错误
据我所知,功能还不错
请帮助我在这个问题上浪费了很多时间
更新:错误是规则模拟器中的错误,请参阅下面的道格评论。
我尝试了您的规则,它们在模拟器中按预期工作。
规则:
match /UserProfile {
function isUserLoggedIn(){
return request.auth != null;
}
function isUserBlocked(uId){
return (('blocked' in get(/databases/$(database)/documents/UserSettings/$(uId)).data) && (request.auth.uid in get(/databases/$(database)/documents/UserSettings/$(uId)).data.blocked));
}
match /{uId}{
allow get: if isUserLoggedIn() && !isUserBlocked(uId);
}
}
Run Code Online (Sandbox Code Playgroud)
在模拟器中测试查询:
get /UserProfile/foo
Authenticated: Yes
Firebase UID: bar
Run Code Online (Sandbox Code Playgroud)
根据UserSettings/foo
数据库中的文档,请求成功或失败:
拒绝要求:
/UserSettings/foo
{
content: "my content"
blocked: { bar: true }
}
Run Code Online (Sandbox Code Playgroud)
允许请求:
/UserSettings/foo
{
content: "my content"
blocked: { otheruser: true }
}
Run Code Online (Sandbox Code Playgroud)
我认为,如果数据不存在或格式不正确,则会弹出错误。
例如,如果删除/UserSettings/foo
文档,我还会收到:
Error: simulator.rules line [58], column [28]. Function not found error: Name: [get].
Run Code Online (Sandbox Code Playgroud)
如果该blocked
字段不是地图(因为这in
是地图的函数),那么我也会收到此错误:
Error: simulator.rules line [58], column [95]. Function not found error: Name: [in].
Run Code Online (Sandbox Code Playgroud)
您可能可以通过使用exists
并检查的类型来清除这些错误,blocked
但是无论哪种方式,您的规则仍应按预期拒绝请求。
归档时间: |
|
查看次数: |
716 次 |
最近记录: |