Hi I have the following use case, assuming I have a Cloudant doc with the following field:
{
public: false, // Visibility of the doc
permissions: [1,2,3] // List of user IDs that are allowed view access to the doc
}
Only privileged users (as stated in permissions field) have the access on it or if its public (public: true) then every users have the access to it.
My problem arises when I am trying to write a search index to retrieve all docs that the current user has access to. My search index will look something like this:
function (doc) {
if (!doc.public) {
doc.permissions.forEach(function(entry) {
index("user_id", parseInt(entry));
});
} else {
index("user_id", ???); // If its public document, how should I index it?
}
}
It works well on the doc that has the public set to false, but my search index fails to return those documents with the public set to true. My search query looks something like this:
q=user_id:1
Any suggestions as I really need to implement this on the Cloudant layer instead of on my controller layer.
According to your description, I find the views much more adapted for the situation. If you absolutely need query search, I can try to figure out another solution.
So first of all, I would do a view like with a mapping function like this :
function(doc) {
if (doc.public) //If it's public, we will emit the magic ID *. Feel free to use any other key that will be unique.
emit('*');
else if (doc.permissions && Array.isArray(doc.permissions)) //We valid the permission property
for (var i in doc.permissions)
if (doc.permissions[i])
emit(doc.permissions[i]);
}
Then, when I would query to get the documents allowed for the user, I would simply query the view like this : _view/byUserId?keys=['myUserId','*']
For each row, you will have the id of the document allowed in the id property.