Skip to content

grails/gorm-mongodb#77 Updated MultiTenantEventListener #1245

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ class MultiTenantEventListener implements PersistenceEventListener {
TenantId tenantId = entity.getTenantId();
if(tenantId != null) {
Serializable currentId = Tenants.currentId(datastoreClient.getClass());
query.eq(tenantId.getName(), currentId );
if (currentId != ConnectionSource.DEFAULT) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@graemerocher The reason I decided to add this check here is that in case of non shared multi-tenant database (e.g. multi-tenancy across databases) we might be using that default data source in case tenantId is not specified and we do not know in Tenants.groovy that if it's a shared or nonshared multi-tenancy. However, I am not sure about the above logic so please suggest if we need to fix this some other way.

query.eq(tenantId.getName(), currentId );
}
}
}
}
}
else if(event instanceof PreInsertEvent) {
} else if(event instanceof PreInsertEvent) {
PreInsertEvent preInsertEvent = (PreInsertEvent) event;
PersistentEntity entity = preInsertEvent.getEntity();
if(entity.isMultiTenant()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package grails.gorm.services.multitenancy.partitioned


import grails.gorm.MultiTenant
import grails.gorm.annotation.Entity
import grails.gorm.multitenancy.CurrentTenant
import grails.gorm.multitenancy.Tenants
import grails.gorm.multitenancy.WithoutTenant
import grails.gorm.services.Service
import grails.gorm.transactions.ReadOnly
Expand Down Expand Up @@ -39,12 +37,14 @@ class PartitionMultiTenancySpec extends Specification {
then:"You still get an exception"
thrown(TenantNotFoundException)

and:
bookDataService.countBooks() == 0

when:"But look you can change tenant"
System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "12")

then:
bookService.countBooks() == 0
bookDataService.countBooks() == 0

when:"And the new @CurrentTenant transformation deals with the details for you!"
bookService.saveBook("The Stand")
Expand All @@ -60,22 +60,18 @@ class PartitionMultiTenancySpec extends Specification {

then:
bookService.countBooks() == 0
bookDataService.countBooks() == 0

when: "calling a method save using Tenants.withoutId"
Book book1 = new Book(title: "The Secret", tenantId: 55)
Book book2 = new Book(title: "The Secret - 2", tenantId: 55)

bookDataService.saveBook(book1)
bookDataService.saveBook(book2)
bookDataService.countBooks() == 2

and: "Swapping to another schema and we get the right results!"
when: "Swapping to another schema and we get the right results!"
System.setProperty(SystemPropertyTenantResolver.PROPERTY_NAME, "55")

and: "calling a method save using Tenants.withoutId"
bookDataService.saveBook("The Secret")
bookDataService.saveBook("The Secret - 2")

then: "two books are created with tenantId 55"
bookService.countBooks() == 2
bookDataService.countBooks() == 2

bookDataService.countBooks() == 4

when: "book is saved without tenantId"
Book book3 = bookDataService.saveBook(
Expand All @@ -85,7 +81,6 @@ class PartitionMultiTenancySpec extends Specification {
then: "new book is saved without tenantId"
book3.id && !book3.tenantId


}
}

Expand Down Expand Up @@ -131,5 +126,6 @@ interface IBookService {
@WithoutTenant
Book saveBook(Book book)

@WithoutTenant
Integer countBooks()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import org.grails.datastore.mapping.multitenancy.MultiTenantCapableDatastore;
import org.grails.datastore.mapping.multitenancy.exceptions.TenantException;
import org.grails.datastore.mapping.query.Query;
import org.grails.datastore.mapping.query.event.PostQueryEvent;
import org.grails.datastore.mapping.query.event.PreQueryEvent;
import org.grails.datastore.mapping.reflect.EntityReflector;
import org.springframework.context.ApplicationEvent;

import java.io.Serializable;
Expand Down Expand Up @@ -53,7 +51,7 @@ public void onApplicationEvent(ApplicationEvent event) {
Query query = preQueryEvent.getQuery();

PersistentEntity entity = query.getEntity();
if(entity.isMultiTenant()) {
if (entity.isMultiTenant()) {
if(datastore == null) {
datastore = GormEnhancer.findDatastore(entity.getJavaClass());
}
Expand All @@ -64,16 +62,16 @@ public void onApplicationEvent(ApplicationEvent event) {

if(datastore instanceof MultiTenantCapableDatastore) {
currentId = Tenants.currentId((MultiTenantCapableDatastore) datastore);
}
else {
} else {
currentId = Tenants.currentId(datastore.getClass());
}
query.eq(tenantId.getName(), currentId );
if (currentId != ConnectionSource.DEFAULT) {
query.eq(tenantId.getName(), currentId );
}
}
}
}
}
else if((event instanceof ValidationEvent) || (event instanceof PreInsertEvent) || (event instanceof PreUpdateEvent)) {
} else if((event instanceof ValidationEvent) || (event instanceof PreInsertEvent) || (event instanceof PreUpdateEvent)) {
AbstractPersistenceEvent preInsertEvent = (AbstractPersistenceEvent) event;
PersistentEntity entity = preInsertEvent.getEntity();
if(entity.isMultiTenant()) {
Expand Down