Skip to content

Changed tenantId to be a Long instead of Integer. #36

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion grails-app/domain/demo/DemoTenant.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DemoTenant implements Tenant {
domain blank: false, unique: true
}

Integer tenantId() {
Long tenantId() {
id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class MultiTenantService {
* The code will be executed in a new transaction with a new session
* to avoid other tenants entities laying in the first level cache to leak in.
*/
def doWithTenantId(Integer tenantId, Closure callback) {
Integer oldTenantId = currentTenant.get()
def doWithTenantId(Long tenantId, Closure callback) {
Long oldTenantId = currentTenant.get()
try {
if(log.debugEnabled) log.debug "doWithTenantId oldTenantId - $oldTenantId"
currentTenant.set(tenantId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class MtSingleDbPluginSupport {
// each tenant only sees and touches its own data.
multiTenantHibernateFilter(FilterDefinitionFactoryBean) {
defaultFilterCondition = ":tenantId = tenant_id"
parameterTypes = [ tenantId: "java.lang.Integer" ]
parameterTypes = [ tenantId: "java.lang.Long" ]
}

// Listens for new Hibernate sessions and enables the
Expand Down Expand Up @@ -123,7 +123,7 @@ class MtSingleDbPluginSupport {

static createWithThisTenantMethod(Class tenantClass, MultiTenantService mtService) {
tenantClass.metaClass.withThisTenant = { Closure closure ->
Integer tenantId = tenantId()
Long tenantId = tenantId()
if (tenantId == null) {
String exMessage = ("Can't execute closure in tenent namespace without a tenant id. "
+ "Make sure that the domain instance has been saved to database "
Expand All @@ -137,7 +137,7 @@ class MtSingleDbPluginSupport {
}

static createWithTenantIdMethod(Class tenantClass, MultiTenantService mtService) {
tenantClass.metaClass.'static'.withTenantId = { Integer tenantId, Closure closure ->
tenantClass.metaClass.'static'.withTenantId = { Long tenantId, Closure closure ->
mtService.doWithTenantId(tenantId, closure)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/java/grails/plugin/multitenant/core/CurrentTenant.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public interface CurrentTenant {
String TENANT_BEFORE_CHANGE_EVENT = "multitenant.set-current-tenant.before";
String TENANT_AFTER_CHANGE_EVENT = "multitenant.set-current-tenant.after";

Integer get();
Long get();

void set(Integer tenantId);
void set(Long tenantId);

boolean isSet();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
*/
public interface MultiTenantDomainClass {

Integer getTenantId();
Long getTenantId();

void setTenantId(Integer tenantId);
void setTenantId(Long tenantId);

}
4 changes: 2 additions & 2 deletions src/java/grails/plugin/multitenant/core/Tenant.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public interface Tenant {
* GORM to mistake it for a property and fail.
* @return tenant id, will often be a database PK
*/
Integer tenantId();
Long tenantId();

}
}
4 changes: 2 additions & 2 deletions src/java/grails/plugin/multitenant/core/TenantRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public interface TenantRepository {
* @return The tenant instance associated with the tenant id
* @throws TenantNotFoundException
*/
Tenant findByTenantId(Integer tenantId) throws TenantNotFoundException;
Tenant findByTenantId(Long tenantId) throws TenantNotFoundException;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MultiTenantAST implements ASTTransformation {
// TODO: ConstantExpression.NULL would be better, but that leads to all sorts of crazy problems
// with default GORM constraints. It looks like it will become easier to hook into this in Grails 1.4:
// https://github.com/grails/grails-core/blob/master/grails-core/src/main/groovy/org/codehaus/groovy/grails/validation/ConstraintsEvaluatorFactoryBean.java
public final static Integer NO_TENANT_VALUE = Integer.MIN_VALUE;
public final static Long NO_TENANT_VALUE = Long.MIN_VALUE;

@Override
public void visit(ASTNode[] astNodes, SourceUnit sourceUnit) {
Expand All @@ -45,7 +45,7 @@ private boolean hasTenantIdProperty(ClassNode node) {
}

private void addTenantProperty(ClassNode node) {
ClassNode integerType = new ClassNode(Integer.class);
ClassNode integerType = new ClassNode(Long.class);
ConstantExpression defaultValue = new ConstantExpression(NO_TENANT_VALUE);
Statement getterBlock = null;
Statement setterBlock = null;
Expand All @@ -60,4 +60,4 @@ private void implementInterface(ClassNode node, Class<?> interf) {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
@SuppressWarnings("serial")
public class TenantNotFoundException extends TenantException {

private Integer missingTenantId;
private Long missingTenantId;

public TenantNotFoundException(Integer tenantId) {
public TenantNotFoundException(Long tenantId) {
super("Unable to find tenant with id " + tenantId);
this.missingTenantId = tenantId;
}
Expand All @@ -22,8 +22,8 @@ public TenantNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public Integer getMissingTenantId() {
public Long getMissingTenantId() {
return missingTenantId;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
@SuppressWarnings("serial")
public class TenantSecurityException extends TenantException {

private Integer currentTenantId, loadedTenantId;
private Long currentTenantId, loadedTenantId;

public TenantSecurityException(String message, Integer currentTenantId, Integer loadedTenantId) {
public TenantSecurityException(String message, Long currentTenantId, Long loadedTenantId) {
super(message);
this.currentTenantId = currentTenantId;
this.loadedTenantId = loadedTenantId;
}

public Integer getCurrentTenantId() {
public Long getCurrentTenantId() {
return currentTenantId;
}

public Integer getLoadedTenantId() {
public Long getLoadedTenantId() {
return loadedTenantId;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
*/
public class CurrentTenantThreadLocal implements CurrentTenant {

private static ThreadLocal<Integer> currentTenant = new ThreadLocal<Integer>();
private static ThreadLocal<Long> currentTenant = new ThreadLocal<Long>();
private EventBroker eventBroker;

@Override
public Integer get() {
public Long get() {
return currentTenant.get();
}

Expand All @@ -24,7 +24,7 @@ public boolean isSet() {
}

@Override
public void set(Integer tenantId) {
public void set(Long tenantId) {
eventBroker.publish(TENANT_BEFORE_CHANGE_EVENT, tenantId);
currentTenant.set(tenantId);
eventBroker.publish(TENANT_AFTER_CHANGE_EVENT, tenantId);
Expand All @@ -34,4 +34,4 @@ public void setEventBroker(EventBroker broker) {
this.eventBroker = broker;
}

}
}
4 changes: 2 additions & 2 deletions src/java/grails/plugin/multitenant/core/log/TenantMDC.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public class TenantMDC {

@Consuming(CurrentTenant.TENANT_AFTER_CHANGE_EVENT)
public void currentTenantUpdated(Event event) {
Integer tenantId = (Integer) event.getPayload();
Long tenantId = (Long) event.getPayload();
String tenant = (tenantId == null) ? "no-tenant" : "tenant-" + tenantId;
MDC.put("tenant", tenant);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public interface TenantResolver {
* @return tenant id, don't return 'null' unless you know what you're doing
* @throws TenantResolveException
*/
Integer resolve(HttpServletRequest request) throws TenantResolveException;
Long resolve(HttpServletRequest request) throws TenantResolveException;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void doFilter(ServletRequest request, ServletResponse response,
try {
if (request instanceof HttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
Integer currentTenantId = tenantResolver.resolve(httpRequest);
Long currentTenantId = tenantResolver.resolve(httpRequest);
currentTenant.set(currentTenantId);
}

Expand All @@ -60,4 +60,4 @@ public void doFilter(ServletRequest request, ServletResponse response,
public void destroy() {
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,17 @@ public class TenantScope implements Scope, ApplicationContextAware {
private CurrentTenant currentTenant;
private ApplicationContext applicationContext;

private ConcurrentHashMap<Integer, Map<String, Object>> tenantBeanCache = new ConcurrentHashMap<Integer, Map<String, Object>>(50);
private ConcurrentHashMap<Long, Map<String, Object>> tenantBeanCache = new ConcurrentHashMap<Long, Map<String, Object>>(50);

/**
* Return the object with the given name from the underlying scope, creating
* it if not found in the underlying storage mechanism.
*/
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
Integer tenantId = currentTenant.get();
Long tenantId = currentTenant.get();
if (tenantId == null) { // ConcurrentHashMap does not allow nulls, so mapping to -1
tenantId = -1;
tenantId = -1L;
}
Map<String, Object> beanCache = getBeanCacheForTenant(tenantId);
if (!beanCache.containsKey(name)) {
Expand All @@ -50,7 +50,7 @@ public Object get(String name, ObjectFactory<?> objectFactory) {
return beanCache.get(name);
}

private Map<String, Object> getBeanCacheForTenant(Integer tenantId) {
private Map<String, Object> getBeanCacheForTenant(Long tenantId) {
if (!tenantBeanCache.containsKey(tenantId)) {
tenantBeanCache.put(tenantId, new ConcurrentHashMap<String, Object>(10));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public boolean onPreInsert(PreInsertEvent event) {
+ event.getEntity().getClass().getSimpleName() + "', but no tenant is set");
}

Integer currentTenantId = currentTenant.get();
Long currentTenantId = currentTenant.get();
hibernateEventPropertyUpdater.updateProperty(event, MultiTenantAST.TENANT_ID_FIELD_NAME, currentTenantId);
MultiTenantDomainClass entity = (MultiTenantDomainClass) event.getEntity();
entity.setTenantId(currentTenantId);
Expand All @@ -68,9 +68,9 @@ public boolean onPreInsert(PreInsertEvent event) {
@Override
public boolean onPreUpdate(PreUpdateEvent event) {
if (isMultiTenantEntity(event.getEntity())) {
Integer currentTenantId = currentTenant.get();
Long currentTenantId = currentTenant.get();
MultiTenantDomainClass entity = (MultiTenantDomainClass) event.getEntity();
Integer entityTenantId = entity.getTenantId();
Long entityTenantId = entity.getTenantId();
if (currentTenantId != null && !currentTenantId.equals(entityTenantId)) {
throw new TenantSecurityException("Tried to update '" + event.getEntity() + "' with another tenant id. Expected "
+ currentTenantId + ", found " + entityTenantId, currentTenantId, entityTenantId);
Expand All @@ -85,7 +85,7 @@ public void onLoad(LoadEvent event, LoadType loadType) throws HibernateException
Object LoadedEntity = event.getResult();
if (LoadedEntity != null && isMultiTenantEntity(LoadedEntity)) {
MultiTenantDomainClass entity = (MultiTenantDomainClass) LoadedEntity;
Integer currentTenantId = currentTenant.get();
Long currentTenantId = currentTenant.get();

// We won't be able to extract tenant-id from an association fetch.
// TODO: This is a bit scary as it means that we potentially can load entities from
Expand All @@ -97,7 +97,7 @@ public void onLoad(LoadEvent event, LoadType loadType) throws HibernateException
}
}

protected boolean allowEntityLoad(Integer currentTenantId, MultiTenantDomainClass entity) {
protected boolean allowEntityLoad(Long currentTenantId, MultiTenantDomainClass entity) {
if (currentTenantId != null) {
if (belongsToCurrentTenant(currentTenantId, entity)) {
return true;
Expand Down Expand Up @@ -127,7 +127,7 @@ public void onPostLoad(PostLoadEvent event) {
// MultiTenantDomainClass tenantEntity = (MultiTenantDomainClass) entity;
// System.out.println(" -> post load tenant id: " + tenantEntity.getTenantId());
//
// Integer tenantEntityId = tenantEntity.getTenantId();
// Long tenantEntityId = tenantEntity.getTenantId();
// if (!currentTenant.get().equals(tenantEntityId)) {
// System.out.println(" -> Warning! Detected another tenant");
// throw new TenantSecurityException("Tried to load '" + tenantEntity + "' with another tenant id. Expected "
Expand All @@ -143,7 +143,7 @@ public boolean onPreDelete(PreDeleteEvent event) {
boolean shouldVetoDelete = false;
if (isMultiTenantEntity(event.getEntity())) {
MultiTenantDomainClass tenantEntity = (MultiTenantDomainClass) event.getEntity();
Integer currentTenantId = currentTenant.get();
Long currentTenantId = currentTenant.get();

if (currentTenantId != null && !belongsToCurrentTenant(currentTenantId, tenantEntity)) {
log.warn("Tenant {} tried to delete another tenants entity {}", currentTenant.get(), tenantEntity);
Expand All @@ -154,8 +154,8 @@ public boolean onPreDelete(PreDeleteEvent event) {
return shouldVetoDelete;
}

protected boolean belongsToCurrentTenant(Integer currentTenantId, MultiTenantDomainClass entity) {
Integer entityTenantId = entity.getTenantId();
protected boolean belongsToCurrentTenant(Long currentTenantId, MultiTenantDomainClass entity) {
Long entityTenantId = entity.getTenantId();
return currentTenantId != null && currentTenantId.equals(entityTenantId);
}

Expand All @@ -171,4 +171,4 @@ public void setHibernateEventPropertyUpdater(HibernateEventPropertyUpdater updat
this.hibernateEventPropertyUpdater = updater;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public class TenantHibernateFilterEnabler {
@Consuming("hibernate.sessionCreated")
public void newHibernateSessionCreated(Event event) {
Session newSession = (Session) event.getPayload();
Integer currentTenantId = currentTenant.get();
Long currentTenantId = currentTenant.get();
updateFilterParameter(newSession, currentTenantId);
}

@Consuming(CurrentTenant.TENANT_AFTER_CHANGE_EVENT)
public void currentTenantUpdated(Event event) {
if (hasSessionBoundToThread()) {
Integer updatedTenantId = (Integer) event.getPayload();
Long updatedTenantId = (Long) event.getPayload();
Session currentSession = sessionFactory.getCurrentSession();
updateFilterParameter(currentSession, updatedTenantId);
}
Expand All @@ -46,7 +46,7 @@ private boolean hasSessionBoundToThread() {
return TransactionSynchronizationManager.hasResource(sessionFactory);
}

private void updateFilterParameter(Session session, Integer tenantId) {
private void updateFilterParameter(Session session, Long tenantId) {
if (tenantId != null) {
enableHibernateFilterForTenant(session, tenantId);
} else {
Expand All @@ -55,7 +55,7 @@ private void updateFilterParameter(Session session, Integer tenantId) {
}

@SuppressWarnings("unchecked")
private void enableHibernateFilterForTenant(Session session, Integer tenantId) {
private void enableHibernateFilterForTenant(Session session, Long tenantId) {
String filterName = multiTenantHibernateFilter.getFilterName();
Set<String> paramNames = multiTenantHibernateFilter.getParameterNames();
String paramName = paramNames.iterator().next();
Expand Down
4 changes: 2 additions & 2 deletions src/templates/DefaultTenantRepository.groovy.template
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ${tenantRepositoryClassName} implements CurrentTenantAwareRepositor
@Override
Tenant getCurrentTenant() {
Tenant tenantInstance = null
Integer currentTenantId = currentTenant.get()
Long currentTenantId = currentTenant.get()
if (currentTenantId != null) {
tenantInstance = findByTenantId(currentTenantId)
}
Expand All @@ -30,7 +30,7 @@ public class ${tenantRepositoryClassName} implements CurrentTenantAwareRepositor
}

@Override
Tenant findByTenantId(Integer tenantId) {
Tenant findByTenantId(Long tenantId) {
${tenantDomainClassName}.get(tenantId); // Assuming that you use ${tenantDomainClassName}'s primary key as tenant id
}
}
Loading