grailsgrails-ormgrails-4

DRY disable of Grails 4 multi-tenancy for admins


I am using discriminator-based multi-tenancy in Grails 4, upgrading from Grails 2 where I used the hibernate-filter plugin. In Grails 2, I could disable the hibernate-filter in a Grails filter for admin roles - thereby not needing "if (admin)"-checks throughout my application. Can I somehow disable multi-tenancy in Grails 4 and keep it DRY?

Based on Jeffs comment, I rephrase my question for future references:

Can multitenancy be fully disabled for the entire request any time the request can be authenticated to come from a client that has an admin role?


Solution

  • I think I finally found a solution to this. Debugging Grails I found this in AbstractHibernateDatastore.java:

    Serializable currentId = Tenants.currentId(this);
    if(ConnectionSource.DEFAULT.equals(currentId)) {
      disableMultiTenancyFilter();
    }
    

    Which gave me the idea to simply return ConnectionSource.DEFAULT instead of the tenant id from my tenant resolver, when I want to disable multitenancy (in my case when the current user is an admin).

    A simple and straightforward solution, which works for Grails 4.0.12. I'll see if I can get it officially documented, since my admin use-case shouldn't be uncommon for multitenant applications.