【问题标题】:Firebase database rules for a collection shared with two users与两个用户共享的集合的 Firebase 数据库规则
【发布时间】:2017-05-03 14:18:05
【问题描述】:

我有这个收藏

"connection-requests":{
    "push-key1":{
        "foo":"bar",
        "biz":"baz",
        "user1":{
            "some-info":"",
            "uid":"user1uid"
        },
        "user2":{
            "some-info":"",
            "uid":"user2uid"
        }
    }
}

我希望这个集合只能被 uid 存在于这个集合的两个用户读写。

这是我的数据库规则:

"connection-requests": {
    "$key": {
        ".read": "root.child('connection-requests').child($key).child('user1/uid').val() == auth.uid || root.child('connection-requests').child($key).child('user2/uid').val() == auth.uid",
        ".write": "root.child('connection-requests').child($key).child('user1/uid').val() == auth.uid || root.child('connection-requests').child($key).child('user2/uid').val() == auth.uid",
    }
}

我正在使用此请求访问数据:

db.ref('connection-requests')
    .orderByChild('user1/uid')
    .equalTo(uid) // <- auth.uid of user
    .once('value')
    .then()
    .catch()

上面的规则不起作用,我猜.child($any) 部分有问题,但我不确定是什么。

提前致谢。

【问题讨论】:

    标签: firebase firebase-realtime-database firebase-security


    【解决方案1】:

    每条规则的结束双引号放错了位置。它不是在行尾,而是在 OR 运算符之后:

    auth.uid || "root.child
                ^
                ^
    

    应该是:

    "connection-requests": {
        "$key": {
            ".read": "root.child('connection-requests') ... == auth.uid",
            ".write": "root.child('connection-requests') ... == auth.uid",
        }
    }
    

    【讨论】:

    • 您好 Bob,感谢您指出这个错误,但这只是提出问题时的拼写错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    相关资源
    最近更新 更多