Reputation: 35
I have am trying to get the admin rights of a user which is either true or false. I do not think i am getting the current logged in users details, the below code is what i have but when i debug it, it seems that it loops through all the users in the firebase table and if any are true it sets the buttons to visible
databaseUsers = FirebaseDatabase.getInstance().getReference("users");
// buttonAddProducts.setVisibility(View.GONE);
// buttonAddBeverages.setVisibility(View.GONE);
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference currentUserReference = databaseUsers.child(uid);
currentUserReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot userSnapshot) {
if (userSnapshot.exists()) {
User user = userSnapshot.getValue(User.class);
if(user.getAdminUser() == true)
{
buttonAddProducts.setVisibility(View.VISIBLE);
buttonAddBeverages.setVisibility(View.VISIBLE);
}
}
else{
Log.i("myInfoTag", "Connection problem");
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore exceptions
}
});
Upvotes: 0
Views: 82
Reputation: 600036
You're listening for all users, then loop over them, and if any of them is an admin, you show the buttons.
If you only want to show the buttons for the current user, you should only query the current users. Within your current data model that can be something like:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference currentUserReference = databaseUsers.child(uid);
currentUserReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
User user = venueSnapshot.getValue(User.class);
if(user.getAdminUser() == true)
{
buttonAddProducts.setVisibility(View.VISIBLE);
buttonAddBeverages.setVisibility(View.VISIBLE);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException(); // don't ignore exceptions
}
});
Not only will that give you the behavior you want, it also drastically reduces the bandwidth your app uses.
Upvotes: 0
Reputation: 1
First of all, when we are referring to Firebase, we cannot speak about tables. In a NoSQL database there are no tables. There are only pairs of key and value.
To solve your problem, i recomand you change your database a little bit, by adding a new node in your Firebase database named userAdmins
. When you add a user into your database and is also an admin, add him in this new section. Your new node should look like this:
Firebase-root
|
--- userAdmins
|
--- userId1: true
|
--- userId2: true
This practice is named denormalization
and for that i recomand you see this video, Denormalization is normal with the Firebase Database. If you'll see this video, you'll have a better understanding about this practice.
To verify if a user is admin, just use exists()
method on the DataSnapshot
object like this:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("userAdmins").child(uid);
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()) {
//do something
} else {
//do something else
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
uidRef.addListenerForSingleValueEvent(eventListener);
In which uid
is the id of the user you want to verify if is an admin.
Upvotes: 1