如何根据ID授予对Firebase数据库中某些对象的访问权限 [英] How can I give access to certain objects in Firebase database based on id
问题描述
我有这个数据结构:
红色箭头指向动物ID,蓝色箭头指向用户ID.每个用户都有一个或多个动物.
The red arrows points at animal IDs, and the blue arrow points at user IDs. Every user have one or many animals.
我尝试了不同的方法来仅显示具有存储在当前用户节点中的id的动物.
I have tried different methods for showing only the animals that have id that is stored in the current user node.
示例:如果我有UID = 48onHXIxgDP465j5WW16oo7psNm2
(用户"中的第一个),我想显示来自"dog2"和"dog3"的数据.
Example: If I have UID = 48onHXIxgDP465j5WW16oo7psNm2
(the first one in "users") I want to show the data from: "dog2" and "dog3".
现在,我拥有以下代码,这些代码从数据库中的动物"节点获取快照,然后从每个子节点获取数据.
Now iIhave the following code that gets snapshot from the "animals" node in the database, and then gets data from every child.
myAnimalRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<AnimalCard>();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
AnimalCard value = dataSnapshot1.getValue(AnimalCard.class);
AnimalCard animal = new AnimalCard();
String name = value.getName();
int age = value.getAge();
String url = value.getUrl();
animal.setName(name);
animal.setAge(age);
animal.setUrl(url);
list.add(animal);
}
recyclerViewSetAdapter();
progressDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG1, "failed to read value: " + databaseError.toException());
}
});
如何获取我的代码以过滤掉用户节点中没有其ID的每只动物?
How can get my code to filter out every animal that does not have their ID in the user node?
我想让用户使用存储在数据库中的UID进行访问的原因是因为稍后我要进行访问,以便多个用户可以访问同一动物.
The reason I want to make the user get access with an UID stored in the database is because later on I want to make it so that multiple users can get access to the same animal.
推荐答案
要实现此目的,您需要查询数据库两次.请使用以下代码:
To achieve this, you need to query your database twice. Please use the following code:
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
String uid = firebaseUser.getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = usersRef.child("users").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String aid = ds.getKey();
DatabaseReference animalRef = rootRef.child("animals").child(aid);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dSnapshot) {
int age = dSnapshot.child("age").getValue(Integer.class);
String name = dSnapshot.child("name").getValue(String.class);
String url = dSnapshot.child("url").getValue(String.class);
Log.d("TAG", age + " / " + name + " / " + url);
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
animalRef.addListenerForSingleValueEvent(valueEventListener);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(eventListener);
其中uid
是已登录用户的ID,aid
是动物的ID.您的输出将是:
In which uid
is the id of the logged-in user and aid
is the id of the animal. Your output will be:
11 / dog1 / http...
12 / dog2 / http...
这篇关于如何根据ID授予对Firebase数据库中某些对象的访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!