数据库有时会拒绝进入?

时间:2016-12-20 17:54:49

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

  

错误:ANDROID

W/SyncTree: Listen at /Leaders/JdGreJBImwYBPGrWHAeyEWBoWE73/Sample Group/REQUESTS failed: DatabaseError: Permission denied

我有一个类(Fragment)访问它:

public class AdminRequestsFragment extends Fragment {

    public FirebaseAuth.AuthStateListener AuthListener;
    DatabaseReference leadRef = FirebaseDatabase.getInstance().getReference().child("Leaders");
    DatabaseReference groupRef;
    private static  String nameOfGroupSelected;
  HashMap<String, String> userMap = new HashMap<>();
    private static final String ARG_PARAM1 = "param1";
    TextView noReqField;
    private static final String ARG_PARAM2 = "param2";
    ArrayAdapter adapter;
    ArrayList<String> namesofRequests = new ArrayList<>();
    String uid;

    private OnFragmentInteractionListener mListener;

    ListView lview;
    public AdminRequestsFragment() {
        // Required empty public constructor
    }

    public static AdminRequestsFragment newInstance(String param1, String param2) {
        AdminRequestsFragment fragment = new AdminRequestsFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        Bundle bundle = this.getArguments();
        if (bundle != null) {
            nameOfGroupSelected = (String) bundle.getSerializable("DBREFGROUP");

        }

        AuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                final FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    uid = user.getUid().toString();
                    groupRef = leadRef.child(uid).child(nameOfGroupSelected).child("REQUESTS");

                    groupRef.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            for(DataSnapshot d: dataSnapshot.getChildren()) {
                                if (d.getKey().equals("FakeReq")) {
                                    //wont add to list

                                } else {
                                    namesofRequests.add(d.getValue().toString()); //Adds disp names not UIDS
                                    userMap.put(d.getValue().toString(), d.getKey().toString());
                                }
                            }
                            onPost();
                        }
                        public void onPost(){
                            if(namesofRequests.size() == 0){
                                noReqField.setVisibility(View.VISIBLE);

                            } else {

                                adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, namesofRequests);
                                lview.setAdapter(adapter);

                                lview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                        // Toast.makeText(getApplicationContext(), "# " + arrayList.get(position), Toast.LENGTH_SHORT).show();
                                        String key = namesofRequests.get(position); //name of the person who was selected.
                                        //Log.i("", itemClicked);
                                        String uidOfPicked = userMap.get(key);
                                        System.out.println("Request selected was " + uidOfPicked + "  " + key);
                                        Fragment goToAdminPanel = new AdminDecideRequestOutcomeFragment();
                                        Bundle bundle = new Bundle(); //Create a bundle.
                                        bundle.putSerializable("DBREFGROUP", nameOfGroupSelected); //send DB Ref String to Fragment
                                        bundle.putSerializable("DBUIDOFREQUESTER", uidOfPicked);
                                        bundle.putSerializable("DBNAMEOFREQUESTER", key);
                                        goToAdminPanel.setArguments(bundle);
                                        FragmentManager fragmentManager = getFragmentManager();
                                        fragmentManager.beginTransaction().replace(R.id.flContent, goToAdminPanel).addToBackStack(null)//flcontent is the FrameLayout in
                                                // Main Activity class (the main holder of the frags)
                                                .commit();  //Replace old frag (findpplfrag) with the (someonegotclickedonfrag) (frag->frag)]


                                    }
                                });

                            }

                        }
                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });


                } else {
                    Log.i("Problem:", "User wasn't logged in.");
                }
            }
        };
        FirebaseAuth.getInstance().addAuthStateListener(AuthListener);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_admin_requests, container, false);
        lview = (ListView) v.findViewById(R.id.lview);
        noReqField = (TextView) v.findViewById(R.id.EmailField);
        noReqField.setVisibility(View.INVISIBLE);
        return v;
    }


    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    public interface OnFragmentInteractionListener {

        void onFragmentInteraction(Uri uri);
    }
}

规则:

