Skip to content

Instantly share code, notes, and snippets.

@rajagp
Last active November 8, 2021 04:51
Show Gist options
  • Save rajagp/d757f8b5ef822e0da381cfcb9276b275 to your computer and use it in GitHub Desktop.
Save rajagp/d757f8b5ef822e0da381cfcb9276b275 to your computer and use it in GitHub Desktop.
sync-gateway-config-userprofile-demo.json
{
"interface":":4984",
"adminInterface":":4985",
"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,
"server_tls_skip_verify": true,
"databases": {
"userprofile": {
"import_docs": true,
"bucket":"userprofile",
"server": "couchbases://cb-server",
"enable_shared_bucket_access":true,
"delta_sync": {
"enabled":false
},
"num_index_replicas":0,
"username": "admin",
"password": "password",
"users": { "demo@example.com": { "password": "password"},"demo1@example.com": { "password": "password"},"demo2@example.com":{"password":"password" },"demo3@example.com":{"password":"password"},"demo4@example.com":{"password":"password"}},
"sync": `
function sync(doc, oldDoc) {
console.log("********Procesing Doc. Is oldDoc == null? " + (oldDoc == null));
/* Data Validation */
// Validate the presence of email field. This is the "username"
validateNotEmpty("email", doc.email);
// Validate that the document Id _id is prefixed by owner.
var expectedDocId = "user" + "::" + doc.email;
if (expectedDocId != doc._id) {
// reject document
throw({forbidden: "user doc Id must be of form user::email"});
}
try {
// Check if this is an import processing (done with admin credentials)
requireAdmin();
if (!isDelete()) {
/* Routing */
var username = getEmail();
var channelId = "channel."+ username;
channel(channelId);
// Give user access to document
access(username,channelId);
}
}catch (error) {
console.log("This is not a doc import " + error);
// If non admin client replication
if (!isDelete()) {
/* Authorization */
// Verify the user making the request is the same as the one in doc's email
requireUser(doc.email);
// Check if document is being created / added for first time
// We allow any user to create the document
if (isCreate()) {
/* Routing */
// Add doc to the user's channel.
var username = getEmail();
var channelId = "channel."+ username;
channel(channelId);
// Give user access to document
access(username,channelId);
} else {
// This is an update
// Validate that the email hasn't changed.
validateReadOnly("email", doc.email, oldDoc.email);
// Add doc to the user's channel.
var username = getEmail();
var channelId = "channel."+ username;
channel(channelId);
// Give user access to document
access(username,channelId);
}
}
}
// 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