允许条件.read的Firebase安全规则

问题描述 投票:0回答:1

我想创建一个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"
    }
}

此规则限制用户写入权限,但它永远不会让用户读取,即使他们尝试读取的子项具有等于uidauth.id属性。

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

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

尽管.read许可被设置为永久true值,用户仍然无法读取$registerQueueKey孩子。

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

"tasks": {
    ".indexOn": "_state",
    ".read": true,
    "$registerQueueKey": {
        ".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在其使用的范围内未定义。

我如何完成这样的规则?

firebase firebase-realtime-database firebase-security
1个回答
0
投票

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

"tasks": {
  "$uid": {
    ".indexOn": "_state",
    ".read": "data.child($registerQueueKey").child('$uid').val() == auth.uid",
    "$registerQueueKey": {
        ".write": "newData.child('$uid').val() == auth.uid"
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.