Document Listener based on roles


#1

Hey guys, for my android application , i have a waiter role and an admin role. I have many waiters and only 1 admin.

I have added a document.changelistener to the document to listen for changes. However, i only want to listen when the admin has change the document and not the waiter. Any tips ?


#2

You could add a property that says who changed the document last.


#3

But then how do i use the listener to check?

My code is below
Document doc = database.getDocument(“Drinks”);
doc.addChangeListener(new Document.ChangeListener() {

                @Override
                public void changed(Document.ChangeEvent event) {
                    DocumentChange docChange = event.getChange();
                    String msg = "New revision added: %s. Conflict: %s";
                    msg = String.format(msg, docChange.getAddedRevision(), docChange.isConflict());
                    Log.d(TAG, msg);
                    showToast(msg);                    }
            });
            try {
                doc.createRevision().save();
            } catch (CouchbaseLiteException e) {
                e.printStackTrace();
            }.

#4

Are waiters ever allowed to change the doc? If not, you can block replication in Sync Gateway (or just block them in your app based on how they log in).

Jens is suggesting adding an entry for the user role to the document. You can access the whole document in the listener, and pull out the user type to check.

It sounds like you might want to look more at channels and roles and how to control who can make changes through that on the Sync Gateway side.


#5

Hey bro, yeah both admin and waiter are allowed to update that specific document but i only want to listener for the admin changes as they are something some default values.


#6

So in your observer method check the value of the property, and if it’s not the name of the admin, ignore it.


#7

Thanks bro :slight_smile: hahaha