Files
LinHelp/backend/src/main/java/com/linhelp/common/security/TenantContext.java
2026-07-07 15:12:51 +08:00

45 lines
1.0 KiB
Java

package com.linhelp.common.security;
public final class TenantContext {
private static final ThreadLocal<Scope> SCOPE = new ThreadLocal<Scope>();
private TenantContext() {
}
public static void set(Long tenantId, Long communityId) {
SCOPE.set(new Scope(tenantId, communityId));
}
public static Long tenantId() {
Scope scope = SCOPE.get();
return scope == null ? null : scope.tenantId();
}
public static Long communityId() {
Scope scope = SCOPE.get();
return scope == null ? null : scope.communityId();
}
public static void clear() {
SCOPE.remove();
}
private static class Scope {
private final Long tenantId;
private final Long communityId;
private Scope(Long tenantId, Long communityId) {
this.tenantId = tenantId;
this.communityId = communityId;
}
private Long tenantId() {
return tenantId;
}
private Long communityId() {
return communityId;
}
}
}