Frage

WE are creating a multi-tenant system using Grails framework. Our database is using MongoDB and our multi-tenancy strategy is sharing same database which means most document will have a tenant id reference.

We are looking for a plugin or a common way to inject or filter the tenant id while doing the database query and update.

Any idea?

Thanks

War es hilfreich?

Lösung

One possible approach:

It is possible to hook into queries by listening for PreQuery events with an ApplicationListener (similar to handling PreUpdate events, etc., see the GORM docs). You can then transform the org.grails.datastore.mapping.query.Query object to add in an additional criterion to filter by the tenant id. You could make the applicable tenant id accessible to your PreQuery event listener by setting a ThreadLocal appropriately, or by adding it to the current session as a session config (see Session.setSessionConfig). You could automatically enable the tenancy filtering for the request using a Filter perhaps (which would set the thread local or session config based on a request param or whatever). Obviously, you'd wrap all this up in some services and ideally a plugin.

One issue is that 'get' and 'getAll' requests (also used internally to fetch some associations) currently bypass the Query objects and hence avoid any filtering you apply, though this presumably won't be an issue, since IDs will be globally unique, but you might want to add a sanity or security check to ensure tenancy isn't violated. Dynamic finders and other query methods (where, DetachedCriteria, etc.) should be ok ('findById' would filter, but 'get' won't). Obviously, any direct GMongo queries will also bypass this feature.

Of course there are lots of alternative approaches if you're happy for your controller/service code to know a little bit about tenancy.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top