Skip to content

Instantly share code, notes, and snippets.

@nbari
Created March 5, 2019 16:02
Show Gist options
  • Save nbari/0cc78a09739c3d4ac07adc1f18bbb66a to your computer and use it in GitHub Desktop.
Save nbari/0cc78a09739c3d4ac07adc1f18bbb66a to your computer and use it in GitHub Desktop.
couchdb read-only _design/read_only
function(newDoc, oldDoc, userCtx, secObj) {
var ddoc = this;
secObj.admins = secObj.admins || {};
secObj.admins.names = secObj.admins.names || [];
secObj.admins.roles = secObj.admins.roles || [];
var IS_DB_ADMIN = false;
if(~ userCtx.roles.indexOf("_admin"))
IS_DB_ADMIN = true;
if(~ secObj.admins.names.indexOf(userCtx.name))
IS_DB_ADMIN = true;
for(var i = 0; i < userCtx.roles; i++)
if(~ secObj.admins.roles.indexOf(userCtx.roles[i]))
IS_DB_ADMIN = true;
if(!IS_DB_ADMIN)
throw ({forbidden:"read-only"});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment