Created
February 28, 2022 20:32
-
-
Save onurcill/9933881f7ebccb4c7ec85ac3a00f1a99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Aspect | |
@Component | |
public class TenantServiceAspect { | |
@PersistenceContext public EntityManager entityManager; | |
@Pointcut("execution(public * org.springframework.data.repository.Repository+.*(..))") | |
void isRepository() { | |
/* aspect */ | |
} | |
@Pointcut(value = "isRepository()") | |
void enableMultiTenancy() { | |
/* aspect */ | |
} | |
@Around("execution(public * *(..)) && enableMultiTenancy()") | |
public Object aroundExecution(final ProceedingJoinPoint pjp) throws Throwable { | |
if (!NoMultiTenancyConfiguration.doesTargetClassInterfaceHasNoMultiTenancyAnnotation(pjp.getTarget())) { | |
if(!TenantAssistance.resolveCurrentUser().equals("admin")){ | |
final Session session = this.entityManager.unwrap(Session.class); | |
final Filter filter = | |
session | |
.enableFilter(TENANT_FILTER_NAME) | |
.setParameter( | |
TENANT_FILTER_ARGUMENT_NAME, TenantAssistance.resolveCurrentTenantIdentifier()); | |
filter.validate(); | |
if (Objects.isNull(session.getEnabledFilter(TENANT_FILTER_NAME))) { | |
throw new UnknownTenantException("Hibernate filter is not enabled"); | |
} | |
} | |
} | |
return pjp.proceed(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment