Last active
August 24, 2022 23:08
-
-
Save rajagp/6427f016e7994847c6cdee28e51479f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"interface":":4984", | |
"adminInterface":":4985", | |
"metricsInterface":":4986", | |
"use_tls_server":false, | |
"log": ["*"], | |
"logging": { | |
"log_file_path": "/var/tmp/sglogs", | |
"console": { | |
"log_level": "debug", | |
"log_keys": ["*"] | |
}, | |
"error": { | |
"enabled": true, | |
"rotation": { | |
"max_size": 20, | |
"max_age": 180 | |
} | |
}, | |
"warn": { | |
"enabled": true, | |
"rotation": { | |
"max_size": 20, | |
"max_age": 90 | |
} | |
}, | |
"info": { | |
"enabled": false | |
}, | |
"debug": { | |
"enabled": false | |
} | |
}, | |
"disable_persistent_config":true, | |
"databases": { | |
"userprofile": { | |
"import_docs": true, | |
"bucket":"demo", | |
"server": "couchbase://localhost", | |
"enable_shared_bucket_access":true, | |
"delta_sync": { | |
"enabled":true | |
}, | |
"num_index_replicas":0, | |
"import_filter": ` | |
function(doc) { | |
return true; | |
} | |
`, | |
"username": "admin", | |
"password": "password", | |
"users": { | |
"user1": {"password": "pass", "admin_channels": ["*"]}, | |
"[email protected]": {"password": "password"}, | |
"user2": {"password": "pass", "admin_channels": ["*"]}, | |
"user3": {"password": "pass", "admin_channels": ["*"]}, | |
"mod": {"password": "pass", "admin_roles": ["moderator"]}, | |
"admin": {"password": "pass", "admin_roles": ["admin"]} | |
}, | |
"sync": ` | |
function sync(doc, oldDoc) { | |
/* Authorization */ | |
// Verify the user making the request is the same as the one in doc's email | |
requireUser(doc.email); | |
/* Data Validation */ | |
if (!isDelete()) { | |
// Validate the presence of email fields | |
validateNotEmpty("email", doc.email); | |
// Check if document is being created / added for first time | |
// We allow any user to create the document | |
if (isCreate()) { | |
// Validate that the document Id _id is prefixed by owner. | |
var expectedDocId = "user" + "::" + doc.email; | |
if (expectedDocId != doc._id) { | |
throw({forbidden: "user doc Id must be of form user:email"}); | |
} | |
} else { | |
// Validate that the email hasn't changed. | |
validateReadOnly("email", doc.email, oldDoc.email); | |
} | |
} | |
/* Routing */ | |
// Subsequent updates to document must be authorized | |
var email = getEmail(); | |
// Add doc to the user's channel. | |
channel("channel." + email); | |
/* Access Control */ | |
// Give user read access to channel | |
if (!isDelete()) { | |
// Deletion of user document is essentially deletion of user | |
access(email,"channel." + email) | |
} | |
// get type property | |
function getType() { | |
return (isDelete() ? oldDoc.type : doc.type); | |
} | |
// get email Id property | |
function getEmail() { | |
return (isDelete() ? oldDoc.email : doc.email); | |
} | |
// Check if document is being created/added for first time | |
function isCreate() { | |
// Checking false for the Admin UI to work | |
return ((oldDoc == false) || (oldDoc == null || oldDoc._deleted) && !isDelete()); | |
} | |
// Check if this is a document update | |
function isUpdate() { | |
return (!isCreate() && !isDelete()); | |
} | |
// Check if this is a document delete | |
function isDelete() { | |
return (doc._deleted == true); | |
} | |
// Verify that specified property exists | |
function validateNotEmpty(key, value) { | |
if (!value) { | |
throw({forbidden: key + " is not provided."}); | |
} | |
} | |
// Verify that specified property value has not changed during update | |
function validateReadOnly(name, value, oldValue) { | |
if (value != oldValue) { | |
throw({forbidden: name + " is read-only."}); | |
} | |
} | |
} | |
` | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment