Skip to content

Commit

Permalink
FEATURE: Multi-tenant : different work list by host (Hypertopic#139).
Browse files Browse the repository at this point in the history
  • Loading branch information
franck-eyraud committed Mar 3, 2018
1 parent 40af85c commit 7ad32e3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
7 changes: 6 additions & 1 deletion couchdb/couchapp.sample.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"index" : "_rewrite/",
"traduxio" : { "chat" : false, "sessions" : false }
"traduxio" : {
"chat" : false
, "sessions" : false
,"multi-tenant":true
,"default-tenant":"localhost"
}
}
31 changes: 30 additions & 1 deletion couchdb/lib/traduxio.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ Traduxio= {
return false;
},

sameTenant: function(work) {
if (this.isOriginalWork(work) && this.config["multi-tenant"] && this.req) {
if (this.req.tenant) {
if (work.privileges.tenant) {
log("tenant :"+this.req.tenant+", work is from "+work.privileges.tenant);
return this.req.tenant==work.privileges.tenant;
} else {
return false;
}
} else {
//if req headers is absent, means that we are in validate function
//which has no access to req object, so we force access to doc
return !work.privileges.tenant || !this.req.headers;
}
}
return true;
},

isOwner:function (work) {
work=work || this.doc;
if (work) {
Expand Down Expand Up @@ -147,6 +165,7 @@ Traduxio= {
this.config.debug && log ("can access absent work");
return true;
} else {
if (!this.sameTenant(work)) return false;
this.config.debug && log (this.getUser().name + " can access ?");
if (this.isAdmin()) return true;
if (this.isOwner(work)) return true;
Expand Down Expand Up @@ -393,4 +412,14 @@ Traduxio= {
};

if (typeof doc != "undefined" && doc!=null) Traduxio.doc=doc;
if (typeof req != "undefined" && req!=null) Traduxio.req=req;
if (typeof req != "undefined" && req!=null) {
Traduxio.req=req;
if (Traduxio.config["multi-tenant"] && Traduxio.req.headers && Traduxio.req.headers["Host"]) {
var tenant=Traduxio.req.headers["Host"].split(":");
tenant=tenant[0];
if (tenant!=Traduxio.config["defaul-tenant"]) {
Traduxio.req.tenant=tenant;
log("for this request, tenant is "+tenant);
}
}
}
3 changes: 3 additions & 0 deletions couchdb/shows/work.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ function(o, req) {
} else {
o.privileges.public=true;
}
if (Traduxio.req.tenant) {
o.privileges.tenant=Traduxio.req.tenant;
}
}
o.privileges=o.privileges||{};
var data = {
Expand Down
5 changes: 4 additions & 1 deletion couchdb/updates/work.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function(work, req) {
var version_name = req.query.version;
if (["PUT","DELETE"].indexOf(req.method)!=-1) {

if (work===null) {
if (work===null || !Traduxio.sameTenant(work)) {
return [null,{code:404,body:"Not found"}];
}
var original=false,
Expand Down Expand Up @@ -57,6 +57,9 @@ function(work, req) {
} else {
doc.privileges.public=true;
}
if (Traduxio.req.tenant) {
work.privileges.tenant=Traduxio.req.tenant;
}
work._id=work.id || req.uuid;
delete work.id;
work.edits=[];
Expand Down

0 comments on commit 7ad32e3

Please sign in to comment.