Freitag, 21. November 2014

Couchbase: set document owner in sync function using requireUser

Couchbase's sync function is the central point where you can manage access rights of the documents in the database. In addition to the new and old document it used to receive a userContext as a third parameter that could be used to retrieve the currently logged in user name. This parameter was removed because a current user is not available in all the situations where the sync function gets invoked by Couchbase.

However, a common usecase is to set the currently logged in user as the owner of the document, for example to make sure only he can edit it. How do you achieve giving ownership of a document to the current user then? If you store the owner in doc.creator, then in your sync function, you can make sure the currently logged in user matches that by invoking requireUser(doc.creator);
After having verified doc.creator by this call, you can use it to set up your access model, for instance by setting the channel for the document.

Relevant post from the mailing list for more details.