{
  "rules": {
    "Names":{
     ".read": "auth != null" ,
      ".write": "auth != null"
    },
    "Leaders":{
      "$leaderuid": {
         ".write": "$leaderuid == auth.uid",
         ".read": "$leaderuid == auth.uid",

           "INVOLVEDIN":{
             ".read": "auth != null",
               ".write": "auth != null"

           },

           "OWNEDGROUPS":{
             ".read": "auth != null",
               ".write": "auth != null"

           },


           "USERPROFILE":{
             ".read": "auth != null"

           },

           "$groupname":{
             "GROUPNAME":{
               ".read": "auth != null"

             },
             "REQUESTS":{
               ".read": "$leaderuid == auth.uid",
              ".write": "auth != null"
             },
             "DATA": {
           ".write": "data.parent().child('ALLOWED').child(auth.uid).exists()",
           ".read": "data.parent().child('ALLOWED').child(auth.uid).exists()"
         },
             "BLACKLIST":{
               ".read": "auth != null",
                 ".write": "$leaderuid == auth.uid"

             }

           }
       }
    }
  }
}

数据库正在构建

{
  "Leaders" : {
    "JdGreJBImwYBPGrWHAeyEWBoWE73" : {
      "INVOLVEDIN" : {
        "Sample Group" : "Sample Group"
      },
      "OWNEDGROUPS" : {
        "Sample Group" : "Sample Group"
      },
      "Sample Group" : {
        "ALLOWED" : {
          "Child1" : "",
          "Jjd4n6Ti2wg8qwnCKIQfWVLUb5Z2" : "A6"
        },
        "BLACKLIST" : {
          "Child2" : ""
        },
        "DATA" : {
          "Data" : "Fake data content"
        },
        "REQUESTS" : {
          "FakeReq" : "",
          "Uq9D8YBtxYTMUphBGOmGwxCufiV2" : "A7"
        }
      },
      "USERPROFILE" : {
        "DISPNAME" : "A5"
      }
    },
    "Jjd4n6Ti2wg8qwnCKIQfWVLUb5Z2" : {
      "INVOLVEDIN" : {
        "Sample Group" : "Sample Group"
      },
      "OWNEDGROUPS" : {
        "Sample Group" : "Sample Group"
      },
      "Sample Group" : {
        "ALLOWED" : {
          "Child1" : "",
          "JdGreJBImwYBPGrWHAeyEWBoWE73" : "A5"
        },
        "BLACKLIST" : {
          "Child2" : ""
        },
        "DATA" : {
          "Data" : "Fake data content"
        },
        "REQUESTS" : {
          "FakeReq" : "",
          "Uq9D8YBtxYTMUphBGOmGwxCufiV2" : "A7"
        }
      },
      "USERPROFILE" : {
        "DISPNAME" : "A6"
      }
    },
    "Uq9D8YBtxYTMUphBGOmGwxCufiV2" : {
      "INVOLVEDIN" : {
        "Sample Group" : "Sample Group"
      },
      "OWNEDGROUPS" : {
        "Sample Group" : "Sample Group"
      },
      "Sample Group" : {
        "ALLOWED" : {
          "Child1" : ""
        },
        "BLACKLIST" : {
          "Child2" : ""
        },
        "DATA" : {
          "Data" : "Fake data content"
        },
        "REQUESTS" : {
          "FakeReq" : ""
        }
      },
      "USERPROFILE" : {
        "DISPNAME" : "A7"
      }
    }
  },
  "Names" : {
    "JdGreJBImwYBPGrWHAeyEWBoWE73" : "A5",
    "Jjd4n6Ti2wg8qwnCKIQfWVLUb5Z2" : "A6",
    "Uq9D8YBtxYTMUphBGOmGwxCufiV2" : "A7"
  }
}

我在另一个活动中以主登录方式单独登录用户。

问题:Firebase的功能真的很有趣。有时我没有得到这个错误,一切都完全正常其他时候像这样,我在顶部得到这个错误,我不明白为什么?问题是什么?为什么会发生有时而不是其他?

感谢。

修改

用户已登录是否拥有UID: JdGreJBImwYBPGrWHAeyEWBoWE73

0 个答案:

没有答案