We have a couchapp application with multiple users and a complex system of permissions. Our models are of two kinds: Foo and bar.
Users have admin access to their own Foo and Bar, and can be given permission to see, change and delete other people's Foo and bar.
Example:
User Sabrina has these models:
Foo {
_id: 1
}
Foo {
_id: 2
}
Bar {
_id:1
}
Bar {
_id:2
}
Of course the real models are larger documents.
She wants to give Giulia read access to her Foos, and read and write access to her first bar. She also wants Giulia not to be able to see her second Bar.
How can we model this kind of permissions in couchdb?
This is the solution we are using, but it seems a lot complex and we wonder if there's a simpler one:
We have a selection of roles:
{username}:admin
: can read, write, delete everything on every database related to the user
{username}:foos:read
: can read every document in the foos database related to the user
{username}:foos:write
: can write every document in the foos database related to the user
{username}:{bar}:read
: can read the Bar database related to the user
{username}:{bar}:write
: can write the Bar database related to the user
When Sabrina register to the app, we create a new sabrina-foos
database, and we give to the user Sabrina the role sabrina:admin
.
The sabrina-foos
database is created with a _security
document granting access to roles sabrina:admin
, sabrina:foos:read
, sabrina:foos:write
.
The sabrina-foos
database is created with a validation function which allows write access to the roles sabrina:admin
, sabrina:foos:write
.
When Sabrina decides to let Giulia see her foos, we give Giulia the role sabrina:foos:read
When Sabrina creates a new Bar called 'Bar 1', we create a new sabrina-bar_1
database.
The sabrina-bar_1
database is created with a _security document granting access to roles sabrina:admin
, sabrina:bar_1:read
, sabrina:bar_1:write
The sabrina-bar_1
database is created with a validation function which allows write access to the roles sabrina:admin
, sabrina:bar_1:write
.
Of course, being this a CouchApp, the creation of databases and editing of user models is handled by a Node Process.