即使将Firebase安全规则设置为true,也无法从节点读取数据

时间:2018-11-26 11:03:10

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

我有一个带有主题节点的firebase数据库,其中包含各种主题。当我尝试在此节点上应用读取规则时,我能够在Firebase安全规则模拟器中读取值,但无法在我的实际应用中读取该值。但是,如果我将规则应用于父节点本身,则可以获取该值。

这是我的数据库: enter image description here

以下是我的Firebase安全规则:

  {
  "rules": {
    "users":{
      "$user_id":{
        ".read": "auth != null && !root.child('blocked/'+$user_id+'/'+auth.uid).exists()",
        ".write": "$user_id === auth.uid"
      }
    },
    "topics":{
      //".read": "true",
      "$topic_id":{
        //restricting blocked users from reading the topcis
        ".read": "true",
        ".write": "auth != null"
      },
    }
  }
}

现在,如果我取消注释我可以读取的父节点上的“ .read”规则,但当我将其与通配符一起应用时则不能。我也可以写完主题节点。

这是我尝试从Firebase数据库访问数据的功能。

public void loadTopics() {
        //Load topics for the first time
        createTopicFab.show();
        Query topicQuery = FirebaseDatabase.getInstance().getReference()
                .child(FirebaseValues.TOPICS_TABLE).limitToLast(TOTAL_ITEM_EACH_LOAD).orderByChild("createdTimestamp");
        itemPos = 0;
        topics.clear();
        topicIds.clear();
        topicQuery.addChildEventListener(new ChildEventListener() {


            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

                // Add the values in the topics list and notify the adapter
                loadingLayout.setVisibility(View.GONE);
                Topics topic = dataSnapshot.getValue(Topics.class);
                Log.d(TAG, "loadTopics(), Topic: " + topic.getText());
                topics.add(0, topic);
                if (itemPos++ == 0) {
                    //first key
                    lastKey = String.valueOf(dataSnapshot.getKey());
                }
                topicIds.add(0, dataSnapshot.getKey());
                topicsListAdapter.notifyDataSetChanged();
                swipeRefTopic.setRefreshing(false);
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
                // remove the values in the topics list and notify the adapter
            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

1 个答案:

答案 0 :(得分:2)

这是您的基本查询:

Query topicQuery = FirebaseDatabase.getInstance().getReference()
    .child(FirebaseValues.TOPICS_TABLE)

我认为FirebaseValues.TOPIC_TABLE是“主题”。该查询无法读取/ topics处的子级,因为您尚未在该位置授予读取权限。如您所见,当您授予对/ topics的访问权限时,它将起作用。查询的位置很重要-在这种情况下,孩子的权限并不重要。安全规则不会为您“过滤”该位置下的子级。阅读有关“ security rules are not filters”的文档。