Sorry in advance if my question sounds silly. I am a newby. In my app I have an adapter which shows the documents queried from Firebase Firestore.
At the moment there is a OnSwipe listener which can delete documents. I want to set it the way that the only documents which were recorded by the certain user can be deleted. Meaning: user can only delete documents writen by him and cannot delete documents written by others. Please advise how to do this?
At the moment the code looks like this:
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;
}
@Override
public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
adapter.deleteItem(viewHolder.getAdapterPosition());
}
}).attachToRecyclerView(recyclerView);
You need to implement security rules to restrict the delete operation to the author of the document. In this document[1], it mentions how to set rules for write, which also contains create, update, and delete rules [2] . So you can set a rule like:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read;
allow delete: if request.auth.uid == resource.data.userid;
}
}
}
As mentioned above, it would be clearer if you could provide the contents of deleteitem(), and the document structure.
[1]https://firebase.google.com/docs/firestore/security/rules-query [2] https://firebase.google.com/docs/firestore/security/rules-structure#granular_operations
Thank you all for your help. I did modify my delete method and it worked: Thank you for your hints and help
public void deleteItem(int position, String userID) {
String docUserID = getSnapshots().getSnapshot(position).getString("userID");
Log.i("deleteItem", "docUserID: " + docUserID + " userID: " + userID);
if (docUserID != null) {
if (docUserID.equals(userID)) {
getSnapshots().getSnapshot(position).getReference().delete();
Log.i("deleteItem", " if worked... ");
} else {
Log.i("deleteItem", " if did not work");
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.