Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Firebase安全规则允许有条件。读取_Firebase_Firebase Realtime Database_Firebase Security - Fatal编程技术网

Firebase安全规则允许有条件。读取

Firebase安全规则允许有条件。读取,firebase,firebase-realtime-database,firebase-security,Firebase,Firebase Realtime Database,Firebase Security,我想创建一个firebase规则,允许用户在该子级的属性具有特定值时读写该子级。假设我的数据库如下所示: "tasks": { "SDkh7s62jnd23d9": { "uid": "someuserid", "other": "datagoes here" } } 以下是我当前的安全规则: "tasks": { ".indexOn": "_state", "$registerQueueKey": { ".rea

我想创建一个firebase规则,允许用户在该子级的属性具有特定值时读写该子级。假设我的数据库如下所示:

"tasks": {
    "SDkh7s62jnd23d9": {
        "uid": "someuserid",
        "other": "datagoes here"
    }
}
以下是我当前的安全规则:

"tasks": {
    ".indexOn": "_state",
    "$registerQueueKey": {
        ".read": "data.child('uid').val() == auth.uid",
        ".write": "newData.child('uid').val() == auth.uid"
    }
}
"tasks": {
    ".indexOn": "_state",
    ".read": "data.child($registerQueueKey").child('uid').val() == auth.uid",
    "$registerQueueKey": {
        ".write": "newData.child('uid').val() == auth.uid"
    }
}
此规则限制用户写入权限,但它从不允许用户读取,即使他们试图读取的子级具有等于
auth.id
uid
属性

然后,我测试了以下规则集:

"tasks": {
    ".indexOn": "_state",
    "$registerQueueKey": {
        ".read": true,
        ".write": "newData.child('uid').val() == auth.uid"
    }
}
"tasks": {
    ".indexOn": "_state",
    ".read": true,
    "$registerQueueKey": {
        ".write": "newData.child('uid').val() == auth.uid"
    }
}
尽管将
.read
权限设置为永久
true
值,用户仍然无法读取
$registerQueueKey
子项

然后,我测试了以下规则集:

"tasks": {
    ".indexOn": "_state",
    "$registerQueueKey": {
        ".read": true,
        ".write": "newData.child('uid').val() == auth.uid"
    }
}
"tasks": {
    ".indexOn": "_state",
    ".read": true,
    "$registerQueueKey": {
        ".write": "newData.child('uid').val() == auth.uid"
    }
}
现在我可以很好地读懂孩子了,所以我尝试了最后一条安全规则:

"tasks": {
    ".indexOn": "_state",
    "$registerQueueKey": {
        ".read": "data.child('uid').val() == auth.uid",
        ".write": "newData.child('uid').val() == auth.uid"
    }
}
"tasks": {
    ".indexOn": "_state",
    ".read": "data.child($registerQueueKey").child('uid').val() == auth.uid",
    "$registerQueueKey": {
        ".write": "newData.child('uid').val() == auth.uid"
    }
}
但此规则引发错误,因为变量
$registerQueueKey
在其使用的范围内未定义


如何实现这样的规则?

我认为错误是因为您没有放置通配符:

"tasks": {
  "$uid": {
    ".indexOn": "_state",
    ".read": "data.child($registerQueueKey").child('$uid').val() == auth.uid",
    "$registerQueueKey": {
        ".write": "newData.child('$uid').val() == auth.uid"
    }
  }
}

连接侦听器的位置上的Firebase检查读取授权。所以,要么你可以从那个位置读取数据(从而读取数据下的所有内容),要么你不能从那个位置读取数据。这意味着您不能使用安全规则来筛选您在此处尝试的内容。这在文档中是众所周知的。@FrankvanPuffelen那么我是否需要重新构造我的数据库,以便我试图检查的值是该子项的键?这将是一个确实可行的模型。