fix 添加租户

master
jay 2024-06-14 10:41:37 +08:00
parent ec7e0f5c21
commit 050d4c6c2e
4 changed files with 74 additions and 0 deletions

View File

@ -49,6 +49,9 @@ public class TenantFilterAspect {
@AfterReturning(pointcut = "openSession()", returning = "session")
public void afterOpenSession(Object session) {
if(TenantHelper.isIgnore()){
return;
}
if (session instanceof Session) {
Long tenantId = LoginHelper.getTenantId();
Long dynamic = TenantHelper.getDynamic();

View File

@ -37,6 +37,7 @@ import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.Objects;
import java.util.function.Supplier;
/**
*
@ -51,6 +52,10 @@ public class TenantHelper {
private static final ThreadLocal<Long> TEMP_DYNAMIC_TENANT = new TransmittableThreadLocal<>();
/**
*
*/
private static final ThreadLocal<Boolean> IGNORE = new TransmittableThreadLocal<>();
/**
*
*/
@ -117,4 +122,67 @@ public class TenantHelper {
return tenantId;
}
public static void setIgnore(Boolean ignore) {
IGNORE.set(ignore);
}
/**
* ( {@link #disableIgnore()} )
*/
public static void enableIgnore() {
IGNORE.set(Boolean.TRUE);
}
/**
*
*/
public static void disableIgnore() {
IGNORE.remove();
}
/**
*
*
* @param handle
*/
public static void ignore(Runnable handle) {
enableIgnore();
try {
handle.run();
} finally {
disableIgnore();
}
}
/**
*
*
* @param handle
*/
public static <T> T ignore(Supplier<T> handle) {
enableIgnore();
try {
return handle.get();
} finally {
disableIgnore();
}
}
/**
*
*
* @return
*/
public static boolean isIgnore() {
Boolean aBoolean = IGNORE.get();
return Boolean.TRUE.equals(aBoolean);
}
public static void clear() {
TEMP_DYNAMIC_TENANT.remove();
IGNORE.remove();
}
}

View File

@ -49,6 +49,9 @@ public class TenantListener {
@PreRemove
@PrePersist
public void setTenant(TenantAware entity) {
if(TenantHelper.isIgnore()){
return;
}
Long tenantId = LoginHelper.getTenantId();
Long dynamic = TenantHelper.getDynamic();
if (!Objects.isNull(dynamic)) {