Skip to content

Commit

Permalink
Multi-tenant issue fix. The list method with parameters is not resolv…
Browse files Browse the repository at this point in the history
…ing the current tenant id

See grails#537
  • Loading branch information
fjloma committed Mar 24, 2022
1 parent 64dabf4 commit 0d39148
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class HibernateGormStaticApi<D> extends AbstractHibernateGormStaticApi<D> {
return new PagedResultList(
hibernateTemplate,
persistentEntity,
query,
hibernateQuery,
criteriaQuery,
queryRoot,
criteriaBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@

import org.grails.datastore.mapping.model.PersistentEntity;
import org.grails.orm.hibernate.GrailsHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.internal.CriteriaImpl;
import org.hibernate.query.Query;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.sql.SQLException;
import java.util.Iterator;

public class PagedResultList extends grails.gorm.PagedResultList {

Expand All @@ -40,12 +36,11 @@ public class PagedResultList extends grails.gorm.PagedResultList {

public PagedResultList(GrailsHibernateTemplate template,
PersistentEntity entity,
Query query,
HibernateHqlQuery hibernateHqlQuery,
CriteriaQuery criteriaQuery,
Root queryRoot,
CriteriaBuilder criteriaBuilder) {
super(null);
resultList = query.getResultList();
super(hibernateHqlQuery);
hibernateTemplate = template;
this.criteriaQuery = criteriaQuery;
this.queryRoot = queryRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import org.grails.datastore.mapping.multitenancy.resolvers.SystemPropertyTenantR
import org.grails.orm.hibernate.HibernateDatastore
import org.hibernate.Session
import org.hibernate.dialect.H2Dialect
import org.springframework.orm.hibernate5.SessionHolder
import org.springframework.transaction.support.TransactionSynchronizationManager
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification
Expand Down Expand Up @@ -181,6 +179,55 @@ class PartitionedMultiTenancySpec extends Specification {

}

void "Test list without '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"
datastore.sessionFactory.currentSession.clear()
System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, '')
MultiTenantAuthor.list()
then: "An exception is thrown"
thrown(TenantNotFoundException)

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

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"
datastore.sessionFactory.currentSession.clear()
System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, '')
MultiTenantAuthor.list([max: 2])
then: "An exception is thrown"
thrown(TenantNotFoundException)

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

when: "Check the paged results"
def sameTenantList = MultiTenantAuthor.list([max:1])
then:
sameTenantList.size() == 1
sameTenantList.getTotalCount() == 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
list.getTotalCount() == 0
}
}

class MyTenantResolver extends SystemPropertyTenantResolver implements AllTenantsResolver {
Expand Down

0 comments on commit 0d39148

Please sign in to comment.