Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi Tenancy issue, list method with max params not resolving the current tenant id #537

Open
fjloma opened this issue Mar 24, 2022 · 1 comment

Comments

@fjloma
Copy link
Contributor

fjloma commented Mar 24, 2022

The list method with max params returns a PagedResultList. This class obtains the resulting list by calling org.hibernate.query.Query.getResultList, which does not fire the preQuery event that is neccesary for the TenantResolver to be invoked.

This makes the list method to use the last tenant id used in the session, instead of resolving the current one, or issuing a query without a tenant.

For example, the next test will fail, because as we clear the tenant id, the list method should throw an error.

    void "Test list with 'max' parameter"() {
        given: "Create two Authors with tenant T0"
            System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'TENANT')
            MultiTenantAuthor.saveAll([new MultiTenantAuthor(name: "A"), new MultiTenantAuthor(name: "B")])

        when: "Query with no tenant"
            System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, '')
            MultiTenantAuthor.list([max: 2])
        then: "An exception is thrown"
            thrown(TenantNotFoundException)
   }

If you run this, you can see the query that it is being used:

    select
        multitenan0_.id as id1_0_,
        multitenan0_.version as version2_0_,
        multitenan0_.tenant_id as tenant_i3_0_,
        multitenan0_.name as name4_0_ 
    from
        multi_tenant_author multitenan0_ limit ?

However, if we use the list method without any parameters, it works correctly and raises the exception.

Another test:

        given: "Create two Authors with tenant T0"
            System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'TENANT')
            MultiTenantAuthor.saveAll([new MultiTenantAuthor(name: "A"), new MultiTenantAuthor(name: "B")])

        when: "Query with the same tenant as saved, should obtain 2 entities"
            System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'TENANT')
        then:
            MultiTenantAuthor.list([:]).size() == 2

        when: "Query by another tenant, should obtain no entities"
            datastore.sessionFactory.currentSession.clear()
            System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, 'OTHER TENANT')
            def list = MultiTenantAuthor.list([max: 2])
        then:
            list.size() == 0

Again, this fails because the list method with max parameter, is using the first tenant id 'TENANT' (because it wont fire the preQuery event..). You can see that the query used is:

    select
        multitenan0_.id as id1_0_,
        multitenan0_.version as version2_0_,
        multitenan0_.tenant_id as tenant_i3_0_,
        multitenan0_.name as name4_0_ 
    from
        multi_tenant_author multitenan0_ 
    where
        ? = multitenan0_.tenant_id limit ?
[Test worker] DEBUG org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [VARCHAR] - [TENANT]

I've created a fork with this tests and a possible fix here: https://github.com/fjloma/gorm-hibernate5

fjloma added a commit to fjloma/gorm-hibernate5 that referenced this issue Mar 24, 2022
@fjloma fjloma changed the title Multi Tenancy issue with list method with max params Multi Tenancy issue, list method with max params not resolving the current tenant id Mar 24, 2022
@fjloma
Copy link
Contributor Author

fjloma commented Mar 25, 2022

This is also related to this issue grails/grails-data-mapping#1379
The first, last, and findAll(with max param) methods have the same problem, because they call internally to the list method with max param.

puneetbehl pushed a commit that referenced this issue Apr 14, 2022
…urrent tenant id (#539)

* Multi-tenant issue fix. The list method with parameters is not resolving the current tenant id

See #537

* More tests for first, last and findAll, with the same issue with max parameter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant