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

feat(core): Exclude domain for specific data sources in Multi Tenancy #1380

Merged
merged 1 commit into from
Mar 6, 2024
Merged
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 @@ -2,8 +2,10 @@

import org.grails.datastore.mapping.config.Entity;
import org.grails.datastore.mapping.model.PersistentEntity;
import org.grails.datastore.mapping.multitenancy.TenantDataSourceConfig;
import org.springframework.util.ClassUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -56,7 +58,7 @@ public static List<String> getConnectionSourceNames(PersistentEntity entity) {
public static boolean usesConnectionSource(PersistentEntity entity, String connectionSourceName) {
Class[] interfaces = ClassUtils.getAllInterfacesForClass(entity.getJavaClass());
if(isMultiTenant(interfaces)) {
return true;
return !isMultiTenantExcludedDataSource( entity, connectionSourceName );
}
else {
List<String> names = getConnectionSourceNames(entity);
Expand All @@ -74,4 +76,22 @@ protected static boolean isMultiTenant(Class[] interfaces) {
}
return false;
}

/**
* Returns whether the given entity should be excluded from given connection source name or not.
* It can be configured over {@link TenantDataSourceConfig} annotation
*
* @param entity The name of the multi tenant entity
* @param connectionSourceName The connection source name to check
* @return Whether the given connection should be excluded
*/
protected static boolean isMultiTenantExcludedDataSource(PersistentEntity entity, String connectionSourceName) {
TenantDataSourceConfig tdsc = (TenantDataSourceConfig) entity.getJavaClass().getAnnotation(TenantDataSourceConfig.class);
boolean result = false;
if ( null != tdsc ) {
final String[] dataSourcesToExclude = tdsc.dataSourcesToExclude();
result = Arrays.asList(dataSourcesToExclude).contains(connectionSourceName);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.grails.datastore.mapping.multitenancy;

import java.lang.annotation.*;

/**
* <p>An annotation that adds possibility to configure whether a domain should be excluded from particular
* data source(s). For example:</p>
*
* <pre>
* {@literal @}TenantDataSourceConfig(dataSourcesToExclude=["adminDataSource"])
* class Animal implements MultiTenant<Animal> {
* ...
* }
* </pre>
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TenantDataSourceConfig {
String[] dataSourcesToExclude() default "";
}
Loading