Skip to content

Instantly share code, notes, and snippets.

@ThibaultJanBeyer
Created July 5, 2018 17:21
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThibaultJanBeyer/389bca37f4b6973b84908895300bb7de to your computer and use it in GitHub Desktop.
Save ThibaultJanBeyer/389bca37f4b6973b84908895300bb7de to your computer and use it in GitHub Desktop.
read only couchdb: add this to the design documents of the database you want to protect. Now, only users with role admin will be able to edit or add documents to this database.
{
"_id": "_design/auth",
"language": "javascript",
"validate_doc_update": "function(newDoc, oldDoc, userCtx) { if (userCtx.roles.indexOf('admin') !== -1) { return; } else { throw ({ forbidden: 'Only admins may edit the database' }); } }"
}
@ThibaultJanBeyer
Copy link
Author

ThibaultJanBeyer commented Jul 5, 2018

With cURL:

curl -X PUT http://username:password@localhost:5984/database/_design/auth -d "{ \"language\": \"javascript\", \"validate_doc_update\": \"function(newDoc, oldDoc, userCtx) { if (userCtx.roles.indexOf('admin') !== -1) { return; } else { throw ({ forbidden: 'Only admins may edit the database' }); } }\"}"

Replace username, password and database accordingly.


Change userCtx.roles.indexOf('admin') !== -1 to userCtx.name === 'admin' if you want to check for a specific username.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment