Intercept criteria instance from Domain.findAllBy

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Intercept criteria instance from Domain.findAllBy

Eric Martineau
Our project has a multi-tenant database - we store data for multiple customers in the same table, and use a "tenantId" column to differentiate between them.  We are trying to hide the tenantId complexity from developers by:

1.  Attaching a tenantId to the user's session when it's initialized
2.  Putting a filter in front of each request that retrieves the tenantId from the user's session and puts it into a ThreadLocal variable
3.  Intercepting GORM calls and injecting a "WHERE TenantId=xxx" automatically using the ThreadLocal variable

The most efficient way to do this would be to intercept the Criteria object that's created during the findAllBy, etc method calls.  The way it's coded, there's no way to access that variable.  I can think of a half-dozen ways to add this capability, with different levels of "dirtying" the grails core.

Is this something that other people would be interested in, or should I figure out a solution on my own?

Thanks in advance,