I have a recyclerview in my app which with a get, it retrieves all the documents in a collection. Each document contains the uid of the person who created it. I would like to implement a security rule in which you retrieve only the documents matches the uid of the owner. It looks like quite trivial, but I am not able to make it working.
Database has a structure like this
cars
|
|-documentA
|-documentB
|-documentC
an each document:
documentA
|
|
|-fieldA: aaa
|-fieldB: bbb
|-userId: aklsjdaosjfpasf
Security rules at firestore
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{cars=**}/{carId} {
allow read: if request.auth.uid == resource.data.userId;
allow write: if request.auth.uid != null;
}
}
}
EDIT query:
query = firestore.collection("cars").limit(10L)
Firestore Adapter for make queries for the recyclerview
public abstract class FirestoreAdapter<VH extends RecyclerView.ViewHolder>
extends RecyclerView.Adapter<VH> {
private static final String TAG = "Firestore Adapter";
private Query mQuery;
private ListenerRegistration mRegistration;
private ArrayList<DocumentSnapshot> mSnapshots = new ArrayList<>();
public FirestoreAdapter(Query query) {
mQuery = query;
}
public void startListening() {
// TODO(developer): Implement
}
public void stopListening() {
if (mRegistration != null) {
mRegistration.remove();
mRegistration = null;
}
mSnapshots.clear();
notifyDataSetChanged();
}
public void setQuery(Query query) {
// Stop listening
stopListening();
// Clear existing data
mSnapshots.clear();
notifyDataSetChanged();
// Listen to new query
mQuery = query;
startListening();
}
@Override
public int getItemCount() {
return mSnapshots.size();
}
protected DocumentSnapshot getSnapshot(int index) {
return mSnapshots.get(index);
}
protected void onError(FirebaseFirestoreException e) {};
protected void onDataChanged() {}
}
allow create
instead ofallow write
– Max