Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.0.0, removes subdomain specific code, improves tenantify querying …
…logic
- Loading branch information
Showing
6 changed files
with
73 additions
and
165 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,82 @@ | ||
// constants | ||
TENANTIFY_TYPE_SUBDOMAIN = 'subdomain'; | ||
|
||
// tenantify global | ||
Tenantify = {}; | ||
|
||
Tenantify._headers = false; | ||
Tenantify._tenantCollection = false; | ||
Tenantify._tenantField = '_tenantId'; // the tenant field name in the tenant collection | ||
|
||
Tenantify.tenantIdentifyMethod = TENANTIFY_TYPE_SUBDOMAIN; | ||
|
||
Tenantify._currentTenantId = false; | ||
Tenantify._tenantIdCache = {}; | ||
|
||
// functions | ||
Tenantify.setTenantCollection = function(collection, field) { | ||
this._tenantCollection = collection; | ||
this._tenantField = field; | ||
} | ||
|
||
Tenantify.collection = function(collection, options) { | ||
var defaults = { | ||
tenantField: Tenantify._tenantField, | ||
denyForNonTenant: true | ||
}; | ||
|
||
var tenantifyOptions = options; | ||
|
||
tenantifyOptions = _.extend(defaults,tenantifyOptions); | ||
this._tenantCollection = collection; | ||
|
||
collection.before.find(function(userId, selector, options) { | ||
selector = selector || {}; | ||
options = options || {}; | ||
|
||
var tenantId = Tenantify._currentTenantId || Tenantify.getTenantId(); | ||
|
||
if(tenantId) { | ||
selector[tenantifyOptions.tenantField] = tenantId; | ||
} else if(tenantifyOptions.denyForNonTenant) { | ||
selector[tenantifyOptions.tenantField] = 'disallowed'; | ||
} | ||
}); | ||
|
||
collection.before.insert(function(userId, doc) { | ||
var tenantId = Tenantify._currentTenantId || Tenantify.getTenantId(); | ||
|
||
if(tenantId) { | ||
doc[tenantifyOptions.tenantField] = tenantId; | ||
} else if(tenantifyOptions.denyForNonTenant) { | ||
doc[tenantifyOptions.tenantField] = 'disallowed'; | ||
} | ||
}); | ||
if (field) { | ||
this._tenantField = field; | ||
} | ||
} | ||
|
||
Tenantify.getTenantId = function(method) { | ||
// if a tenantId is already set, return it | ||
if(Tenantify._currentTenantId) | ||
return Tenantify._currentTenantId; | ||
|
||
// get the tenant id by using one of the given methods | ||
switch(this.tenantIdentifyMethod) { | ||
case TENANTIFY_TYPE_SUBDOMAIN: | ||
if(this._headers) { | ||
var host = splitHostname(this._headers.host); | ||
|
||
if(typeof(host.subdomain) !== undefined && host.subdomain != '') { | ||
// it's a subdomain. let's find the tenant id | ||
var tenantData = Tenantify._tenantCollection.findOne({ subdomain: host.subdomain }); | ||
|
||
if(tenantData) { | ||
Tenantify._currentTenantId = tenantData._id; | ||
return tenantData._id; | ||
} | ||
} | ||
} | ||
break; | ||
default: | ||
return false; | ||
break; | ||
} | ||
|
||
return false; | ||
Tenantify.collection = function(collection, options) { | ||
var defaults = { | ||
tenantField: Tenantify._tenantField, | ||
denyForNonTenant: true | ||
}; | ||
|
||
var tenantifyOptions = options; | ||
|
||
tenantifyOptions = _.extend(defaults,tenantifyOptions); | ||
|
||
var indexObject = {}; | ||
indexObject[tenantifyOptions.tenantField] = 1; | ||
collection._ensureIndex(indexObject); | ||
|
||
collection.before.find(function(userId, selector, options) { | ||
selector = selector || {}; | ||
options = options || {}; | ||
|
||
if (selector._ignoreTenantify) { | ||
delete selector._ignoreTenantify; | ||
return; | ||
} | ||
|
||
var tenantId = Tenantify.getTenantId(userId); | ||
|
||
if(tenantId) { | ||
selector[tenantifyOptions.tenantField] = tenantId; | ||
} else if(tenantifyOptions.denyForNonTenant) { | ||
selector[tenantifyOptions.tenantField] = 'disallowed'; | ||
} | ||
}); | ||
|
||
collection.before.insert(function(userId, doc) { | ||
var tenantId = Tenantify.getTenantId(userId); | ||
|
||
if(tenantId) { | ||
doc[tenantifyOptions.tenantField] = tenantId; | ||
} else if(tenantifyOptions.denyForNonTenant) { | ||
doc[tenantifyOptions.tenantField] = 'disallowed'; | ||
} | ||
}); | ||
|
||
collection.before.update(function(userId, doc) { | ||
var tenantId = Tenantify.getTenantId(userId); | ||
|
||
if(tenantId) { | ||
// do | ||
} else if(tenantifyOptions.denyForNonTenant) { | ||
modifier = {} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Common functions | ||
*/ | ||
|
||
function splitHostname(hostname) { | ||
var result = {}; | ||
var regexParse = new RegExp('([a-z\-0-9]{2,63})\.([a-z\.]{2,5})$'); | ||
var urlParts = regexParse.exec(hostname); | ||
|
||
if(urlParts) { | ||
result.domain = urlParts[1]; | ||
result.type = urlParts[2]; | ||
result.subdomain = hostname.replace(result.domain + '.' + result.type, '').slice(0, -1); | ||
Tenantify.getTenantId = function(userId) { | ||
// if a tenantId is already set, return it | ||
if(Tenantify._tenantIdCache[userId]) | ||
return Tenantify._tenantIdCache[userId]; | ||
|
||
if(result.subdomain.indexOf('.') > -1) { | ||
result.subdomain = result.subdomain.split('.')[0]; | ||
} | ||
} | ||
var userData = Meteor.users.findOne(userId); | ||
var tenantId = userData.profile[Tenantify._tenantField]; | ||
|
||
Tenantify._tenantIdCache[userId] = tenantId; | ||
|
||
return result; | ||
return tenantId; | ||
} |