Skip to content

Instantly share code, notes, and snippets.

@PaulCapestany
Created April 18, 2013 00:58
Show Gist options
  • Select an option

  • Save PaulCapestany/5409031 to your computer and use it in GitHub Desktop.

Select an option

Save PaulCapestany/5409031 to your computer and use it in GitHub Desktop.
{
"interface": ":4984",
"adminInterface": ":4985",
"persona": {
"origin": "http://myaweomeserver.com:4984/"
},
"log": ["CRUD", "Auth", "HTTP+"],
"databases": {
"couchchat": {
"server": "http://localhost:8091",
"bucket": "couchchat",
"users": {
"GUEST": {
"disabled": true
}
},
"sync": `
function(doc, oldDoc, userCtx, secObj) {
if (!Array.isArray) {
Array.isArray = function(vArg) {
return Object.prototype.toString.call(vArg) === "[object Array]";
};
}
if (doc.channel_id) {
// doc belongs to a channel
channel("ch-" + doc.channel_id);
// this document describes a channel
if (doc.channel_id == doc._id) {
// magic document, treat it carefully
if (oldDoc && oldDoc.owners.indexOf(userCtx.name) == -1) {
throw ({
unauthorized: "you are not a channel owner"
});
}
if (!Array.isArray(doc.owners)) {
throw ({
forbidden: "owners must be an array"
})
}
// grants access to the channel to all members and owners
var members = [];
if (Array.isArray(doc.members)) {
members = doc.members;
}
var them = doc.owners.concat(members);
access(them, "ch-" + doc._id);
// notify members of the new channel
// TODO this can be removed once
// https://github.com/couchbaselabs/sync_gateway/issues/31
// is fixed
for (var i = 0; i < them.length; i++) {
channel("chs-" + them[i]);
};
}
}
if (doc.type == "profile") {
channel("profiles");
var user = doc._id.substring(doc._id.indexOf(":") + 1);
if (userCtx.name != user) {
throw ({
unauthorized: "you can only edit your own profile"
});
}
access(user, "profiles");
// TODO remove when #31 is fixed
access(user, "chs-" + user);
}
}
`
},
"couchchat2": {
"server": "http://localhost:8091",
"bucket": "couchchat2",
"users": {
"GUEST": {
"disabled": true
}
},
"sync": `
function(doc, oldDoc, userCtx, secObj) {
if (!Array.isArray) {
Array.isArray = function(vArg) {
return Object.prototype.toString.call(vArg) === "[object Array]";
};
}
if (doc.channel_id) {
// doc belongs to a channel
channel("ch-" + doc.channel_id);
// this document describes a channel
if (doc.channel_id == doc._id) {
// magic document, treat it carefully
if (oldDoc && oldDoc.owners.indexOf(userCtx.name) == -1) {
throw ({
unauthorized: "you are not a channel owner"
});
}
if (!Array.isArray(doc.owners)) {
throw ({
forbidden: "owners must be an array"
})
}
// grants access to the channel to all members and owners
var members = [];
if (Array.isArray(doc.members)) {
members = doc.members;
}
var them = doc.owners.concat(members);
access(them, "ch-" + doc._id);
// notify members of the new channel
// TODO this can be removed once
// https://github.com/couchbaselabs/sync_gateway/issues/31
// is fixed
for (var i = 0; i < them.length; i++) {
channel("chs-" + them[i]);
};
}
}
if (doc.type == "profile") {
channel("profiles");
var user = doc._id.substring(doc._id.indexOf(":") + 1);
if (userCtx.name != user) {
throw ({
unauthorized: "you can only edit your own profile"
});
}
access(user, "profiles");
// TODO remove when #31 is fixed
access(user, "chs-" + user);
}
}
`
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment