Couchdb-lucene and ad-hoc queries for the authenticated user

211 Views Asked by At

I'm using CouchDB to store data coming from various sources and couchdb-lucene to allow ad-hoc queries. That's important for me because I display the data in a feed and I want this feed to be filterable. CL seems perfect for that.

However, I also want to introduce permissions to the feed app - a user should only be able to see a feed item if he/she has the permission to see it.

Now, I would like to be able to run ad-hoc queries and only return the feed items that the currently authenticated user has permissions to read.

The only solution that I could figure out (so far) was to add a 'permissions' field to each feed item where I store all the permission for the other users (obviously skipping the users that have no permissions for this item at all)

permissions: [{user_id: '123', read: true, write: true}, ...]

and then index this array in CL.

While this will probably work, I feel kind of bad being forced to nest the permissions metadata in the feed item...it might even be a better solution than keeping it separate, but I just don't like that I don't seem to have a choice here.

The only other solution (well, other than dumping CouchDB) would be to run the ad-hoc query without being concerned about the permissions, then run a second query on the server that selects all "my items" and do a set intersection. But those sets can be huge (and if I chunk it, it would require possibly many DB requests => slow).

Is my solution fine or is there anything better? Or is CouchDB just not a good fit for such queries?

Cheers!

1

There are 1 best solutions below

0
On

You are on the right path with keeping that permission data on the document itself. This will be the easiest way for you to build views later on, which will enable you to check for user permissions. So dont worry and just let it flow in that direction. Feeling bad about nesting that data probably comes from previous ages when you were using SQL and RDBMS'es, where you'd want to normalize the hell out of each table. This time it's completely different :)

Btw, the only possibility to do "JOINS" in CouchDB is to use Linked Documents. If you are interested you can give that a try. However it wont enable you to look inside the linked document, while creating a view.