Firebase安全规则如何克服级联

时间:2018-02-12 22:23:53

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

我正在使用Firebase后端编写一个民意调查应用。用户回答是/否问题。我只希望那些回答问题的人能够看到结果,并且只有用户的朋友能够看到用户已经回答了问题。我应该如何构建它并编写安全规则?以下是我的尝试

questions:
    question1:
        readable:
            text: "Did you have a good day?"
            yes_count: 37
            no_count: 24
        answers:
            user1: yes
            user2: no
            ...
            user100: yes

users:
    user1:
        friends:
            user3
            user4

我提出了以下安全规则。它隐藏答案但不隐藏身份。通过级联,我不知道如何限制只有那些已回答问题的人

questions:
    $qid:
        answers:
           $uid:
               ".read": root.child("users").child(auth.uid).child("friends").child(uid).exists()

1 个答案:

答案 0 :(得分:0)

read路径的当前questions/$qid/answers规则可用于确定任何特定用户的朋友是否已回答问题,但需要额外的规则来回答第一部分({{1} })。为此,您需要I want only those who answered a question to be able to see the resultsyes_count路径的规则,如下所示:

no_count

请注意questions: $qid: answers: $uid: ".read": root.child("users").child(auth.uid).child("friends").child(uid).exists() readable: text: ".read": true ".write": false $child: ".read": "data.parent().parent().child('answers').child(auth.uid).exists()" ".write": false 读取规则中.parent()的双重使用。这样它可以上升两个路径(到questions/$qid/readable/$child),然后下到$qid子进程,以确定登录用户的id是否存在。 answers此处只有$childyes_count,除非您在将来某个时候更改数据结构。